Change a checkbox text and function based on checkbox.checkstate in groupbox - vb.net

I'm trying to teach myself VB .net and as my first project I'm trying to design a form that functions much like the checkboxes in Gmail. Tons of checkboxes in a group and one checkbox that sits outside the group to select/deselect those within.
I've gotten far enough to have that master checkbox do its thing, but I would really like to have the form notice whenever anything within the groupbox is checked by the user, then to change its text & function automatically. The code I came up with to change the text works, but I can't figure out where to put it:
For Each ctrl As CheckBox In GroupBox1.Controls
If ctrl.CheckState = 1 Then
CheckBox1.Text = "Deselect All"
End If
Next
I can link the code to a button push or a checkbox change, but I'd like it to be automatic since having the user click something to run the check defeats the purpose. I tried double clicking the groupbox and placing the code there but it does nothing. Also tried double clicking the form background but it does nothing there either. Please help.

As you have probably noticed, there may be a few different places where you need to do this. To reuse a piece of functionality, create a new method that does that job. Double-click the form, and place this just before the End Class:
''' <summary>Update each of the CheckBoxes that in the same GroupBox</summary>
''' <param name="sender">The CheckBox that was clicked.</param>
''' <param name="e"></param>
''' <remarks>It is assumed that only checkboxed that live in a GroupBox will use this method.</remarks>
Public Sub UpdateCheckBoxState(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Get the group box that the clicked checkbox lives in
Dim parentGroupBox As System.Windows.Forms.GroupBox = DirectCast(DirectCast(sender, System.Windows.Forms.CheckBox).Parent, System.Windows.Forms.GroupBox)
For Each ctrl As System.Windows.Forms.Control In parentGroupBox.Controls
'Only process the checkboxes (in case there's other stuff in the GroupBox as well)
If TypeOf ctrl Is System.Windows.Forms.CheckBox Then
'This control is a checkbox. Let's remember that to make it easier.
Dim chkBox As System.Windows.Forms.CheckBox = DirectCast(ctrl, System.Windows.Forms.CheckBox)
If chkBox.CheckState = 1 Then
chkBox.Text = "Deselect All"
Else
chkBox.Text = "Select All"
End If
End If ' - is CheckBox
Next ctrl
End Sub
Now you have a method that will do what you want, you need to connect it to each CheckBox that you want to manage. Do this by adding the following code in The Form_Load event:
AddHandler CheckBox1.CheckedChanged, AddressOf UpdateCheckBoxState
AddHandler CheckBox2.CheckedChanged, AddressOf UpdateCheckBoxState
...
So now the same method will handle the ClickChanged method of all of your connected checkboxes.
You can also update the checkBoxes in addition to when the user clicks it by calling the Method UpdateCheckBoxState(CheckBoxThatYouWantToProgramaticallyUpdate, Nothing) perhaps in Form_Load, or elsewhere.

Related

Use a virtual Keyboard on focused Textboxes and DataGridView Cells

In my Form I have various Textboxes that I write into with an in Form keyboard I created using Buttons. I have this code in Form.Load, which uses an event handler to determine which Textbox has the Focus:
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() FocussedTextbox = textBox
End If
Next
Then I use this on each button to write a specific character:
Private Sub btnQ_Click(sender As Object, e As EventArgs) Handles btnQ.Click
If btnQ.Text = "Q" Then
FocussedTextbox.Text += "Q"
ElseIf btnQ.Text = "q" Then
FocussedTextbox.Text += "q"
End If
End Sub
Up to that point I'm good and everything works as intended. The problem is I also have a DataGridView I want to write into but can't focus on it selected cells as I do on Textboxes.
I tried this:
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() FocussedTextbox = textBox
ElseIf control.GetType.Equals(GetType(DataGridViewCell)) Then
Dim DGVC As DataGridView = control
AddHandler DGVC.CellBeginEdit, Sub() FocussedTextbox = DGVC
End If
Next
But it just selects my last Textbox.
I declared the variable FocussedTextbox as Control so it's not specific to Textbox but any control.
Any help will be greatly appreciated.
To add text to the current ActiveControl using Buttons, these Button must not steal the focus from the ActiveControl (otherwise they become the ActiveControl).
This way, you can also avoid all those FocusedTextbox = textBox etc. and remove that code.
You just need Buttons that don't have the Selectable attribute set. You can use a Custom Control derived from Button and remove ControlStyles.Selectable in its constructor using the SetStyle method:
Public Class ButtonNoSel
Inherits Button
Public Sub New()
SetStyle(ControlStyles.Selectable, False)
End Sub
End Class
Replace your Buttons with this one (or, well, just set the Style if you're already using Custom Controls).
To replace the existing Buttons with this Custom Control:
Add a new class object to your Project, name it ButtonNoSel, copy all the code above inside the new class to replace the two lines of code you find there.
Build the Project. You can find the ButtonNoSel Control in your ToolBox now. Replace your Buttons with this one.
Or, open up the Form's Designer file and replace (CTRL+H) all System.Windows.Forms.Button() related to the Virtual KeyBoard with ButtonNoSel.
Remove the existing event handlers, these are not needed anymore.
Add the same Click event handler in the Constructor of the class that hosts those Buttons (a Form or whatever else you're using).
You can then remove all those event handlers, one for each control, that you have now; only one event handler is needed for all:
Public Sub New()
InitializeComponent()
For Each ctrl As Control In Me.Controls.OfType(Of ButtonNoSel)
AddHandler ctrl.Click, AddressOf KeyBoardButtons_Click
Next
End Sub
Of course, you also don't need to add event handlers to any other control, this is all that's required.
Now, you can filter the Control types you want your keyboard to work on, e.g., TextBoxBase Controls (TextBox and RichTextBox), DataGridView, NumericUpDown etc.
Or filter only special cases that need special treatment (e.g., MonthCalendar).
To add the char corresponding to the Button pressed, you can use SendKeys.Send(): it will insert the new char in the current insertion point, so you don't need any other code to store and reset the caret/cursor position as it happens if you set the Text property of a Control.
In this example, I'm checking whether the ActiveControl is a TextBoxBase Control, then just send the char that the clicked Button holds.
If it's a DataGridView, first send F2 to enter Edit Mode, then send the char.
You could also just send a char (so, no filter would be required), but in this case, you'll replace, not add to, the existing value of that Cell.
Private Sub KeyBoardButtons_Click(sender As Object, e As EventArgs)
Dim selectedButton = DirectCast(sender, Control)
Dim keysToSend = String.Empty
If TypeOf ActiveControl Is TextBoxBase Then
keysToSend = selectedButton.Text
ElseIf TypeOf ActiveControl Is DataGridView Then
Dim ctrl = DirectCast(ActiveControl, DataGridView)
If TypeOf ctrl.CurrentCell IsNot DataGridViewTextBoxCell Then Return
SendKeys.Send("{F2}")
keysToSend = selectedButton.Text
Else
' Whatever else
End If
If Not String.IsNullOrEmpty(keysToSend) Then
SendKeys.Send(keysToSend)
End If
End Sub
► Note that {F2} is sent just once: when the Cell enters Edit Mode, the ActiveControl is a DataGridViewTextBoxEditingControl, hence a TextBox Control, handled by the TextBoxBase filter.
This is how it works (using just the code posted here):

Visual Basic - Performing an action on an object that has the focus

I'm trying to do something very simple:
Create a method that handles multiple textbox.gotFocus() events in my form.
The goal is to select all text when the focus is given to any of the textboxes.
I know I could create an if-else block that checks to see whether or not each textbox has the focus, and then if so, just do textbox1.selectall(), or textbox2.selectall(), etc.
Is there a quicker way to do this that I'm missing that would just do it all in one statement that uses something like object.focused.selectall()? I know these aren't keywords in vb, but they're the best descriptive words I can think of to explain what I'm trying to do.
Any input is appreciated. Thanks!
What you want to do is more complicated than you think. You would have to create an event to trigger the method that handles the textboxes focus. The easy way is to put as you said a selectAll() method in every textbox gotFocus event.
Nothing built in to do that, unless you inherit from the TextBox and call SelectAll in the OnEnter override method yourself.
If you have a lot of TextBox controls, you could just iterate over the collections and add the method yourself:
Public Sub New()
InitializeComponent()
Dim ctrls As New Stack(Of Control)
ctrls.Push(Me)
While ctrls.Count > 0
Dim ctrl As Control = ctrls.Pop
If ctrl.Controls.Count > 0 Then
For Each c As Control In ctrl.Controls
ctrls.Push(c)
Next
Else
If TypeOf ctrl Is TextBox Then
AddHandler ctrl.Enter, Sub() DirectCast(ctrl, TextBox).SelectAll()
End If
End If
End While
End Sub
Since textbox doesn't have a focus event, what you can do is set up one handler to handle all the textboxes' Enter event.
Enter Event Handler
Private Sub TextboxEnter(Sender As Object, E As EventArgs)
Dim FocusedTextbox As TextBox = DirectCast(Sender, TextBox)
FocusedTextBox.SelectAll
End Sub
Add the handler for each textboxes' Enter event
For Each tb As TextBox in Me.Controls.OfType(Of TextBox)
AddHandler tb.Enter, AddressOf TextBoxEnter
Next
If you need to filter the textboxes, use a common name pattern and use a conditional to check for that pattern in the Name property.

All Controls Click event on form

Is there a simple way to Activate the form when any controls in the form is clicked like datagirdview, panel, menustrip, button, textbox, label, etc....
it happens that my project can show many different form and it's hard for me to activate one form when it's on the back of the active form. I need to clicked the border of the form to activate it.
most likely your problem arises because your form is MDI child.. you'll have to click the menu bar to activate the form.. but if you have a borderless form, clicking on controls wont activate the form.. again, this usually happens on MDI but it shouldnt happen on a regular winform.. unless you have other running events in the background that interferes with the process.
You question is not clear at all, and I don't know what you are trying to archieve, but for executing something with a click event you have to add the handler for every control.
If you are declaring it not dinamically just:
Private Sub ControlsClick(sender As Object, e As EventArgs) _
Handles Panel1.Click, Button1.Click, TextBox2.Click ' etc.
Me.Activate 'Or Whatever
End Sub
You have to add the handler for each control. The same if you do it dinamically:
Private Sub InitializeClickHandlers(sender As Control, Optional bChilds As Boolean = True)
For Each elem As Control In sender.Controls
AddHandler elem.Click, AddressOf ControlsClick(elem, New EventArgs)
If bChilds AndAlso elem.Controls.Count > 0 Then
Call InitializeClickHandlers(sender)
End If
Next
End Sub
Then, for every control in the form, you call it like: Call InitializeClickHandlers(Me)
Or, for every control inside a panel: Call InitializeClickHandlers(Panel1)

Access properties of controls on a programatically created user control VB .NET

After taking a few years off from programming, I decided to start learning vb.net. I have created a user control that contains a picture box. I added some custom properties to the picture box that will hold general string data from a database.
My problem is that the user control is programatically created during run time, and during this time a DoubleClick event handler is added for the picture box that is within the user control.
I need to be able to set the custom properties for the picture box during the creation of the user control, so that when the control (picture box) is double clicked I can read these values but am unsure on how to access them.
The picture box is the entire size of the user control, or I would just add the custom properties right to the user control and add the DoubleClick event handler to that. However, double clicking needs to be done on the picture box since it takes up the entire user control, unless anyone has an idea to trigger the DoubleClick event of the user control when the picture box is double clicked.
Here is a bit of code I am using to add the user control to the form programatically -
hb_item = New PictureLoader
With hb_item
.Name = "item_" & i
.Left = itemLeft
.Top = itemTop
.SetImageSizeMode = ImageLayout.Stretch
.SetLoadingImageSizeMode = ImageLayout.Stretch
.Size = New Size(100, 126)
.SetImage = BlobToImage(sql_reader("ThumbImage"))
.Visible = True
.SetHighlight(True)
.SetHighlightColor = Color.GreenYellow
.TextColor = Color.White
.CircleColor = Color.GreenYellow
'--- THIS UPDATES ONE OF THE CUSTOM PROPERTIES FOR THE PICTURE BOX
'--- CONTAINED WITHIN THE USER CONTROL
.SetID = "test"
AddHandler .picMainClick, AddressOf frmHome.HBItem_Click
AddHandler .picMainDoubleClick, AddressOf frmHome.HBItem_DoubleClick
End With
Here is the event handler code I am trying to access the picture box's custom properties from
Public Sub HBItem_DoubleClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.DoubleClick
With sender
'--- THIS IS WHERE I WANT TO READ THE DATA IN THE CUSTOM PROPERTIES
'--- OF THE PICTURE BOX... SOMETHING SIMILAR TO THE FOLLOWING -
' Database_ID is one of the custom properties of the sender (picMain
' control on the user control)
MessageBox.Show(.Database_ID)
End With
End Sub
EDIT: Got it all worked out, thanks for everything. All that was needed was casting the sender to the actual picture box like stated, I was just looking way to deeply into things. A simple one line of code is all that was needed in the event handler -
Dim pb As xPictureBox = CType(sender, xPictureBox)
Then all the custom properties could be accessed using pb.property_here.
sender is of type System.Object - you need to cast (convert) sender to the type it actually is (in your case, your custom user control), i.e.:
Dim myControl As MyCustomControl = CType(sender, MyCustomControl)
With myControl
MessageBox.Show(.Database_ID)
End With

Dynamic button click event handler

I've 100 buttons created dynamically in a form. How can I an add event handler to them?
You can use AddHandler to add a handler for any event.
For example, this might be:
AddHandler theButton.Click, AddressOf Me.theButton_Click
Just to round out Reed's answer, you can either get the Button objects from the Form or other container and add the handler, or you could create the Button objects programmatically.
If you get the Button objects from the Form or other container, then you can iterate over the Controls collection of the Form or other container control, such as Panel or FlowLayoutPanel and so on. You can then just add the click handler with
AddHandler ctrl.Click, AddressOf Me.Button_Click (variables as in the code below),
but I prefer to check the type of the Control and cast to a Button so as I'm not adding click handlers for any other controls in the container (such as Labels). Remember that you can add handlers for any event of the Button at this point using AddHandler.
Alternatively, you can create the Button objects programmatically, as in the second block of code below.
Then, of course, you have to write the handler method, as in the third code block below.
Here is an example using Form as the container, but you're probably better off using a Panel or some other container control.
Dim btn as Button = Nothing
For Each ctrl As Control in myForm.Controls
If TypeOf ctrl Is Button Then
btn = DirectCast(ctrl, Button)
AddHandler btn.Click, AddressOf Me.Button_Click ' From answer by Reed.
End If
Next
Alternatively creating the Buttons programmatically, this time adding to a Panel container.
Dim Panel1 As new Panel()
For i As Integer = 1 to 100
btn = New Button()
' Set Button properties or call a method to do so.
Panel1.Controls.Add(btn) ' Add Button to the container.
AddHandler btn.Click, AddressOf Me.Button_Click ' Again from the answer by Reed.
Next
Then your handler will look something like this
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
End Sub
#Debasish Sahu, your answer is an answer to another question, namely: how to know which button (or any other control) was clicked when there is a common handler for a couple of controls? So I'm giving an answer to this question how I usually do it, almost the same as yours, but note that also works without type conversion when it handles the same type of Controls:
Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim selectedBtn As Button = sender
MsgBox("you have clicked button " & selectedBtn.Name)
End Sub
I needed a common event handler in which I can show from which button it is called without using switch case... and done like this..
Private Sub btn_done_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox.Show("you have clicked button " & CType(CType(sender, _
System.Windows.Forms.Button).Tag, String))
End Sub
Some code for a variation on this problem. Using the above code got me my click events as needed, but I was then stuck trying to work out which button had been clicked.
My scenario is I have a dynamic amount of tab pages. On each tab page are (all dynamically created) 2 charts, 2 DGVs and a pair of radio buttons. Each control has a unique name relative to the tab, but there could be 20 radio buttons with the same name if I had 20 tab pages. The radio buttons switch between which of the 2 graphs and DGVs you get to see. Here is the code for when one of the radio buttons gets checked (There's a nearly identical block that swaps the charts and DGVs back):
Private Sub radioFit_Components_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If sender.name = "radioFit_Components" And sender.visible Then
If sender.checked Then
For Each ctrl As Control In TabControl1.SelectedTab.Controls
Select Case ctrl.Name
Case "embChartSSE_Components"
ctrl.BringToFront()
Case "embChartSSE_Fit_Curve"
ctrl.SendToBack()
Case "dgvFit_Components"
ctrl.BringToFront()
End Select
Next
End If
End If
End Sub
This code will fire for any of the tab pages and swap the charts and DGVs over on any of the tab pages. The sender.visible check is to stop the code firing when the form is being created.