15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

st<strong>and</strong>ard Controls <strong>and</strong> Components ❘ 1137<br />

the image is sized <strong>and</strong> positioned in the control. The SizeMode property can be AutoSize, CenterImage,<br />

Normal, <strong>and</strong> StretchImage.<br />

You can change the size of the display of the PictureBox by setting the ClientSize property. You load the<br />

PictureBox by first creating an Image-based object. For example, to load a JPEG file into a PictureBox,<br />

you would do the following:<br />

Bitmap myJpeg = Bitmap.FromFile("whatever.png") as Bitmap;<br />

pictureBox1.Image = myJpeg;<br />

Progressbar<br />

The ProgressBar control is a visual clue to the status of a lengthy operation. It indicates to users that there<br />

is something going on <strong>and</strong> that they should wait. The ProgressBar control works by setting the Minimum<br />

<strong>and</strong> Maximum properties, which correspond to the progress indicator being all the way to the left (Minimum)<br />

or all the way to the right (Maximum). You set the Step property to determine the number that the value is<br />

incremented each time the PerformStep method is called. You can also use the Increment method <strong>and</strong><br />

increment the value by the value passed in the method call. The Value property returns the current value of<br />

the ProgressBar.<br />

Textbox, richTextbox, <strong>and</strong> maskedTextbox<br />

The TextBox control is one of the most used controls in the toolbox. The TextBox, RichTextBox, <strong>and</strong><br />

MaskedTextBox controls are all derived from TextBoxBase. TextBoxBase provides properties such as<br />

MultiLine <strong>and</strong> Lines. MultiLine is a Boolean value that allows the TextBox control to display text in<br />

more than one line. Each line in a text box is a part of an array of strings. This array is exposed through the<br />

Lines property. The Text property returns the entire text box contents as a single string. TextLength is<br />

the total length of the string that text would return. The MaxLength property will limit the length of the<br />

text to the specified amount.<br />

SelectedText, SelectionLength, <strong>and</strong> SelectionStart all deal with the currently selected text in the text<br />

box. The selected text is highlighted when the control has focus.<br />

The TextBox control adds a couple of interesting properties. AcceptsReturn is a Boolean value that will<br />

allow the TextBox to capture the Enter key. If the value is set to false, the Enter key clicks the default<br />

button on the form. When AcceptsReturn is set to true, pressing the Enter key creates a new line in the<br />

TextBox. CharacterCasing determines the casing of the text in the text box. The CharacterCasing<br />

enumeration contains three values, Lower, Normal, <strong>and</strong> Upper. Lower lowercases all text regardless of how<br />

it is entered, Upper renders all text in uppercase letters, <strong>and</strong> Normal displays the text as it is entered. The<br />

PasswordChar property takes a char that represents what is displayed to the users when they type text<br />

in the text box. This is typically used for entering passwords <strong>and</strong> PINs. The text property will return the<br />

actual text that was entered; only the display is affected by this property.<br />

The RichTextBox is a text-editing control that can h<strong>and</strong>le special formatting features. As the name<br />

implies, the RichTextBox control uses Rich Text Format (RTF) to h<strong>and</strong>le the special formatting. You can<br />

make formatting changes by using the Selection properties: SelectionFont, SelectionColor, <strong>and</strong><br />

SelectionBullet, <strong>and</strong> paragraph formatting with SelectionIndent, SelectionRightIndent, <strong>and</strong><br />

SelectionHangingIndent. All of the Selection properties work in the same way. If a section of text is<br />

highlighted, a change to a Selection property affects the selected text. If no text is selected, the change<br />

takes effect with any text that is inserted to the right of the current insertion point.<br />

The text of the control can be retrieved by using the Text property or the Rtf property. The Text property<br />

returns just the text of the control, whereas the Rtf property returns the formatted text.<br />

The LoadFile method can load text from a file in a couple of different ways. It can use a string that<br />

represents the path <strong>and</strong> filename or it can use a stream object. You can also specify the RichText<br />

BoxStreamType. The following table lists the values of RichTextBoxStreamType.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!