How to get value from dynamic radio button - vb.net

I'm creating the 3 radio buttons in a private sub like such:
For counter As Integer = 0 To rc - 1
'controller name Radio button and properties.
Dim dynRadio As New RadioButton()
Me.Controls.Add(dynRadio)
With dynRadio
.Name = CStr(ds.Tables("MakeThisNameMeaningful").Rows(counter).Item(0))
.Location = New Point(xAxis, yAxis)
.TabStop = False
.Text = CStr(ds.Tables("MakeThisNameMeaningful").Rows(counter).Item(0))
.Width = 80
End With
yAxis = yAxis + 40
Next
The radios get drawn ok. So I have 3 radios with the text (result of select from db) controller1, controller2, controller3
I've triead all sorts and couldnt' find anything on Google. Oh, I should mention that I'm trying to get the radio value from another private sub. I want to do along the lines of:
If controller1.Selected = true then
'do stuff
End if
I know the above is wrong but not sure how to determine which radio is selected :(
Cheers,
J

You can iterate through your radio buttons in your 2nd private sub.
Dim radios = Controls.OfType(Of RadioButton).AsQueryable()
For Each r As RadioButton In radios
If r.Checked Then
'this radio is checked. do something.
End If
Next

Related

VB Winforms use for loop to reference control of specific number

Ok so say i have 9 buttons named btn1 to btn9 i need to reference these to set there texts accordingly i tried square bracket notation like this
For i = 0 To 9 Step +1
For Each btn In TableLayoutPanel7.Controls.OfType(Of Button)()
btn[i].text = i
Next
Next
this doesnt work is there any way to use i in the control name to reference it so it will just name them 1 - 9 respectively? any help anyone could provide on where to start would be greatly appreciated
Or, as I suggested:
For i = 1 To 9
TableLayoutPanel7.Controls("btn" & i).Text = i.ToString()
Next
This will set the text:
Dim counter As Integer = 1
For Each btn In Me.Controls.OfType(Of Button)()
btn.Text = counter
counter += 1
Next
However, if you need to set specific values, then you need to test the name of each button:
For Each btn In Me.Controls.OfType(Of Button)()
Select Case btn.Name
Case "Button1"
btn.Text = 1
Case "Button2"
btn.Text = 2
'etc
End Select
Next

Changing double clicking activation on Combo box cell to single click?

I have a setup in my code where there is a datagridview. For each row I have a combo box cell that I have a separate combo box cell since I want a different selection of items for each cell.
Problem : The cell only drops down when the arrow is double clicked. How can I change the cell formatting, or possibly a cell click event, so that the cell response to just one click?
Here's my cell creation code. Frankly, I didn't start any other code since I didn't know what event to touch or call. Is there a property I can edit?
Code:
'add items to combobox list
Dim comboCell As New DataGridViewComboBoxCell
comboCell.FlatStyle = FlatStyle.Flat
Dim resolutionList As New List(Of cmbStruct)
Dim currentResIndex As Integer = 0
'create list of resolutions
For j As Integer = 0 To resolutions.Length - 1
Dim resClass As New cmbStruct
resClass.Name = resolutions(j)
resClass.ID = resolutions(j)
resolutionList.Add(resClass)
comboCell.Items.Add(resolutions(j))
Next
'set combocell values
comboCell.DisplayMember = "Name"
comboCell.ValueMember = "ID"
'set the default value to the current resolution index
Try
comboCell.Value = resolutions(currentResIndex)
Catch ex As Exception
End Try
comboCell.ValueType = GetType(cmbStruct)
comboCell.DataSource = resolutionList
editCameraTable("Resolution", i) = comboCell
Next
Change the EditMode property:
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
There seems to be a nearly identical question and a very good answer. It involves using the click_event. Here is the link:
How to manually drop down a DataGridViewComboBoxColumn?
In the link:
Private Sub cell_Click(ByVal sender As System.Object, ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1.BeginEdit(True)
If DataGridView1.Rows(e.RowIndex).Cells(ddl.Name).Selected = True Then
DirectCast(DataGridView1.EditingControl, DataGridViewComboBoxEditingControl).DroppedDown = True
End If
End Sub

Using loop for to shorten object name in VB.NET code

i have 5 textbox and i want to call its name with for loop , how to do this :
textbox1.backcolor = color.Lightblue
textbox2.backcolor = color.Lightblue
textbox3.backcolor = color.Lightblue
textbox4.backcolor = color.Lightblue
textbox5.backcolor = color.Lightblue
i want to know how to make the code shorter with loop for, so far my only clue is this code :
Public Sub ShortCode
For i = 1 to 5
textbox(i).backcolor = color.lightblue
Next
End Sub
any idea how to make this happen ?
Assuming you stick to your naming convention, you don't need to add the text boxes to an array. Use Control.Controls. This function finds controls directly inside some container i.e. Me.Controls searches only the form (Me), not inside containers such as panels and group boxes.
For i = 1 to 5
CType(Me.Controls("textbox" & i.ToString()), TextBox).BackColor = Color.LightBlue
Next
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls(v=vs.110).aspx
Similar (not identical) solution in one line with LINQ
Me.Controls.OfType(Of Control).
Where(Function(c As Control) c.Name.StartsWith("textbox")).
ToList().ForEach(Sub(c) c.BackColor = Color.Red)
Create an array of your TextBox controls and then iterate over them as you suggested. Below is untested code adapted from http://bytes.com/topic/visual-basic-net/answers/846341-array-textbox, but should get you there.
Dim textboxes As TextBox()
textboxes = New TextBox() {TextBox1, TextBox2, TextBox3};
For i = 1 to 3
textbox(i).backcolor = color.lightblue
if the answer above doesn't work for you try this
Public Sub ShortCode
dim i = 1
for i >= 5
textbox(i).backcolor = color.lightblue
i = i + 1
Next
End Sub
Dim TextBoxBag() As TextBox
TextBoxBag = {TextBox1,TextBox2,TextBox3,TextBox4,TextBox5}
For count = 0 to Ubound(TextBoxBag)
TextBoxBag(count).BackColor = Color.LightBlue
Next

VB 10 how to hundle multiple buttons

I have made a form in VB10 with 50 buttons. How can i manage their visibility with a for loop ??
For example i want to do something like that:
For i As Integer = 1 To 50
Button(i).Visible = False
Next
How can i map the current number of the i? I want to avoid write it 50 times.
Thank you in advance for your help.
Here's how to get the buttons no matter what container they are in, even multiple ones:
Dim matches() As Control
For i As Integer = 1 To 50
matches = Me.Controls.Find("Button" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Button Then
Dim btn As Button = DirectCast(matches(0), Button)
btn.Visible = False
End If
Next
If there names are Button1, Button2, etc then this will work:
For i As Integer = 1 To 50
Me.Controls("Button" & i.ToString).Visible = False
Next

Can't seem to disable button

I'm new to Visual Basic and I am stuck on a Tic Tac Toe exercise. Here is my code.
Public Class Form1
Public Enum Buttons As Byte
btn1 = 1
btn2 = 2
btn3 = 3
btn4 = 4
btn5 = 5
btn6 = 6
btn7 = 7
btn8 = 8
btn9 = 9
End Enum
Public Sub Computer()
Dim RandomNumberGenerator As New Random
Dim RandomNumber As Integer
RandomNumber = RandomNumberGenerator.Next(1, 9)
Dim RandomButton = CType(RandomNumber, Buttons)
Do
If RandomButton.Enabled = True Then
RandomButton.Enabled = False
RandomButton.Text = "O"
RandomButton.Font = New Font("Consolas", 50, FontStyle.Bold)
Exit Do
Else
RandomNumber = RandomNumberGenerator.Next(1, 9)
Dim RandomButton = CType(RandomNumber, Buttons)
End If
Loop
The problem that I am having is the if loop. I am trying to see if the Random button selected is enabled or not. But instead Visual Basic tells me that "Enable is not a member of Tic_Tac_Toe.Fourm1.Buttons. I wanted to know if there is any way I can disable or enable a button via an enum. Can someone please help me figure this out?
First problem: Your enum (Buttons) doesn't have a member "Enabled". You are setting properties of a Button on an enum, which won't work.
You have to create an actual UI Button for each "RandomButton" and set the value, ID, and text, then add it to the form. At this point you could get/set those properties, including "Enabled".
If these buttons are all on the Form already and you're just trying to pick a random one, you can use Me.Controls.Find(ID)