Getting text from dynamically created radio buttons in a Table - vb.net

In a recent VB project, I was asked to create a Quiz template in Windows Forms. The concept is to read from a txt file how many radio buttons and questions the user has to answer. In order to achieve that, I created and stored dynamically each radio button in Table1.Controls, Table1 is my table. When I try to handle the "enter" event, it does not return in the sender text, or even type. It still says that the sender is Table1. How to know what item on that Table triggered the Event? Thanks. Here is the code of creating the radios:
For i As Integer = 1 To (bullets_num)
Dim rdo As New RadioButton
rdo.Name = "RadioButton" & i
If ansArr.Length < bullets_num Then
rdo.Text = i
Else
rdo.Text = ansArr(i)
End If
rdo.Location = New Point(5, 30 * i)
Panel1.Controls.Add(rdo)
Next

Related

Giving buttons different names through code and referring to them later on

My code creates a 5x5 grid of buttons. I am wanting to give each of these buttons different names "BtnColour1", "BtnColour2", etc. How do I give them all different names and how do I refer to each button later in the program?
Dim bytCounter As Byte
For bytCounter = 1 To 25
Dim btnColour As New Button
Me.Controls.Add(btnColour)
btnColour.Height = 50
btnColour.Width = 50
btnColour.Name = "btnColour" & bytCounter
btnColour.Enabled = False
btnColour.Left = ((bytCounter - 1) Mod 5) * 51
btnColour.Top = ((bytCounter - 1) \ 5) * 51
AddHandler btnColour.Click, AddressOf BtnClick
Your code (I guess you forgot the ending Next) does create 25 Buttons, with names btnColour1... btnColour25.
In the BtnClick event, to get the name of the clicked button, you should write something like:
Private Sub BtnClick(sender As Object, e As EventArgs)
Dim buttonName as string=CType(sender, Button).Name
'buttonName now has the clicked button name
End Sub
Of course, since you set the enabled property to False, your button click event will not fire.
In a general sense (and in addition to Spyros' answer, which is a good way to do it in an event handler - the sender is always the thing that raised the event), when you give a control a name and add it to a control's Controls collection, you can then retrieve it by that name later:
'Here you added the button to the form controls:
Me.Controls.Add(btnColour)
'later in the code you can ask for it back by name, for example:
Dim controls = Me.Controls.Find("btnColour1")
What you get back is an array of Controls. You get an array because Find can search all children (panels inside panels inside groupboxes inside forms etc) and it is thus conceivable that multiple controls in different panels will both have the same name. In your case if you know you only have one control called "btnColour1" it's safe to get it by array index:
Dim control = controls(0) 'controls variable is from the above Find
Lastly, remember that it comes back as a Control, the parent class for all controls. Because you know it's a button, it's safe to cast without check:
Dim button = DirectCast(control, Button)
Remember that if your property is available on the base Control class you don't even need a cast:
'here's a 1 line way to get the text of the button named btnColour1
'Find all controls named btnColour1, take the first, get the text
Dim t = Me.Controls.Find("btnColour1")(0).Text
If you want to refer to the buttons later in the program to change a setting without clicking the button, you can add each button to an array and call each of them with an index number:
Dim buttons(24) As Button
Then as the buttons are created, you can add each button to the array:
Dim bytCounter As Byte
For bytCounter = 1 To 25
Dim btnColour As New Button
Me.Controls.Add(btnColour)
buttons(bytCounter) = btnColour
you can then reference each button and their properties using the index number of the button in the array. You may also want to add a specific tag to each button to make each button more unique using:
btnColour.Tag = bytCounter

Reversi VB.net logic behind it

I am trying to make the reversi game in VB.Net. I have some difficulties translating the game`s logic into vb.net
If a button is black and the button next to it is white,than the button next to the white one will be black wen pressed.
newButton.tag = colum of button + (row of button * amount of columns)
-> I made 64 buttons via a function loop and added a tag
Dim knop As Button = sender
Dim value As String = knop.Tag
If value = "...(?)" Then
knop.BackColor = Color.Black
If ....(?)
End If
End If
I already made a scheme with the label of the buttons, but I find it hard to implement the logic. Can someone help me out with thid one?
EDIT: http://i.stack.imgur.com/3gdrJ.png
If you use Dim ButtonList As List(Of List(Of Button)) and add the buttons to the form in runtime you can add each the button for each row to a list then add that list to ButtonList. Now you can access each button by the indexes in the 2 dimensional list.
Since you're changing the backcolor just use that instead of using the tag.

Programatically creating buttons on an applications FIRST Run and Save it for future runs

I am developing an application (VB.NET and Visual Studio 2010 Pro) and require assistance on the following:
1) Items / products are stored in a database, these items are stored under specific groups.
2) Upon the loading of my form, I need to get a list of all the groups in the items table and create a button for each group item, in addition to this it will have to add a tab for each group to the adjacent Tab Control. I've been able to do this however not using buttons, rather I'm using a list view. My perfect solution would be able to do this using buttons.
3) After creating the buttons for the tabs and their respective tabs I need to further populate each tab with the items that are listed under the respective groups, here too the items need to be displayed as buttons in the ideal situation but I will use a list view if I have to.
4) I would like to make it that once the buttons for the groups and items are created it must be saved as part of the interface if not permanently just for the session for which the application is running. (i.e t must not have to be re created every time the tab page is loaded it should just be created once for each time the application is run.)
here is the code I have for getting the groups from the DB and populating the list view along with the code for generating the tabs and adding a list view to the tab:
Private Sub frmItemSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstItemGroups.Clear()
tbItemGroups.TabPages.Clear()
If openConnection(SQLConnection6) Then
Dim cmd2 As String = "select distinct itemgroup " & vbCrLf & _
"from stkitem" & vbCrLf & _
"where ItemGroup != ''" & vbCrLf & _
"order by ItemGroup asc"
SQLCommand5 = New SqlClient.SqlCommand(cmd2, SQLConnection6)
SQLReader6 = SQLCommand5.ExecuteReader
While SQLReader6.Read
lstItemGroups.Items.Add(SQLReader6.Item(0))
End While
End If
Dim x, i, index As Integer
x = lstItemGroups.Items.Count
i = 0
index = 0
Dim NewTab As New TabPage()
'MessageBox.Show(x)
While i < x
NewTab.Text = lstItemGroups.Items(i).Text
tbItemGroups.TabPages.Insert(index, NewTab)
i += 1
index += 1
End While
End Sub
Private Sub lstItemGroups_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstItemGroups.Click
Dim lstMenuItems As New ListView()
lstMenuItems.BackColor = Color.Blue
lstMenuItems.Dock = DockStyle.Fill
lstMenuItems.Items.Add("A")
lstMenuItems.Items.Add("A")
lstMenuItems.Items.Add("A")
lstMenuItems.Items.Add("A")
lstMenuItems.Items.Add("A")
Dim index As Integer = lstItemGroups.SelectedIndices(0)
tbItemGroups.SelectedIndex = index
tbItemGroups.SelectedTab.Controls.Add(lstMenuItems)
End Sub
The problem here is that when I click on the group, the respective tab is selected but the list view is not added to the tab. If I can get this to happen, populating it with the items from that group would be achievable as it will be the same as populating the groups in the groups list view. (The items added to the list view in my code are just for test purposes.)
SO In summary,
The main issue is how do I add the list view to the dynamically generated tab?
If you can assist with a solution to using buttons instead of a list view that would be awesome and I would greatly appreciate that.
Thanks in advance,
You are adding the Tabs wrongly what provokes the whole TabControl to be faulty and, consequently, no Controls can be added to it. The right way to do this is:
While i < x
Dim NewTab As New TabPage() 'This has to be called as many times as tabs to include
NewTab.Text = lstItemGroups.Items(i).Text
tbItemGroups.TabPages.Insert(index, NewTab)
i += 1
index += 1
End While
After this correction, the code in the lstItemGroups_Click will work fine. Nonetheless, bear in mind that you are adding the controls without specifying size (Width/Height) and location (Left/Top) within the given tab; you should account for those issues.

vb.net - click on listview subitem and open window

I'm not sure if this is possible after doing a bunch of googling, but hopefully it is. I have an application that pulls a list of information from a MySQL database and populates a listview (Unfortunately, I can't change to a datagrid at this time.) What I'm tasked to do is make it so that when clicking on a certain column, a window will open and, based on the ID of the row that was clicked on, retrieve another set of results from the same database.
The list view is created as such:
Do While result.Read()
Dim siteid = (result.Item("idsite").ToString())
Dim sitename = (result.Item("name").ToString())
Dim last_import_date = (result.Item("import_finished").ToString())
Dim last_import_file = (result.Item("file_name").ToString())
Dim last_line = (result.Item("last_line").ToString())
Dim status = (result.Item("status").ToString())
Dim lv As ListViewItem = ListView1.Items.Add(siteid)
lv.SubItems.Add(sitename)
lv.SubItems.Add(last_import_date)
lv.SubItems.Add(last_import_file)
lv.SubItems.Add(last_line)
lv.SubItems.Add(status)
Loop
So preferably I'd like to click on "Last_import_file" and have that open the window. I've tried a bunch of ItemClicked type commands, but haven't had much luck.
Is what I'm attempting possible? I don't need any special text formatting, just want to register the click and pop open the dialog.
Thanks!
Yes it is possible. To do this in a Listview it is a bit more complicated than a DataGridView. You'll need to make use of the ListViewHitTestInfo class. Using the MouseDown Event of your listview, use this code:
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
Dim info As ListViewHitTestInfo = ListView1.HitTest(e.X, e.Y)
MsgBox(info.Location.ToString())
If Not IsNothing(info.SubItem) Then
'info will contain the information of the clicked listview column. You can then go through it's subitems for more information, if any.
End If
End Sub

Adding buttons at runtime

I want to place a few buttons on my form. The number is unknown at design time. Actually each button will represent an item entered in combo box. So if user adds an item, a button on the form should be added by the code.
Please advise how to do it?
Thanks
Furqan
You can do this by simply looping over any number (in this case from a combo box) and creating the required number of buttons before adding them to the form.
For i As Integer = 0 To myComboBox.Items.Count - 1
Dim newButton = new Button()
// Add some properties, etc. to the button
newButton.Text = myComboBox.Items(i).ToString()
MyForm.Controls.Add(newButton)
Next
You can use a function like this:
Sub AddButton(ByVal label As String, ByVal location As Point)
Dim b As Button
b = New Button
b.Location = location
b.Text = label
Me.Controls.Add(b)
End Sub