Giving buttons different names through code and referring to them later on - vb.net

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

Related

Is there anyway to save a multiple buttons as a list?

I am making small restaurant system project. I have 16 buttons as tables in the restaurant. I would like to change their colors or disable any of them when some events trigger (the form is loaded).
I save my button names in format TableX_ButtonY
I used a for-loop to change theirs border colors like this:
CType(Me.Controls.Find(String.Format("Table{0}Button{1}", i, x), True)(0), Button).FlatAppearance.BorderColor = Color.Blue
It will be great if I can save these buttons as a list, so I can manage it more easily.
I name their tags from 1 to 16 but I don't know how to use them correctly. Because the trigger not based on button click but rather based on the Load Form event.
Buttons are already in a collection and is a bit redundant to add them to a generic collection. In this example there are 2 buttons in a group controls collection, which could very well be any applicable container.
Dim ReservedTables() As Integer = {5, 10, 15, 20}
For Each Btn As Button In GroupBox1.Controls.OfType(Of Button)
If ReservedTables.Contains(CType(Btn.Tag, Integer)) Then
Btn.Enabled = False
End If
Next
Dim TableList As New List(Of Button)
TableList.Add(TableX_ButtonY)
For Each Table As Button in TableList
'do stuff
next
if you generated the buttons by using the designer, you can use the method you described in your question to add them all to the list

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.

Creating a new instance of an existing control? (a bit more complicated than it sounds)

I know that if I want to dynamically create a new instance of an existing form, I can simply do this...
Dim NewForm As New ExistingForm
... and a new instance of all of the controls from ExistingForm will be available in NewForm.
But what if I want to create a new instance of an existing control that's inside a separate existing form?
More specifically, let's say I have 2 forms - FormA and FormB. Inside FormB is an existing panel (we'll call it ExistingPanel). And inside ExistingPanel are several buttons, text boxes, etc., arranged exactly how I want them.
OK, so... inside a button's click event in FormA, I want to do something like this (it doesn't work, obviously, but should give you a good idea as to what I'm trying to accomplish)...
Dim NewExistingPanel As New FormB.ExistingPanel
And then I want NewExistingPanel to contain new instances all of the individual controls from ExistingPanel (so that I can dynamically manipulate each one individually).
So basically, my program will need to create several NewExistingPanels at runtime. And I will need to access each one (and all of its child controls) individually, after they have been created.
How can I accomplish this? I've been experimenting with this for several hours with no luck so far, so figured I'd break down and ask the real professionals :)
When you create a new user control, you could use a dictionary with the tag value as key and the control name as value. Then you can access the control directly by using the IndexOfKey method.
Dim NewForm2 As New Form2
Dim ControlIndex As New Dictionary(Of String, String)
For I = 0 To 2
Dim NewPanel As New WindowsControlLibrary2.MyCustomPanel
NewPanel.Name = "Panel-" + I.ToString
NewPanel.Location = New Point(0 + (NewPanel.Size.Width * I), 0)
NewPanel.Tag = "MyTagValue" + I.ToString
ControlIndex.Add(NewPanel.Tag, NewPanel.Name)
NewForm2.Controls.Add(NewPanel)
NewForm2.Update()
Next
NewForm2.Show()
Dim CurrentPanel As WindowsControlLibrary2.MyCustomPanel =NewForm2.Controls(NewForm2.Controls.IndexOfKey(ControlIndex("MyTagValue1")))
To add control such as panel with textboxes ang button in it ..
In your project - Add > Usercontrol -->
in this part you can make panel with textboxes and button as you want
name it as "myPanel" and save it as "myPanel.vb" --> or whatever you want
In your form and the button named button1 you can add to your button1_click event :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mypanel1 As New MyPanel
mypanel1.Location = New Point(0, 0)
Controls.Add(mypanel1)
mypanel1.TextBox1.Text = "Hi, Im new"
End Sub
And you will get your panel at runtime ...
And you will get the textbox1.text fill with "Hi, I'm new" ...
You could create a user control that lays out your sub-controls the way you want them, then create new instances of that. Add New > User Control in the solution menu, then build, and your controls should be ready to drag into the designer at the top of the Toolbox or to instantiate in code like any other control.
To expand on my comment:
Private somePanels As New Dictionary(Of String, YourUserControl)
Then you can just add them as follows:
Dim c As New YourUserControl()
…
Me.Controls.Add(c)
somePanels.Add("SomeTag", c) ' Instead of using Tag; if your tags are numbers, use a List instead of a Dictionary
And access them as:
Dim c = somePanels("SomeTag")

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