vb.net radiobutton progromatically setting 'Checked' and not cause 'Click' event - vb.net

I am loading a form with values from stored settings from the registry
Typically :
If sFormat = TG_ReportFormatDft Then
RadioButton1.Checked = True
........
Else
RadioButton2.Checked = True
..........
End If
TG_ReportFormatDft is a string constant and is of no significance. The radio buttons are grouped correctly and manually clicked behave correctly.
Later in the procedure I check if there has been a manual change :
'Follow what the user is doing
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
msReports_Format2 = TG_ReportFormatDft
DoButtons(True)
End Sub
Private Sub RadioButton2_Click(sender As Object, e As EventArgs) Handles RadioButton2.Click
msReports_Format2 = TG_ReportFormatAlt
DoButtons(True)
End Sub
Imagine my surprise when these downstream subroutines are triggered without a mouse click. It would appear that :
RadioButton1.Checked = True --- triggers the mouse click event.
I can understand an 'On Change' event but the mouse click has not happened.
How can I prevent this 'click' event from propagating ?

Declare a class level boolean variable "ClickedFromCode". Now when you are setting the values from code set the boolean to true. See below sample code.
Private ClickedFromCode As Boolean
Private Sub Intialize
ClickedFromCode = True
If sFormat = TG_ReportFormatDft Then
RadioButton1.Checked = True
Else
RadioButton2.Checked = True
End If
ClickedFromCode = False
End Sub
Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click
If ClickedFromCode Then
Return
End If
msReports_Format2 = TG_ReportFormatDft
DoButtons(True)
End Sub
Private Sub RadioButton2_Click(sender As Object, e As EventArgs) Handles RadioButton2.Click
If ClickedFromCode Then
Return
End If
msReports_Format2 = TG_ReportFormatAlt
DoButtons(True)
End Sub

Related

Access dynamically created UserControl's Public Declare - WinForms

Logic
There is Form1 with a FlowLayoutPanel 'flowItems' to be populated by UserControl 'UCItem'. For populating flowItems, an array is used. For-Loop loopes over the array and creates a new UCItem, gives it a tag name with number and adds it to flowItem. All this part works.
Issue
I want to change the public declared boolean variable 'isChecked' each time the newly created UCItem is clicked. For achieving this, I've added an event handler (UCItem.Click) which gets and sets the property.
How ever, I'm unable to access the public boolean variable in UCItem.
Code: UC_Item.vb
Public isChecked As Boolean = False
Private Sub toggle_color()
If Me.BackColor = Color.FromArgb(24, 24, 24) Then
Me.BackColor = Color.RoyalBlue
Me.txtName.BackColor = Color.RoyalBlue
Me.txtName.ForeColor = Color.Black
Me.BackgroundImage = Nothing
isChecked = True
Else
Me.BackColor = Color.FromArgb(24, 24, 24)
Me.txtName.BackColor = Color.Black
Me.txtName.ForeColor = Color.White
Me.BackgroundImage = Image.FromFile(Application.StartupPath & "/res/UCItem_Wallpaper.png")
isChecked = False
End If
End Sub
Private Sub UC_Item_Click(sender As Object, e As EventArgs) Handles MyBase.Click
toggle_color()
End Sub
Private Sub TxtName_Click(sender As Object, e As EventArgs) Handles txtName.Click
toggle_color()
End Sub
Code: Form1.vb
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim UCItem As UC_Item
flowItems.SuspendLayout()
For i As Integer = 0 To items.Count - 1
UCItem = New UC_Item
UCItem.Tag = "UCItem" & i
UCItem.txtName.Text = items(i).ToString
flowItems.Controls.Add(UCItem)
UCItem.Show() : UCItem.Visible = True
AddHandler(UCItem.Click), AddressOf UCItem_Click
Next
flowItems.ResumeLayout()
End Sub
Private Sub UCItem_Click(sender As Object, ByVal e As EventArgs)
' -- not working part --
' If sender.isChecked = True Then
' sender.isChecked = False
' else
' sender.isChecked = True
End Sub
Tried
I've tried passing 'UCItem As sender.Tag' but this doesn't work too. I can't access the .Tag and .Name property of sender in the click event.
Any help is appreciated!

How to uncheck other checkboxes by checking a checkbox?

When I press the chkCP1, it unchecks chkYP but chkCP doesn't display its checked state2; I need to double click chkCP before it displays its checked state3.
I used these codes:
Private Sub chkCP_CheckedChanged(sender As Object, e As EventArgs) Handles chkCP.CheckedChanged
chkYP.Checked = False
End Sub
Private Sub chkYP_CheckedChanged(sender As Object, e As EventArgs) Handles chkYP.CheckedChanged
chkCP.Checked = False
End Sub
Figure 1:
Figure 2:
Figure 3:
I would personally use radiobuttons for this as this is what they were intended to do. However, I have seen a time where the option was to select neither option like you can easily do with checkboxes. That being said, You should be able to achieve the desired result by just simply moving your original code into the click event of the checkboxes instead of the checkchanged event. The reason is that when you click one, it triggers the checkchanged event which sets it to false which in turn triggers that controls checkchanged event. Try replacing your original code with
Private Sub chkCP_Click(sender As Object, e As EventArgs) Handles chkCP.Click
chkYP.Checked = False
End Sub
Private Sub chkYP_Click(sender As Object, e As EventArgs) Handles chkYP.Click
chkCP.Checked = False
End Sub
edit:
I tried using if statements and it worked! However, I cannot uncheck the checkbox anymore.
Private Sub chkCP_CheckedChanged(sender As Object, e As EventArgs) Handles chkCP.CheckedChanged
If chkYP.Checked = True Then
chkYP.Checked = False
Else
chkCP.Checked = True
End If
End Sub
Private Sub chkYP_CheckedChanged(sender As Object, e As EventArgs) Handles chkYP.CheckedChanged
If chkCP.Checked = True Then
chkCP.Checked = False
Else
chkYP.Checked = True
End If
End Sub
You might have some issues with recursive event handlers here. If you set chkYP.Checked in chkCP_CheckedChanged, chkYP_CheckedChanged will be triggered. This sets chkCP.Checked, which triggers chkCP_CheckedChanged again.
You might try something like this:
Private _checking As Boolean
Private Sub chkCP_CheckedChanged(sender As Object, e As EventArgs) Handles chkCP.CheckedChanged
If Not _checking Then
_checking = True
chkYP.Checked = False
_checking = False
End If
End Sub
Private Sub chkYP_CheckedChanged(sender As Object, e As EventArgs) Handles chkYP.CheckedChanged
If Not _checking Then
_checking = True
chkCP.Checked = False
_checking = False
End If
End Sub
It may not win a beauty contest, but it might just do the job.
Using Radio Buttons might be a better solution if you want only one of N options selected.
Edit:
Charles May's answer is way more elegant. He handles the Click event instead of the CheckedChanged event. And that also seems to work fine when using the keyboard (pressing the spacebar to toggle the checkbox).

PictureBox is not showing up on button click event

I am using a PictureBox inside a button click event. When the button is clicked I am enabling the PictureBox and I am running a long database call and at the end of the process, I am trying to disable the PictureBox. Inside the PictureBox I have a loading GIF.
But I don't know what's happening. My PictureBox does not show up..
Please suggest how can I fix this. I tried Thread.Sleep(1000), but it didn't work.
Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
Me.PictureBox1.Visible = True
lblSuccess.Text = Nothing
UltraNumberOfConveyance.Value = Nothing
GetData() --Long Running Query
Me.PictureBox1.Visible = False
End Sub
My GetData Function:
Private Function GetData()
dsCheckPointTimes = GetCheckPointTimesByTerminalID()
dtDataTable = dsCheckPointTimes.Tables(0)
chkdtDataTable = dsCheckPointTimes.Tables(1)
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("ConveyanceName")) Then
lblConveyanceNameText.Text = chkdtDataTable.Rows.Item(0).Item("ConveyanceName").ToString()
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("NumberOfConveyance")) Then
UltraNumberOfConveyance.Value = chkdtDataTable.Rows.Item(0).Item("NumberOfConveyance")
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("Dock")) Then
UltratxtChangeLabel1.Value = chkdtDataTable.Rows.Item(0).Item("Dock")
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("Lines")) Then
UltratxtChangeLabel2.Value = chkdtDataTable.Rows.Item(0).Item("Lines")
End If
If Not DBNull.Value.Equals(chkdtDataTable.Rows.Item(0).Item("CHECKPOINTTYPE")) Then
SetFields(chkdtDataTable.Rows.Item(0).Item("CHECKPOINTTYPE"))
End If
If dtDataTable.Rows.Count > 0 Then
LoadFlow()
Else
lblSuccess.Text = "No Records Found! Please check the ordernumber"
lblSuccess.ForeColor = Color.Red
End If
Return Nothing
End Function
Use a BackgroundWorker to do this:
Private WithEvents bgw As New BackgroundWorker
Private Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
PictureBox1.Visible = True
bgw.RunWorkerAsync()
End Sub
Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork
GetData() --Long Running Query
End Sub
Private Sub bgw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
PictureBox1.Visible = False
End Sub

Is it possible to use ToolStrip Controller as TabMenu in VB.net?

i am new at VB.net.. i am making one Application for my friend. but i have one problem while using toolstrip...
i want to use toolstrip menu as tabmenu... like if i select any button from toolstrip menu, than form content change..just like while we change tab then form content will change which is inside that tab...is it possible to do so?
I don't have any code at the moment so i can not attach it..i have tried googling my problem but i didn't found any solution of this problem...hope you guys understand my problem..thank you!
If I understand you correctly, for each "tab" you could create a panel on your form and set the Visible property of each to False. Then for each button click event in your ToolStrip, you could make them all Visible=False apart from the one you want to show.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = False
End Sub
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
Panel1.Visible = True
Panel2.Visible = False
Panel3.Visible = False
End Sub
Private Sub ToolStripButton2_Click(sender As Object, e As EventArgs) Handles ToolStripButton2.Click
Panel1.Visible = False
Panel2.Visible = True
Panel3.Visible = False
End Sub
Private Sub ToolStripButton3_Click(sender As Object, e As EventArgs) Handles ToolStripButton3.Click
Panel1.Visible = False
Panel2.Visible = False
Panel3.Visible = True
End Sub
End Class

Enable and disable TextBox with a checkbox in visual basic

I've 6 TextBox and 6 CheckBox. Now I want to disable the TextBox1 with a CheckBox1 and reactivate it with the Same CheckBox.
How a can do it?
Edit1 15.55 14/02/2013
I have done so to solve my problem!
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True
End If
End Sub `
This will work, just add more for the other check boxes
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
What this does: if checkbox1 is check, the checked_changed event fires and the code inside is ran. The if statement looks to see if the checkbox is checked or not. If it is checked, then it sets the textbox1 to enabled, if not it sets it to disabled. Be sure to set the enabled property to either enabled or disabled when you create your program. If you want it to be enabled from the start, that is the default....otherwise set it to disabled in its properties view.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Enabled = CheckBox1.Checked
End Sub
Take a look at this tutorial below. After, look at the checkbox control's events and choose the most fitting one. The property you will be changing on the textbox is Enabled.
http://www.youtube.com/watch?v=4PbUryXqZ50
This works if you have a layer built in that you can send objects behind (therefore hide things). I use this as a way to make text boxes and other items appear and disappear depending on other selections.
Private Sub checkbox_Click()
If (checkbox = True) Then
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
Else
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
End If
End Sub
This worked for me:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Enabled = False
If Not TextBox1.Enabled Then
TextBox1.BackColor = Color.White
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
End Class