How to display text on a programatically created button? - vb.net

I've created buttons programatically, and they have a text value but for some reason don't show it.
Dim NodeButton As New Control
NodeButton.Name = "Button" & NodeID
NodeButton.BackColor = Color.Red
NodeButton.Text = (NodeID)
Where NodeID is a variabe declared elsewhere in the program

First, as already said in the comments you should change Dim NodeButton As New Control to Dim NodeButton As New Button
In second place I believe that you should add the new control to your form using something like this:
Me.Controls.Add(NodeButton)

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

Is it possible to change the text of a label in another form without using .Show() or .ShowDialog

I have a problem on Vb.net
My question is if it is possible to change the text of a Label without using frm.Show () or .ShowDialog().
Example :
frmMain As frmMain = New frmMain
frmMain.lblText.Text = "Hello please help"
without using a .Show() since it is already shown in a panel :
Yes, it is possible.
What Plutonix said in the comment, I guess is use the label in another form which already exists instead of making a new one.
For example, add a label1 in NewForm1 while designing the form.
Then, in Form1, you can do this:
NewForm1.label1.text = "Your text here"
Otherwise, if you don't want to put a label already in the design time,
you can add it like this:
Dim NewLabel1 As New Label
With NewLabel1
'add properties here
.Text = "your text"
.Parent = Me 'set the parent of the label as new form
.Location = New Point(1, 1)
'other properties here
End With

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")

How to reference controls located on different Tabs (VB.NET)

I have an application written in VB.NET that reads data from a file and displays the data on the screen.
Depending on the data in the file, the program has a TabControl with up to 3 tabs and each tab in turn has a DataGridView for displaying data. For example I have a TabControl that has a tab called "Saturday" and a tab called "Sunday".
The problem I am having is that when I read data from a file, the program displays all the data on the Saturday's tab grid because I am not sure how to reference the Grid on the Sunday tab.
To add the DataGridView I am using the following code:
Grid = New DataGridView
Grid.Dock = DockStyle.Fill
Grid.Name = "Grid" & TabControl.SelectedIndex
Grid.Tag = "Grid" & TabControl.SelectedIndex
And this is how I am reading the data in:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
If reader.Name = "cell" Then
y = y + 1
Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
End If
What I almost want to do is something like (pseudocode):
SelectedTab.Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
However when I use the above code it complains:
'Grid' is not a member of 'String'
I hope you understand the issue. Let me know if you need clarification
Your code is a little unclear. However, it appears to me that the following line:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
is creating at least one problem. It looks like you are attempting to refer to a Tabpage control by the string representation of its name, but unless I missed something, what that line is actually doing is trying to make a tabpage control type("SelectedTab") refer to a string type. If that is the case, then you will want to try this instead:
If reader.GetAttribute("controltype") = "Tab" Then
TabControl1.SelectedTab = TabControl1.TabPages(reader.name)
End If
It is a little hard to tell from the code you have posted, but that might get you headed down the right path.
++++++++++++
UPDATE: It appears from your code that you are naming each DGV control by appending the index of the tab on which it is located to the string "grid." I am going to assume that you are using a class member variable named "SelectedTab" to represent the current tab selected in the control. I will assume that at the top of your class you have done something like this:
'Form-or-class scoped memebr variables:
Private SelectedTab As TabPage
Private SelectedGrid As DataGridView
You should be able to refer to the active grid control using something like this:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
' Set SelectedTab member variable to refer to the new selected tab page:
SelectedTab = TabControl1.SelectedTab
' Set the SelectedGrid to refer to the grid control hosted on the selected tab page:
SelectedGrid = TabControl1.SelectedTab.Controls("Grid" & TabControl1.SelectedIndex.ToString())
End Sub
From here, you should be able to use the member variable for SelectedGrid to refer to the grid present on which ever tab page is selected in your tab control.
It is challenging to address your concerns with only fragments of your code. If you have additional difficulties, please post more of your code, so we can better see what else is going on.
Hope that helps!
Okay, I would go about something like this. Maybe you can simply use a DataSet to load the XML data in one line (if they have been saved with DataSet.WriteXML before).
Dim ds As New DataSet
Dim p As TabPage
Dim gv As DataGridView
ds.ReadXml("F:\testdata.xml")
For i As Integer = TabControl1.TabPages.Count - 1 To 0 Step -1
TabControl1.TabPages.RemoveAt(i)
Next
For Each dt As DataTable In ds.Tables
p = New TabPage(dt.TableName)
gv = New DataGridView
' ... configure the gv here...
gv.AutoGenerateColumns = True
gv.Dock = DockStyle.Fill
' ...
gv.DataSource = dt
TabControl1.TabPages.Add(p)
p.Controls.Add(gv)
Next

Vb.net Add all Declared Buttons to my form

i want to add all declared buttons into my form
something like:
dim btn1,btn2,btn3 as new button
for each btn as button in declared.buttons
me.controls.add(btn)
next
You can add the buttons like this:
For i As Integer = 1 To 150
Dim btn As New Button()
btn.Name = "btn" + i.ToString()
Controls.Add(btn)
Next
Later, you can access the buttons like this:
Dim btn As Button = CType(Controls("btn1"), Button)
However, if you are needing to load that many controls dynamically, you may want to reconsider your design. It smells like a bad idea for most situations.
You can get all your buttons you've declared using something like this:
Dim buttons = From fi In Me.GetType().GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
Where fi.FieldType Is GetType(Button)
Select CType(fi.GetValue(Me), Button)
Off the top of my head.. Still working on it.. but You get the idea?