linked checkboxes, textfields and buttons

Japher

New member
Joined
Aug 14, 2004
Messages
1
Programming Experience
1-3
Public Class Form1

Inherits System.Windows.Forms.Form



#Region " Windows Form Designer generated code "



Public Sub New()

MyBase.New()



'This call is required by the Windows Form Designer.

InitializeComponent()



'Add any initialization after the InitializeComponent() call



End Sub



'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub



'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer



'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents c1 As System.Windows.Forms.CheckBox

Friend WithEvents txt1 As System.Windows.Forms.TextBox

Friend WithEvents Button1 As System.Windows.Forms.Button

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

Me.c1 = New System.Windows.Forms.CheckBox

Me.txt1 = New System.Windows.Forms.TextBox

Me.Button1 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'c1

'

Me.c1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.c1.Location = New System.Drawing.Point(88, 96)

Me.c1.Name = "c1"

Me.c1.TabIndex = 0

Me.c1.Text = "box1"

'

'txt1

'

Me.txt1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

Me.txt1.Location = New System.Drawing.Point(40, 32)

Me.txt1.Name = "txt1"

Me.txt1.Size = New System.Drawing.Size(192, 26)

Me.txt1.TabIndex = 2

Me.txt1.Text = "0.00"

'

'Button1

'

Me.Button1.Location = New System.Drawing.Point(72, 168)

Me.Button1.Name = "Button1"

Me.Button1.Size = New System.Drawing.Size(136, 32)

Me.Button1.TabIndex = 3

Me.Button1.Text = "Reset"

'

'Form1

'

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)

Me.ClientSize = New System.Drawing.Size(292, 266)

Me.Controls.Add(Me.Button1)

Me.Controls.Add(Me.txt1)

Me.Controls.Add(Me.c1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout(False)



End Sub



#End Region



Private Sub c1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles c1.CheckedChanged

If c1.Checked = True Then

txt1.Text = 10

Else

txt1.Text = 0

End If

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

c1.Checked = False

End Sub

End Class





In here I have used a textbox = txt1, a checkbox = c1and a button = button1.



The purpose of the button1 is to reset the value of the check box to it’s default state thus the linked textbox txt1 will show 0.an incident happens when we click the button right after loading the application. There we don’t change the status (unchecked) to checked. At this moment when we click the button, the initial value of the textbox 0.00 stays as it is it doesn’t change to 0. But this situation changes if we change the status of the check box by clicking and change to “Checked” and then we press button the value in the textbox is set to 0 instead of 0.00. Why is this happening, please explain? Further more

“c1.Checked = False” in the “button1_Click“event handler procedure is an assignment operation or a comparison operation?
 
If you put breakPoints at both procedured (c1_CheckedChanged & Button1_Click) and debug, you'll notice what happens.

Since you set the value of the Checked property to False in Button1's click event handler, the c1_Checked procedure doesn't execute since the value of c1 hasn't changed. c1's checked property is initially False and when you set it to False the property hasn't changed and therefore the CheckedChanged event isn't fired.
If you change the value of c1's Checked property to True by clicking the CheckBox then click the button, the CheckChanged event occurs since the Checked property has changed from True to False.
 
Back
Top