common controls
TRANSCRIPT
-
8/3/2019 Common Controls
1/37
-
8/3/2019 Common Controls
2/37
VB.NET INTRO Visual Basic .NET (VB.NET), is an object-oriented computer
programming language that can be viewed as an evolution of theclassicVisual Basic (VB), which is implemented on the .NET Framework.
There are four versions and five releases of Visual Basic .NET implementedby the Visual Basic Team.
Visual Basic .NET 2003 (VB 7.1)
Visual Basic 2005 (VB 8.0)
Visual Basic 2008 (VB 9.0)
Visual Basic 2010 (VB 10.0)
http://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Visual_Basichttp://en.wikipedia.org/wiki/.NET_Frameworkhttp://en.wikipedia.org/wiki/.NET_Frameworkhttp://en.wikipedia.org/wiki/.NET_Frameworkhttp://en.wikipedia.org/wiki/.NET_Frameworkhttp://en.wikipedia.org/wiki/.NET_Frameworkhttp://en.wikipedia.org/wiki/Visual_Basichttp://en.wikipedia.org/wiki/Visual_Basichttp://en.wikipedia.org/wiki/Visual_Basichttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Programming_languagehttp://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Object-oriented_programminghttp://en.wikipedia.org/wiki/Object-oriented_programming -
8/3/2019 Common Controls
3/37
Controls
A control is an object that can be drawn on to theForm to enable or enhance user interaction withthe application. Examples of these
controls, TextBoxes, Buttons, Labels etc. All these Windows Controls are based on the
Control class, the base class for all controls.
Visual Basic allows us to work with controls in
two ways: at design time and at runtime.
-
8/3/2019 Common Controls
4/37
-
8/3/2019 Common Controls
5/37
Notable properties of most of these Windows Controls
which are based on the Control class itself are
summarized in the table below.
-
8/3/2019 Common Controls
6/37
-
8/3/2019 Common Controls
7/37
Button Control
One of the most popular control in Visual Basic isthe Button Control (previously CommandControl).
They are the controls which we click and releaseto perform some action. Buttons are used mostlyfor handling events in code, say, for sending dataentered in the form to the database and so on.
The default event of the Button is the Click eventand the Button class is based on the ButtonBaseclass which is based on the Control class.
-
8/3/2019 Common Controls
8/37
-
8/3/2019 Common Controls
9/37
-
8/3/2019 Common Controls
10/37
Creating a Button in Code
Public Class Form1 Inherits System.Windows.Forms.FormPrivate Sub Form1_Load(ByVal sender As System.Object,ByVal e_As System.EventArgs) Handles_ MyBase.LoadDim Button1 as New Button()'declaring the button, Button1
Button1.Text="Creating a Button"'setting the text to be displayed on the ButtonButton1.Location=New Point(100,50)'setting the location for the Button where it should be createdButton1.Size=New Size(75,23)'setting the size of the Button
Me.Controls.Add(Button1)'adding the Button that is created to the form'the Me keyword is used to refer to the current object, in thiscase the FormEnd SubEnd Class
-
8/3/2019 Common Controls
11/37
MaskedTextBox
A MaskedTextBox control provides validationmechanism for user input on a Form.
For example, if we want a TextBox to accept datein mm/dd/yyyy format, we can set masking in
the MaskedTextBox. We can create a MaskedTextBox control using a
Forms designer at design-time or using theMaskedTextBox class in code at run-time (also
known as dynamically) the MaskedTextBox class is derived from
TextBoxBase that provides its fundamentalproperties*.
-
8/3/2019 Common Controls
12/37
-
8/3/2019 Common Controls
13/37
Properties Probably the most important property of a
masked text box, which sets it apart from the(traditional) text box control, is its ability to
control what the user can and cannot enter in thetext side. To visually configure this text, theMaskedTextBox class is equipped with aproperty named Mask.
Mask the default property and represents theformat of the input can be accepted by a control
There are two main ways you can configure it:
-
8/3/2019 Common Controls
14/37
Setting the mask property
The following code snippet sets the Mask property at run-time.dynamicMaskedTextBox.Mask = "00/00/0000"
-
8/3/2019 Common Controls
15/37
the PromptChar property & et cetra.
When you create a mask, to indicate a placeholder for aletter, a digit, or a symbol, the control uses a specificcharacter. The default character is the underscore.
the MaskedTextBox is equipped with a Boolean propertynamed BeepOnError. When this property is set to
True, if the user enters an invalid character, thecomputer produces the beep sound
MaskFull property represents if the mask values arecompleted.
the MaskedTextBox class is equipped with a Booleanproperty named HidePromptOnLeave. The defaultvalue of this property is False, which indicates that thecontrol would always show its placeholder(s
http://c/Users/shahid/Desktop/controls%20presentation/Windows%20Control%20%20The%20Masked%20Text%20Box.htmhttp://c/Users/shahid/Desktop/controls%20presentation/Windows%20Control%20%20The%20Masked%20Text%20Box.htm -
8/3/2019 Common Controls
16/37
Picture Box
PictureBoxes are used to display images onthem. The images displayed can be anythingvarying from Bitmap, JPEG, GIF, PNG or any
other image format files. The PictureBox controlis based on the Control class.
Notable property of the PictureBox Control inthe Appearance section of the properties window
is the Image property which allows to add theimage to be displayed on the PictureBox.
-
8/3/2019 Common Controls
17/37
-
8/3/2019 Common Controls
18/37
Adding Images to PictureBox
Images can be added to the PictureBox with theImage property from the Properties window orby following lines of code.
Private Sub Button1_Click(ByVal sender AsSystem.Object,_ByVal e As System.EventArgs) Handles Button1.ClickPictureBox1.Image =Image.FromFile("C:\sample.gif")
'loading the image into the picturebox using theFromFile method of the image class'assuming a GIF image named sample in C: driveEnd Sub
-
8/3/2019 Common Controls
19/37
Another important property is SizeMode
Controls how the PictureBox will handle imageplacement and control sizing.
ContextMenuStrip property
This property allow to display shortcut menuwhen a user clicks on the picture box.
The PictureBox control can also be use to displaythe video files.
The PictureBox is not a selectable control, whichmeans that it cannot receive input focus.
-
8/3/2019 Common Controls
20/37
ToolTip Represents a small rectangular pop-up window that
displays a brief description of a control's purposewhen the user rests the pointer on the control. provides useful contextual hints to the client ToolTip is not a control but a component which
means that when we drag a ToolTip from the
toolbox onto a form it will be displayed on thecomponent tray. Tooltip is an Extender provider component which
means that when you place an instance of aToolTipProvider on a form, every control on that
form receives a new property. This property can beviewed and set in the properties window where itappears as Tooltip on n, where n is the name of theToolTipProvider.
-
8/3/2019 Common Controls
21/37
Important methods. The most importantmethods on ToolTip controls you can use in C# or
VB.NET are the Show method, the Hide method, theSetToolTip method, and the GetToolTip method.First, the Show method forces the tip to bedisplayed.
Private Sub Form1_Load(sender As Object, e As System.EventArgs)Handles MyBase.Load
Create the ToolTip and associate with the Form container.Dim toolTip1 As New ToolTip() ' Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000toolTip1.InitialDelay = 1000toolTip1.ReshowDelay = 500 ' Force the ToolTip text to be displayedwhether or not the form is active.
toolTip1.ShowAlways = True ' Set up the ToolTip text for the Button andCheckbox.
toolTip1.SetToolTip(Me.button1, "My button1")toolTip1.SetToolTip(Me.checkBox1, "My checkBox1")
End Sub
-
8/3/2019 Common Controls
22/37
TextBox Represents a Windows text box control.
The TextBox is based on the TextBoxBase classwhich is based on the Control class. TextBoxes
are used to accept input from the user or used todisplay text.
The amount of text that can be stored in the Textproperty is 64K character capacity limit of the
TextBox control
http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx -
8/3/2019 Common Controls
23/37
Some Notable Properties: Some important properties in the Behavior section of the Properties
Window for TextBoxes. Enabled: Default value is True. To disable, set the property to
False.Multiline: Setting this property to True makes the TextBoxmultiline which allows to accept multiple lines of text. Default valueis False.PasswordChar: Used to set the password character. The text
displayed in the TextBox will be the character set by the user. Say, ifyou enter *, the text that is entered in the TextBox is displayed as *.ReadOnly: Makes this TextBox readonly. It doesn't allow to enterany text.Visible: Default value is True. To hide it set the property to False.
Important properties in the Appearance section
TextAlign:Allows to align the text from three possible options. Thedefault value is left and you can set the alignment of text to right orcenter.Scrollbars:Allows to add a scrollbar to a Textbox. Very usefulwhen the TextBox is multiline. You have four options with thisproperty. Options are are None, Horizontal, Vertical and Both.Depending on the size of the TextBox anyone of those can be used.
-
8/3/2019 Common Controls
24/37
-
8/3/2019 Common Controls
25/37
TextBox Event
The default event of the TextBox is theTextChanged Event which looks like this in code:
Private Sub TextBox1_TextChanged(ByVal sender AsSystem.Object, ByVal e As _System.EventArgs) Handles TextBox1.TextChanged
MsgBox(Textchanged")End Sub
-
8/3/2019 Common Controls
26/37
-
8/3/2019 Common Controls
27/37
RichTextBox RichTextBoxes are similar to TextBoxes but they provide
some advanced features over the standard TextBox. RichTextBox allows formatting the text, say adding
colors, displaying particular font types and so on. The RichTextBox, like the TextBox is based on the
TextBoxBase class which is based on the Control class. These RichTextBoxes came into existence because many
word processors these days allow us to save text in a rich
text format. With RichTextBoxes we can also create our own word
processors. We have two options when accessing text ina RichTextBox, text and rtf (rich text format). Text holdstext in normal text and rtf holds text in rich text format.
-
8/3/2019 Common Controls
28/37
-
8/3/2019 Common Controls
29/37
The text within the control can be assignedcharacter and paragraph formatting.
The SelectionFont property enables you to make textbold or italic. You can also use this property tochange the size and typeface of the text. TheSelectionColor property enables you to change thecolor of the text. To create bulleted lists you can usethe SelectionBullet property. You can also adjustparagraph formatting by setting theSelectionIndent, SelectionRightIndent, andSelectionHangingIndent properties.
The RichTextBox control provides methods thatprovide functionality for opening and saving files.The LoadFile method enables you to load an existingRTF or ASCII text file into the control.
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectioncolor.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionbullet.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionindent.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionrightindent.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionhangingindent.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.loadfile.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.loadfile.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionhangingindent.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionrightindent.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionindent.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionbullet.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectioncolor.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont.aspx -
8/3/2019 Common Controls
30/37
Code for Setting the Color of TextCode for setting the color for particular text looks like this:Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As _
System.EventArgs) Handles Button1.ClickRichTextBox1.SelectionStart = RichTextBox1.Find("are")'using the Find method to find the text "are" and setting it'sreturn'property to SelectionStart which selects the textRichTextBox1.SelectionColor = Color.Blue'setting the color for the selected text with SelectionColorpropertyRichTextBox1.SelectionStart =RichTextBox1.Find("working")RichTextBox1.SelectionColor = Color.YellowEnd Sub
-
8/3/2019 Common Controls
31/37
Code for Saving Files to RTFDrag two RichTextBoxes and two Buttons (Save, Load) onto the form. When
you enter some text in RichTextBox1 and click on Save button, the text fromRichTextBox1 is saved into a rtf (rich text format) file. When you click onLoad button the text from the rtf file is displayed into RichTextBox2. The
code for that looks like this:Private Sub Save_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Save.ClickRichTextBox1.SaveFile("hello.rtf")'using SaveFile method to save text in a rich text box to hard diskEnd Sub
Private Sub Load_Click(ByVal sender As System.Object, ByVal e As_System.EventArgs) Handles Load.ClickRichTextBox2.LoadFile("hello.rtf")'using LoadFile method to read the saved fileEnd Sub
-
8/3/2019 Common Controls
32/37
-
8/3/2019 Common Controls
33/37
-
8/3/2019 Common Controls
34/37
-
8/3/2019 Common Controls
35/37
-
8/3/2019 Common Controls
36/37
-
8/3/2019 Common Controls
37/37
References
http://www.dotnetperls.com/tooltip
http://www.devasp.net/net/articles/display/367
.html
http://www.dotnetperls.com/tooltiphttp://www.devasp.net/net/articles/display/367.htmlhttp://www.devasp.net/net/articles/display/367.htmlhttp://www.devasp.net/net/articles/display/367.htmlhttp://www.devasp.net/net/articles/display/367.htmlhttp://www.dotnetperls.com/tooltip