How to acces checked status of dynamicaly created Radio Buttons in VB - vb.net

I create a variable amount of radio buttons in a Tab Page by reading the lines out of an array using:
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rbgen As RadioButton
Dim tab1 = 0
For y As Integer = 0 To Array.GetUpperBound(0) Step 1
If Array(y, 0) = "ABC" Then
rbgen = New RadioButton
rbgen.Name = "RButton" & Convert.ToString(y)
rbgen.Left = 10
rbgen.Top = ((tab1) * 30)
rbgen.Text = Array(y, 2)
rbgen.Size = New System.Drawing.Size(260, 40)
TabPage1.Controls.Add(rbgen)
tab1 = tab1 + 1
End If
Next
End Sub
When i click a "start" Button i need to run different code depending on the checked RadioButton. But how do i access the information of which radio Button is checked?
Thank you very much for your help!

You could try to add a Handler after you add the radio button:
AddHandler rbgen.CheckedChanged, AddressOf RadioBox_CheckedChanged
and then you need to add this sub
Private Sub RadioBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim RadioBox As RadioButton = TryCast(sender, RadioButton)
If RadioBox IsNot Nothing Then
MessageBox.Show(RadioBox.CheckState)
End If
End Sub
and if you want to check the status
For Each RadioBox In TabPage1.Controls.OfType(Of RadioButton)()
if Ctype(TabPage1.controls("RadioButton" & i), radiobutton).checked = true then
'your Code Here
end if
next
I could not test it yet but I hope I already helped you a little bit :)

Related

VB.NET get control name from button created at run time

I have this code to create 3 buttons at run time, which seems to be working ok.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim rawData As String
Dim FILE_NAME As String = "C:\temp\test.txt"
Dim data() As String
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
rawData = objReader.ReadLine() ' & vbNewLine
data = Split(rawData, ",")
'data 0 = X loc, data 1 = Y loc, data 2 = Part Num, data 3 = Reference Des
Dim dynamicButton As New Button
dynamicButton.Location = New Point(data(0), data(1))
dynamicButton.Height = 20
dynamicButton.Width = 20
dynamicButton.FlatStyle = FlatStyle.Flat
dynamicButton.BackColor = Color.Transparent
dynamicButton.ForeColor = Color.FromArgb(10, Color.Transparent)
'dynamicButton.Text = "+"
dynamicButton.Name = data(2)
dynamicButton.FlatAppearance.BorderColor = Color.White
'dynamicButton.Font = New Font("Georgia", 6)
AddHandler dynamicButton.Click, AddressOf DynamicButton_Click
Controls.Add(dynamicButton)
Dim myToolTipText = data(3)
ToolTip1.SetToolTip(dynamicButton, myToolTipText)
Loop
End Sub
I want to get the control name that was created when I click a button, but I cannot seem to get what I need. I have been doing this on the dynamicButton_click event.
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
For Each cntrl In Me.Controls
If TypeOf cntrl Is Button Then
MsgBox("")
Exit Sub
End If
Next
End Sub
All you need to do is cast the sender variable to button. It will tell you which control was the source of the event:
Private Sub DynamicButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = DirectCast(sender, Button)
MessageBox.Show("You clicked: " & btn.Name)
End Sub
You attached an event handler to the dynamic button and that is hitting?
Couple ways you can do this:
This will give you the button you just clicked without needing to loop, the sender is the button. You need to cast it from the object to button:
MessageBox.Show(DirectCast(sender, Button).Name)
Otherwise, inside your loop, cast cntrl (the generic Control object) to the specific Button control object and then get the name.
Dim btn as Button = DirectCast(cntrl, Button)
MessageBox.Show(btn.Name)

How to create widget name from a string in VB.NET?

I am working on a music program that gives user the option to enter a number in a textbox and creates that many buttons inside a container. I want every button created to be named 'btn' followed by a number generated in a loop. Following is the code I have so far:
Private Sub txtNum_TextChanged(sender As Object, e As EventArgs) Handles txtNum.TextChanged
For i As Integer = 1 To CInt(txtNum)
Dim strName as String = "btn" & i.ToString()
Dim <strName> as New Button
Next
End Sub
I know the line in my code where the button is being created doesn't work. How can I do this? Is there another way I can achieve this in VB.NET?
You should set the Name property of a button with, but I think you should avoid using the TextChanged event to do this. This handler is called everytime you type a single character in the textbox (also when your user mistypes something and the cancel its input to fix the error) so it is better to have a Create button that on click event handles the task
Private Sub bntCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
For i As Integer = 1 To CInt(txtNum.Text)
Dim strName as String = "btn" & i.ToString()
Dim btn as New Button
btn.Name = strName
' Now calculate the Location property to place your button
btn.Location = new Point(0, i * 20)
' Set the button click handler (same handler for all buttons)
AddHandler btn.Click, AddressOf onClick
' And finally add it to the form controls collection.
Me.Controls.Add(btn)
Next
End Sub
Sub onClick(ByVal sender as Object, ByVal args as EventArgs)
Dim btn = DirectCast(sender, Button)
if btn.Name = "btn1" Then
' Code for btn1
else if btn.Name = "btn2" Then
.....
End If
End Sub
The tricky part is how to position the buttons on your form. In the sample above the buttons are places in a vertical arrangement with 20 pixels of interval between their Top location, but this leads to other problems (buttons can be placed outside the form borders). If the number of buttons allows is high then you should try to use a FlowLayoutPanel to have them automatically arranged according to the FlowLayout settings
you have to add the button to the controls of your form, and create an event handler
> Private Sub txtNum_TextChanged(sender As Object, e As EventArgs)
> Handles txtNum.TextChanged
> For i As Integer = 1 To CInt(txtNum)
> Dim strName as String = "btn" & i.ToString()
> Dim btn as New button
> With btn
> .Size = (100,20)
> .Location = (20,20)
> End With
> AddHandler btn.Clicked, AddressOf btn_Clicked
> MyForm.Controls.Add(btn )
> Next
> End Sub

Add a Link Button on Each Click Event in VB

I have a drop down list with a bunch of product names in it with an add button on the side. Everytime I press the add button, I want a Linkbutton to appear with the name of the product. I did the pnl.Controls.Add(New LinkButton), but it only adds one and then goes away next time I go to click. Here is the code I have right now that is making a LinkButton appear at click, just can't figure out how to alter the logic to add a new one each time.
Private Sub btnAddLinkedProjects_Click(sender As Object, e As EventArgs) Handles btnAddLinkedProjects.Click
lbLinkedProject.Visible = True
lbLinkedProject.Text = ddlParentProject.SelectedItem.Text
End Sub
I have a linkbutton in my designer that is set to invisible until it's clicked. Ultimately, when the link button of the project is clicked on, it fills in all of the fields like this
Private Sub lbLinkedProject_Click(sender As Object, e As EventArgs) Handles lbLinkedProject.Click
Dim intParentRecID As Integer
Dim pid As Project = Nothing
Dim intCityState As Integer = 0
Dim strState As String = ""
Dim cs As nsCityState = Nothing
intParentRecID = Integer.Parse(ddlParentProject.SelectedValue)
pid = oDesignCon.getProjectByRecID(intParentRecID)
If pid Is Nothing Then
Else
intCityState = pid.CityState
cs = New nsCityState(intCityState)
If cs Is Nothing Then
Else
strState = cs.StateShort
Me.ddlAddState.SelectedValue = strState
Call HandleAddStateChanged()
End If
Call nsLinqFormBinder.LoadContainer(Me.pnlCreateNewPID, pid)
Me.ddlAddAssignTo.SelectedIndex = 0
End If
End Sub
What's wrong if you just create new LinkButton as usual :
Private Sub btnAddLinkedProjects_Click(sender As Object, e As EventArgs) Handles btnAddLinkedProjects.Click
'create & prepare new LinkButton'
Dim newLinkedProject As New LinkButton
newLinkedProject.Visible = True
newLinkedProject.Text = ddlParentProject.SelectedItem.Text
'register event handler'
AddHandler newLinkedProject.Click, AddressOf Me.lbLinkedProject_Click
'add the LinkButton to panel'
pnl.Controls.Add(newLinkedProject)
End Sub

Multi-select if Checked

I am not able to add Checkbox in column header to multi-select(select all) Rows of Datagridview, I google it but they are giving me to add checkbox in row not in column header.
So that I'll select all check box column header click.
Please look at the image belowfor example. it is the listview image that i got form internet But I'm using Datagridview.
I got this from internet but I can't remember where is the link and originally comes from c#.
Private checkboxHeader231 As CheckBox
Private Sub show_chkBox()
Dim rect As Rectangle = DataGridView1.GetCellDisplayRectangle(columnIndexOfCheckBox, -1, True)
' set checkbox header to center of header cell. +1 pixel to position
rect.Y = 3
rect.X = rect.Location.X + 8 + (rect.Width / 4)
checkboxHeader231 = New CheckBox()
With checkboxHeader231
.BackColor = Color.Transparent
End With
checkboxHeader231.Name = "checkboxHeader1"
checkboxHeader231.Size = New Size(18, 18)
checkboxHeader231.Location = rect.Location
AddHandler checkboxHeader231.CheckedChanged, AddressOf checkboxHeader231_CheckedChanged
DataGridView1.Controls.Add(checkboxHeader231)
End Sub
Private Sub checkboxHeader231_CheckedChanged(sender As System.Object, e As System.EventArgs)
Dim headerBox As CheckBox = DirectCast(DataGridView1.Controls.Find("checkboxHeader1", True)(0), CheckBox)
For Each row As DataGridViewRow In DataGridView1.Rows
row.Cells(columnIndexOfCheckBox).Value = headerBox.Checked
Next
End Sub
Usage:
Sub Form1Load(sender As Object, e As EventArgs) Handles MyBase.Load
show_chkBox()
End Sub
I hope it will helps

vb.net deleting lots of dynamically created buttons

I'm a new programmer to vb.net, so apologise for what is likely to be ignorance.
I’m building a simple gui for a database interface, with many parent and child items within it. Upon a form I create buttons depending on how many items (parents/children). I've got the creation of the buttons thus:
For RowNumber As Integer = 0 To NoOfRows
Dim Buttoni As New Button
Buttoni.Location = New Point(LocationX, LocationY)
Buttoni.Width = 100
Buttoni.Height = 40
Buttoni.Visible = True
Buttoni.Text = DatasetA.Tables(0).Rows(RowNumber).Item("Name")
ButtonName = "Button" + RowNumber.ToString
If LocationX < FormWidth - (SpacePerButtonX * 2) Then
LocationX = LocationX + SpacePerButtonX
Else
LocationX = 50
LocationY = LocationY + SpacePerButtonY
End If
AddHandler Buttoni.Click, AddressOf DynamicButtonClick
Me.Controls.Add(Buttoni)
Buttoni.BringToFront() 'brings newest buttons to front!
Next
But I’m struggling with a way to delete the buttons to make way for a new set to replace them... I can delete a single one upon its click, but I’d like to delete all of the buttons that have been created in this way before re-creating them.
I hope that makes sense and there is a fairly simple way to accomplish this..?
I will add, in your creation loop, some value to the Tag property.
This will help to differentiate the buttons created dinamically from the buttons created statically in your form.
Buttoni.Tag = 1
Then, to delete a button, loop in reverse order on the Me.Controls collection,
check if you get a button and if the Tag property IsNot Nothing
For x as Integer = Me.Controls.Count - 1 to 0 step -1)
Dim b as Button = TryCast(Me.Controls(x), Button)
If b IsNot Nothing AndAlso b.Tag IsNot Nothing then
b.Dispose() '' NOTE: disposing the button also removes it
End If
Next
It's hard to know exactly what you want to do. I guess you could just use the same technique in reverse, something like
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
Dim ctrl = Me.Controls(i)
If TypeOf (ctrl) Is Button Then
ctrl.Dispose() '' NOTE: disposing the control also removes it
End If
Next
Create a button and delete it with double click!
Easy Code :
Dim b As New Button
Private btn As Button ' this is a reference object
Private ptX, ptY As Integer
Private drag As Boolean
Private Sub nodebtn_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Clicks = 2 Then
b.Dispose()
End If
If e.Button = MouseButtons.Left Then
drag = True
btn = CType(sender, Button)
ptX = e.X : ptY = e.Y
End If
End Sub
Private Sub nodebtn_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If drag Then
btn.Location = New Point(btn.Location.X + e.X - ptX, btn.Location.Y + e.Y - ptY)
Me.Refresh()
End If
End Sub
Private Sub nodebtn_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
drag = False
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
b.Location = New Point(10, 10)
b.Size = New Size(110, 29)
b.BringToFront()
b.Text = "Button"
AddHandler b.MouseDown, AddressOf nodebtn_MouseDown
AddHandler b.MouseMove, AddressOf nodebtn_MouseMove
AddHandler b.MouseUp, AddressOf nodebtn_MouseUp
Me.Controls.Add(b)
End Sub