Sunteți pe pagina 1din 20

This lesson describes the TabControl control

Tomove a controlin the Windows FormsDesigneris very easy. Justclick and


dragthe control to itsnew position. In addition you can drag controlsin and out of
containercontrols (controls thatcontain,group, and helparrangeother controls).

These controls
includeFlowLayoutPanel,TableLayoutPanel,GroupBox,Panel,TabControl,
andSplitContainer.

TheTabControldisplays datagrouped by pages, while thetabsenable the user to


quicklyjump from page to page.

Meaning, each page is acontrol container, holding whatever controls you want for that
tab. When youclicka tab at design time or the user clicks one at runtime, the control
displays thecorresponding page.

The control can also displayscrollbarsif necessary, but it can be littleawkward.

The TabControl works fine if the data falls intonatural groupingsthat you can use for the
tab pages, but it doesn't work so well if the user must frequentlycompare valueson one
page with those on another, thatforcesthe user to jump back and forth.

To get or set the index of the currentlyselected tab, you use


theSelectedIndexproperty. However atdesign time, you can simplyclickthe tab you
want to select.
?
1 Private Sub TabControl_Load(sender As Object, e As EventArgs)
2 Handles MyBase.Load
3 ' select the tabpage #3
4 TabControl1.SelectedIndex = 2
End Sub
The most usefuleventof theTabControlcontrol isSelectedIndexChanged, which fires
when the control's selected tab indexchangeseither because theuserclicked a new
tab, or because yourcodeset the SelectedIndex or SelectedTab property.

?
1 Private Sub TabControl1_SelectedIndexChanged(sender As Object,
2 e As EventArgs) _
3 Handles TabControl1.SelectedIndexChanged
4 If TabControl1.SelectedIndex = 2 Then
5 Dim pic As New PictureBox
6 pic.Name = "Logo"
7 pic.Height = 100
8 pic.Width = 100
9 pic.Image = My.Resources.logo
1 pic.Location = New Point(20, 100)
0 TabControl1.TabPages(2).Controls.Add(pic)
1 '
1 End If
1 End Sub
2

This lesson describes the TableLayoutPanel control

"

Tomove a controlin the Windows FormsDesigneris very easy. Justclick and


dragthe control to itsnew position. In addition you can drag controlsin and out of
containercontrols (controls thatcontain,group, and helparrangeother controls).

These controls
includeFlowLayoutPanel,TableLayoutPanel,GroupBox,Panel,TabControl,
andSplitContainer.

TheTableLayoutPanelcontrol displays its contents in agrid. All thecellsin a


particularrowhave thesame height, and all the cells in a particularcolumnhave
thesame width.

"

This tutorial will show you how to make a stopwatch in visual basic:

Add a label to the form and change it's text to 00:00:00:000

To do that right click on the label and click on properties. In the properties window, change
the text as follows:
"

Change the font's size of the label. In the properties window, go to Font:

"

The font dialog will show up. Under the size, choose the number 28 then click Ok:
"

Add a timer to the form:

In the toolbox, drag the timer to the form. The timer will appear in the gray area below the
form:
"

Right click on Timer1 and click on properties. In the properties window, change the interval
property to 1.
"

Drag a listbox from the toolbox and place on the form as the picture below:

"

Add a button to the form and change it's text to Mark:


"

Add a button and change it's text to Start:


Add a button and change it's text to Stop:
Add a button and change it's text to Reset:

Let's write the code for the stopwatch:

Right click on the form and click on View Code. Add the following declaration
belowPublic Class Form1
?
1 Private stopwatch As New Stopwatch

Add the following Timer Tick event code:


?
1 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
2 As System.EventArgs) Handles Timer1.Tick
3 Dim elapsed As TimeSpan = Me.stopwatch.Elapsed
4 Label1.Text = String.Format("{0:00}:{1:00}:{2:00}:{3:00}",
5_
6 Math.Floor(elapsed.TotalHours), _
7 elapsed.Minutes, elapsed.Seconds, _
elapsed.Milliseconds)
End Sub
Add the following Start Button click event code
?
1 Private Sub Button2_Click(ByVal sender As System.Object, ByVal
2 e As System.EventArgs) Handles Button2.Click
3 Timer1.Start()
4 Me.stopwatch.Start()
5 Button4.Enabled = False
End Sub

Add the following Stop Button click event code


?
1 Private Sub Button3_Click(ByVal sender As System.Object, ByVal
2 e As System.EventArgs) Handles Button3.Click
3 Timer1.Stop()
4 Me.stopwatch.Stop()
5 Button4.Enabled = True
End Sub

Add the following Reset Button click event code


?
1 Private Sub Button4_Click(ByVal sender As System.Object, ByVal
2 e As System.EventArgs) Handles Button4.Click
3 Me.stopwatch.Reset()
4 Label1.Text = "00:00:00:00"
5 ListBox1.Items.Clear()
End Sub

Add the following Mark Button click event code


?
1 Private Sub Button1_Click_1(ByVal sender As System.Object,
2 ByVal e As System.EventArgs) Handles Button1.Click
3 ListBox1.Items.Add(ListBox1.Items.Count + 1 & " " &
Label1.Text)
End Sub

Test your program and Enjoy


To use the timer control:

First add the timer control to your Form:

Drag it from the ToolBox to your form

You can change the properties of Timer as needed, right click on it and click on Properties:

If you set Enabled to True, Timer will start ticking as soon as the form loads. Default is
False.

Interval: as described: The frequency of Elapsed events in milliseconds. 1000 = 1 second.

You can start the timer in your code. If you want to start the timer when you click a button
for example:
?
1 Private Sub Button1_Click(sender As System.Object, e As
2 System.EventArgs) _
3 Handles Button1.Click
4 Timer1.Start()
5 End Sub
6 To stop it:
Timer1.Stop()

To do things while timer is ticking, double click on the timer control, it should add the
Timer.Tick event for you:

?
1 Private Sub Timer1_Tick(sender As System.Object, e As
2 System.EventArgs) _
3 Handles Timer1.Tick
4 ' Add your code here
End Sub

You can also adjust the intervel frequency using a textbox:

Add a textbox, label and a button to your form:

Double Click the button and add the following code to the button click event:

?
1 Timer1.Interval = TextBox1.Text

Analog Clock Tutorial - Visual Basic .NET

Change both labels enable property to True.

Change the 12 labels text properties to the clock 12 hours. Change also the BackColor
property to white:
Add another label to the form and name it "time"

Let's add the code for the Form Load event:


?
1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As
2 System.EventArgs) Handles Me.Load
3 ' adjust the clock numbers positions on the screen:
4 Label1.Location = New Point(505, 203)
5 Label2.Location = New Point(561, 261)
6 Label3.Location = New Point(592, 335)
7 Label4.Location = New Point(561, 404)
8 Label5.Location = New Point(505, 464)
9 Label6.Location = New Point(430, 489)
1 Label7.Location = New Point(357, 463)
0 Label8.Location = New Point(305, 408)
1 Label9.Location = New Point(272, 334)
1 Label10.Location = New Point(302, 259)
1 Label11.Location = New Point(354, 200)
2 Label12.Location = New Point(430, 177)
1 End Sub
3
1
4
1
5

Then add the following highlighted declarations to the code:


?
1 Public Class Form1
2 ' tick will be used to draw 60 marks for each second on
3 the clock
4 Dim tick As Integer = 270
5 'tick2 will be used to draw 12 bold marks on each hour on
the clock
Dim tick2 As Integer = 270

Then add the timer click event code


?
1 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e
2 As System.EventArgs) Handles Timer1.Tick
3 'timer1 will draw the hands of the clock
4 'convert seconds to angles
5 Dim seconds As Integer = (Now.Second * 6) + 270
6 'convert minutes to angles
7 Dim minutes As Integer = (Now.Minute * 6) + 270
8 'convert hours to angles
9 Dim hours As Integer = (Now.Hour * 30) + 270
1 'text label will hold the current time
0 Time.Text = Now
1 'creating graphics
1 Dim g As Graphics
1 g = Me.CreateGraphics
2 'creating pens
1 Dim hour As New Pen(Color.Blue)
3 Dim hour2 As New Pen(Color.White)
1 Dim second As New Pen(Color.Black)
4 Dim minute As New Pen(Color.Red)
1 Dim minute2 As New Pen(Color.White)
5 Dim white As New Pen(Color.White)
1 Dim circle As New Pen(Color.Black)
6 'assigning pens width
1 hour.Width = 8
7 hour2.Width = 10
1 second.Width = 1
8 minute.Width = 4
1 minute2.Width = 4
9 white.Width = 10
2 circle.Width = 5
0 'drawing the hands of the clock and their locations
2 g.DrawPie(hour2, 319, 219, 240, 240, hours - 30, 360)
1 g.DrawPie(minute2, 289, 189, 300, 300, minutes - 6,
2 360)
2 g.DrawPie(Pens.White, 269, 169, 340, 340, seconds - 6,
2 360)
3 g.DrawPie(hour, 319, 219, 240, 240, hours, 360)
2 g.DrawEllipse(white, 319, 219, 240, 240)
4 g.DrawPie(minute, 289, 189, 300, 300, minutes, 360)
2 g.DrawEllipse(white, 289, 189, 300, 300)
5 g.DrawPie(second, 269, 169, 340, 340, seconds, 360)
2 g.DrawEllipse(white, 269, 169, 340, 340)
6 'Draw a circle around the clock
2 g.DrawEllipse(circle, 249, 149, 380, 380)
7
2 End Sub
8
Add Timer2 Tick event code:
?
1 Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e
2 As System.EventArgs) Handles Timer2.Tick
3 ' timer2 will draw the shape of the clock and the
4 marks
5 tick += 6
6 tick2 += 30
7 Dim g As Graphics
8 Dim hoursMarks As New Pen(Color.Black)
9 hoursMarks.Width = 5
1
0 g = Me.CreateGraphics
1
1 g.DrawPie(Pens.Black, 249, 149, 380, 380, tick, 360)
1 g.DrawPie(hoursMarks, 249, 149, 380, 380, tick2, 360)
2 g.DrawEllipse(Pens.White, 269, 169, 340, 340)
1 g.FillEllipse(Brushes.White, 269, 169, 340, 340)
3 If tick > 800 Then
1 'drawing the shape is done and the timer will stop
4 Timer2.Stop()
1 tick = 270
5 tick2 = 270
1 End If
6 End Sub
1
7
1
8
1
9
2
0
2
1

Note: Change the form WindowState property to Maximum to better see the clock
working

S-ar putea să vă placă și