Text&Richtextbox Visual basic 6.0

Embed Size (px)

DESCRIPTION

Text&Richtextbox Visual basic 6.0

Citation preview

VISUAL BASIC CONTROLSTEXT BOXES & RICH TEXT BOXES

TEXT BOX IN VBText box

CREATING MULTI-LINE, WORD WRAP TEXT BOXES} MultiLine

property to true

ALIGNING AND ADDING SCROLL BARS IN TEXT BOXES}

Use Alignment property to align your text in a justified manner} } }

0-left justified 1-right justified 2-centered

}

Using Scroll bars property to add scroll bars in our text box and there are four ways in adding scroll bars} } } }

0-none 1-horizontal 2-vertical 3-both

MAKING A TEXT BOX READONLY}

Locking a text box} }

We use Locked property to true to make a text box read-only Under programs control only we can add any textPrivate sub command1_click()Text1.text=This box is locked

End sub

}

Disabling a text box} } }

We can use Enabled property as false to disable the text box Once disabled the text box appears gray Disabling is better done to indicate that the control is inaccessible Instead of text boxes we can use read-only text label controls with the help of label captions

}

Using labels instead of text boxes}

ACCESSING TEXT IN A TEXT BOX} To

access the text in the text box we dont have any property called SetText() } Instead we use Text property to do it} }

Private Sub Command1_click()}

Text1.text=Hello from visual basic

End Sub

SELECTING AND REPLACING TEXT IN A TEXT BOX}

To work with part of the text in a text box, we select the text we want using three properties} }

}

SetLength Returns or sets the number of characters selected SelStart Returns or sets the starting point of selected text. If no text is selected, SelStart indicates the position of the insertion point. SelText, Returns or sets the string containing the currently selected text.If no characters are selected, SelText consist of a zero-length string()Private Sub command1_click()Text1.Selstart=0 Text1.SelLength=len(Text1.Text) Text1.SelText=Welcome to visual basic

End Sub

CREATING A PASSWORD} To

convert a standard text box into a password box, we use the PasswordChar property and usually we add * symbol to show a different text

ADDING RICH TEXT BOX IN VB

Components adding method for rich text box

INSERTING RICH TEXT BOXES IN THE TOOLS BAR

Rich text box

ACCESSING TEXT IN A RICH TEXT BOX} }

}

To access text in a rich text box, you can use two properties: Text and TextRTF. As their names imply, Text holds the text in a rich text box in plain text format, and TextRTF holds the text in Rich Text Format. An example where we read the text in RichTextBox1 without any RTF codes and display that text as plain text in RichTextBox2:Private Sub Command1_Click()RichTextBox2.Text = RichTextBox1.Text

End Sub

}

Heres the same operation where we transfer the text including all RTF codesthat is, here were transferring rich text from one rich text box to another:Private Sub Command1_Click()RichTextBox2.TextRTF = RichTextBox1.TextRTF

End Sub

SELCTING TEXT IN A RICH TEXT BOX} }

} }

Rich text boxes support the SetText property just like standard text boxes. SetText only works with plain text. You can set the start and end of plain-text selection with the SelStart and SelLength properties. If you want to work with RTF-selected text, on the other hand, use the SelRTF property. For example, heres how we select the first 10 characters in RichTextBox1 and transfer them to RichTextBox2 using SelRTF:Private Sub Command1_Click()RichTextBox1.SelStart = 0 RichTextBox1.SelLength = 10 RichTextBox2.TextRTF = RichTextBox1.SelRTF

End Sub

(Contd.)THE SPAN METHOD}

Besides the SelRTF property, you can use the Span() method to select text based on a set of characters:RichTextBox.Span characterset, [forward, [negate]]

}

}

} }

}

The characterset parameter is a string that specifies the set of characters to look for. The forward parameter determines which direction the insertion point moves. The negate parameter specifies whether the characters in characterset define the set of target characters or are excluded from the set of target characters. We use Span() to extend a selection from the current insertion point based on a set of specified characters. This method searches the text in the rich text box and extends the text selection to include as many of the characters weve specified in the character set that it can find. Heres an example where we use Span() to find the word underlined and underline it:Private Sub Command1_Click() RichTextBox1.Text = "This rich text box supports underlined, bold, _ italic, and strikethru text." RichTextBox1.SelStart = RichTextBox1.Find("underlined") RichTextBox1.Span ("underlined") RichTextBox1.SelUnderline = True End Sub

USING BOLD, ITALIC UNDERLINE AND StirkeThru} To

make text bold, italic, underlined, or strikethru, you use the SelBold, SelItalic, SelUnderline, and SelStrikethru properties. } These properties work on selected RTF text only, so you have to select the text whose format you want to change

INDENTING TEXT IN RICHTEXT BOXES}

}

One of the aspects of word processors that users have gotten used to is the ability to indent text, and rich text boxes have this capability. To indent paragraph-by-paragraph, you use these properties: SelIndentIndents the first line of the paragraph SelHangingIndentIndents all other lines of the paragraph with respect to SelIndent SelRightIndentSets the right indentation of the paragraphPrivate Sub Command1_Click() RichTextBox1.SelIndent = 500 RichTextBox1.SelHangingIndent = -250 RichTextBox1.SelRightIndent = 100 End Sub

SETTING FONT AND FONT SIZES IN RICH TEXT BOXESset a selections font, you just set the SelFontName to the new font name (for example, Arial or Times New Roman). } To set a selections font size, you just set the SelFontSize property.} To

USING BULLETS IN RICH TEXT BOXES}

}

} }

Rich text boxes support bullets, those black dots that appear in lists of items that you want to set off in text. Putting a bullet in front of each item gives the list a snappy appearance and helps the reader assimilate the information quickly. To set bullets, you use the SelBullet and BulletIndent properties. The SelBullet property displays a bullet in front of the paragraph in which the current selection is; the BulletIndent property indicates how much we want the bullet to be indented from the left.

ALIGNING TEXT IN A RICHTEXTBOXES}

}

}

We can set the alignment of text in a rich text box paragraph-by-paragraph using the SelAlignment property. We just select the paragraph you want to align, or place the insertion point in that paragraph, and set the SelAlignment property to one of the following values: rtfLeft0(the default); the paragraph is aligned along the left margin. rtfRight1; the paragraph is aligned along the right margin. rtfCenter2; the paragraph is centered between the left and right margins.

SETTING TEXT COLOR IN RTF BOXES}

}

To set colors in a rich text box, you just make a selection and set the rich text boxs SelColor property using the RGB() function. For example,Private Sub Command1_Click()RichTextBox1.Text = "This rich text box supports font colors like _ red and blue and green." RichTextBox1.SelStart = RichTextBox1.Find("red") RichTextBox1.Span ("red") RichTextBox1.SelColor = RGB(255, 0, 0) . . . .

End Sub

MOVING THE INSERTION POINT IN RTF BOXS} Using

the UpTo() method, you can move the insertion point around in a rich text box. } Moving the insertion point ourself can be a powerful technique in a rich text box.RichTextBox.UpTo(characterset, forward, negate)

Adding Superscripts And Subscripts In Rich Text Boxes}

} }

The rich text box has SelCharOffset property, we can use this property to make a selection of superscript or subscriptif we set this value to a positive value, we get a superscript, and if you set it to a negative value, we get a subscript. Lets see an example. Here we can display a simple quadratic equation using this textX12 + 2X1 + 1 = 0

} }

where well make the 1s subscripts and the first 2 a superscript. We start by displaying that text in a rich text box:Private Sub Form_Load()RichTextBox1.Text = "X12 + 2X1 + 1 = 0"

End Sub

(Contd.)} Next,

we select the characters we want and set the SelCharOffset property to positive or negative twip values to create superscripts and subscripts:Private Sub Command1_Click()RichTextBox1.UpTo ("1") RichTextBox1.Span ("1") RichTextBox1.SelCharOffset = RichTextBox1.UpTo ("2") RichTextBox1.Span ("2") RichTextBox1.SelCharOffset = 40 RichTextBox1.UpTo ("1") RichTextBox1.Span ("1") RichTextBox1.SelCharOffset =

End Sub

Setting The Mouse Pointer In Text Boxes And Rich Text Boxes} We

can set the mouse pointer when it travels over a text box or rich text box. } Just set the Mousepointer property to one of the values in Table, } For example,CONSTANT rtfDefault rtfArrow rtfCross rtfIbeam VALUE 0 1 2 3 DESCRIPTION (Default) Shape determined by the object Arrow Cross (cross-hair pointer) I Beam

Searching For (And Replacing) Text In RTF Boxes} }

rich text boxes do this with the Find() method. For example, if we placed this text in a rich text box:Private Sub Form_Load()RichTextBox1.Text = "Here is some text."

End Sub}

Next, we could search for the word some this way with Find():Private Sub Command1_Click()RichTextBox1.Find ("some") ...

End Sub} }

After we find an item, it becomes the new selection. So, if we wanted to replace the word some with, say, the, we could do that this way:Private Sub Command1_Click()RichTextBox1.Find ("some") RichTextBox1.SelRTF = "the"

End Sub

Saving RTF Files From Rich Text Box} We

use the SaveFile() method to save the text in a rich text box to disk, and doing that is really easyyou just use SaveFile() this way:RichTextBox.SaveFile(pathname, [filetype])

} We

can save text as plain or RTF text; the settings for filetype are as follows: rtfRTF0(the default); the RichTextBox control saves its contents as an RTF file. rtfText1; the RichTextBox control saves its contents as a text file.

Reading RTF Files Into A Rich Text Box}

} }

We can write files to disk from a rich text box with SaveFile(); how can you read files back in? You use LoadFile(). Like SaveFile(), LoadFile() is very easy to use:RichTextBox.LoadFile pathname, [filetype]

And you can load in plain text or RTF text files; the settings for filetype are as follows: rtfRTF0(The default); the RichTextBox control saves its contents as an RTF file. rtfText1; the RichTextBox control saves its contents as a text file.

Printing From A Rich Text Boxcan print from a rich text box using the SelPrint() method and the Visual Basic Printer object. } The only thing to remember here is that we should first initialize the printer by printing a string of zero length or similar operation.} You