How do you set a dynamically created panel to false in VB.net? - vb.net

I have on my screen design Panel1(left half), and panel2 through 10(right half), the panels on the right half are named based on data from a database.
I need to be able to click on a button in panel1 and when I do so, I need to set visibility to false for the current panel on the right half and set visibility to true that is referenced from the button click. I know that I can do the following but I think this is just way too much overhead and there has to be a better solution than this:
For Each control In Me.Controls.OfType(Of Panel)
If control.visible = true Then
control.visible = false
exit
Next
Panel that the visibility that needs to be set to false was dynamically created so it can not just be accessed by just name, otherwise that would solve my issue easily.
I seen this bit of code elsewhere and it would make my life easier, however not sure how to implement it dynamically when the panels are created, since the name of the panels are unknown at creation.
This bit of code to be able to reference the panels directly.
Dim machinePanel As Panel = DirectCast(Me.Controls.Item("pnl" & strMTB & strMachineID), Panel)

I'm not sure what you mean but "is referenced from the button click", so I'll assume that the text of the button refers to the panel name.
Create a method that handles the visibility
Private Sub SetPanelVisibility(button As Button)
'panel that the button is in
Dim leftPanel = CType(button.Parent, Panel)
'get right panels and ignore left
Dim rightpanels = Me.Controls.OfType(Of Panel).Where(Function(x) x.Name <> leftPanel.Name)
'set visibility of each panel.
For Each panel In rightpanels
panel.Visible = panel.Name = button.Text
Next
End Sub
To call the method just pass the button in on the click event. e.g.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SetPanelVisibility(sender)
End Sub

Related

Refer to a determinate created button

So In my program I’ve putted a flowlayourpanel and with a button I add to the layout infinite new buttons
(button.name = “button” + i.tostring) [i = i + 1]
With another button I want to hide the button with the i=3 so button3.hide(), but it doesn’t work beacause it doesn’t exist yet so How can I refer to the button created when the i was 3?
Name is a property of the Control class so every control has that property. That doesn't mean that it needs to be set though, so some controls don't have a name. When you add a control to your form in the designer, what happens is that the Name property is set AND a field is declared with that name. That's why, if you name a Button control Button1, you are able to use the field Button1 to refer to it.
When you create controls at run time, of course there is no field dedicated to that control so of course you can use such a dedicated field to refer to it. It's up to you to get a reference to that control from wherever you put it when you created it. Where that is is up to you but, if you added it to the Controls collection of a container, that is one option for getting it back. If you set the Name property, you can index the Controls collection with that name to get it, e.g.
Dim myButton = DirectCast(myFlowLayoutPanel.Controls("Button" & i), Button)
There may be other ways of getting that reference too. For instance, if you created your own List(Of Button) and set the Tag of each Button to the number, you could do this:
Dim myButton = myButtonList.Single(Function(btn) CInt(btn.Tag) = i)
When adding your buttons to the FlowLayoutPanel you have correctly assinged a name. You can use this name to refer to the button as shown in Button2.Click.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To 5
Dim b As New Button
b.Name = "btn" & i
b.Text = "btn" & i
FlowLayoutPanel1.Controls.Add(b)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FlowLayoutPanel1.Controls("btn3").Visible = False
End Sub

How to create any one dynamic controls with a for loop without using location property and the form should grow automatically

How to create multiple button controls with a for loop without getting the controls overlapped and without using location property in Vb.Net.
I have created 'n' number of vb controls dynamically but the created controls are getting overlapped to each other. When I use location property to each controls all the controls are getting displayed as per the location value.
The real problem is, I'm using a panel of width 300 and height 300, under that I need to display the dynamically created controls. I have figured it out which is tedious work and does take a lot of time. My idea is to find the panel width and height then need to check whether the new control which is getting created has ample of space to fit inside the panel.
I need to know few things,
1) How to display the controls dynamically using for loop without getting overlapped over each other and without using location property.
2) I need the container or the panel to grow as per the number of controls which gets created dynamically.
3) Accessing each controls which got displayed using an ID or educate or explain me any better idea.
I created a new WinForms project and added a Button to the top of the form. I added a FlowLayoutPanel under that and made it narrow enough to fit a single Button widthwise. I set the AutoSize property of the FLP to True and the FlowDirection to TopDown. I then added this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Create the new Button.
Dim btn As New Button
'Add it to the FLP
FlowLayoutPanel1.Controls.Add(btn)
'Get the position of the bottom, left of the Button relative to the form.
Dim pt = PointToClient(btn.PointToScreen(New Point(0, btn.Height)))
'Resize the form to provide clearance below the new Button.
ClientSize = New Size(ClientSize.Width, pt.Y + 10)
End Sub
I then ran the project and started clicking the Button I added. As expected, each click added a new Button to the FLP in a vertical column and the form resized to fit. In order to access such controls in code, you can simply index the Controls collection of the FLP.
try this helps you.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
'Do Something
Else
'Do Something else
End If
Panel1.Controls.Clear()
For i As Integer = 0 To 10 Step 1
Dim b15 As New Button
b15.Text = "Test3"
b15.ID = "a" & i
AddHandler b15.Click, AddressOf updateFunc
Panel1.Controls.Add(b15)
Next
End Sub

Visual Basic Dynamically Create and Erase Controls

I use Visual Basic in Visual Studio 2015 and i am trying when i click on a StripMenu to appear me some TextBoxes and Buttons.
After another click in Stripmenu i want to erase them and add new one.
My problem is in Erase (delete or clear my buttons and textboxes) controls from my surface.
I try to do it use Button.Visible =True (or False) but it's not seems to be really helpful in a big amount of controls.
Private Sub ClassAToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClassAToolStripMenuItem.Click
Label1.Text = "Sum A class Students: "
Dim btnA As Button = New Button
btnA.Location = New Point(420, 180)
btnA.Name = "Btn1"
btnA.Text = "OK"
btnA.Visible = True
Me.Controls.Add(btnA)
AddHandler btnA.Click, AddressOf button
End Sub
Private Sub button()
'What my Button does.
End Sub
I create dynamically through this code my Button but if i want to go in another Menu option i want to erase this button to add again my new controls (such us new buttons labels etc).
Your declaration is out of scope since you declared it in the menu's click method. You would have to use the Find method to get back the reference to the control you created:
Dim btn = Me.Controls.Find("Btn1", True).FirstOrDefault()
If btn IsNot Nothing Then
btn.Dispose()
End If
If you are trying to replace the contents of a panel with a new "screen" on your menu click, you can try code like this:
While Panel1.Controls.Count > 0
Panel1.Controls(0).Dispose()
End While
Dim newControl As New UserControl1
newControl.Dock = DockStyle.Fill
Panel1.Controls.Add(newControl)

How to make a Search Bar that looks for buttons

I want to make a Flow Layout Panel and fill it with a bunch of buttons. I want to have a Text box on the top of the form to search for a specific button. No clue on how. When the user searches, all other buttons will not be visible, only the button that its .text property matches will be visible.
Assuming search box is looking for the button's .Text property, that the textbox is named txtSearch, and the panel is named pnlButtons:
Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
pnlButtons.SuspendLayout()
For Each c As Control In pnlButtons.Controls
c.Visible = False
Next
Dim matchingButtons = pnlButtons.Controls.OfType(Of Button).Where(Function(b) b.Text.StartsWith(txtSearch.Text))
For Each b As Button In matchingButtons
b.Visible = True
Next
pnlButtons.ResumeLayout()
End Sub

Open Tabs Control

I'm using MDI container to run my bussiness application I created for my client.
Since using MDI means that when I open several forms they will still run in background all the time untill I close them manualy.
What I need is to make User Control or anything else that could preview all opened forms in Tab Form so my client can easily close all or some of opened forms without closing a form he is curently viewing.
For now I have used this code, so for now only first clicked item from menu appears as button, but not others clicked menu items.
Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
Dim Button As New Button
Me.Panel5.Controls.Add(Button)
Button.Text = e.ClickedItem.Name
Button.Width = 50
Button.Height = 25
End Sub
Now I need to write a code to add more buttons bellow, also should add a code for adding buttons only when I click on SubMenu item (The one when is clicked new Form appear).
And also, I should now add a little Close button into previewed User-Button-Control.
From your comments, I understand that your ideas regarding adding buttons at runtime are not too clear and thus I am including a small code which hopefully will help you on this front. Start a new project and put a Panel (Panel5) and a Button (AddButtons) on it, and write this code:
Dim lastButtonIndex, lastLeft, lastTop As Integer
Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
Dim curButton As Button = DirectCast(sender, Button)
If (curButton.Name = "Button1") Then
'do Button1 stuff
End If
'etc.
End Sub
Private Sub addNewButton()
lastButtonIndex = lastButtonIndex + 1
lastLeft = lastLeft + 5
lastTop = lastTop + 5
Dim Button As New Button
With Button
.Name = "Button" + lastButtonIndex.ToString()
.Text = "Button" + lastButtonIndex.ToString()
.Width = 50
.Height = 25
.Left = lastLeft
.Top = lastTop
AddHandler .Click, AddressOf Button_Click
End With
Me.Panel5.Controls.Add(Button)
End Sub
Private Sub ButtonAddButtons_Click(sender As System.Object, e As System.EventArgs) Handles AddButtons.Click
addNewButton()
End Sub
This code will add a new button to the panel every time you click on AddButtons. All the buttons will have an associated Click Event (the same one for all of them): Button_Click. The way to know which button is the current one inside this method is via sender, as shown in the code (you can put as many conditions as buttons. The names are given sequentially starting from 1; but you can take any other property as reference, curButton is the given Button Control).
Bear in mind that one of the problems you have to take care of is the location of the buttons. The code above has a very simplistic X/Y values (Left/Top properties) auto-increase which, logically, will not deliver what you want.