how to add a form in a panel inside tab page ?
i use this code to do it, but when i resize my main form, the form inside tab page doesn't auto resize.
Public Sub showForm(ByVal frm As MetroForm)
Dim a As New Panel
For i As Integer = 0 To tabPage.TabCount - 1
If frm.Text = tabPage.TabPages(i).Text Then
tabPage.SelectedIndex = i
Exit Sub
End If
Next
a = New Panel
tabPage.TabPages.Add("")
tabPage.TabPages(tabPage.TabPages.Count - 1).Controls.Add(a)
a.Dock = DockStyle.Fill
frm.TopLevel = False
frm.MinimizeBox = False
frm.MaximizeBox = False
frm.DisplayHeader = False
frm.AutoSize = False
frm.Dock = DockStyle.Fill
tabPage.TabPages(tabPage.TabPages.Count - 1).Text = frm.Text
a.Visible = True
a.Name = "pnl" & frm.Name
a.Controls.Add(frm)
frm.Dock = DockStyle.Fill
frm.Visible = True
frm.BringToFront()
AddHandler a.ControlRemoved, AddressOf Me.removePanel
tabPage.SelectedIndex = tabPage.TabPages.Count - 1
tabPage.TabPages(tabPage.TabPages.Count - 1).Name = frm.Text
End Sub
and to remove the tab page when form is closed :
Private Sub removePanel(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim a As Panel = DirectCast(sender, Panel)
For Each obj As TabPage In tabPage.TabPages
If obj.Name = sender.Parent.Name Then
obj.Controls(0).Dispose() 'remove form
tabPage.TabPages.Remove(obj) 'remove tab page
RemoveHandler a.ControlRemoved, AddressOf Me.removePanel
End If
Next
End Sub
here's the screenshot :
Related
How can I make the cell contentClickEvent of my datagridview to open another form and display the cell data on the textbox of that opened form?
Private Sub Formbackground()
Dim formBackground As Form = New Form()
Try
Using uu As AddSupplier = New AddSupplier()
formBackground.StartPosition = FormStartPosition.Manual
formBackground.FormBorderStyle = FormBorderStyle.None
formBackground.Opacity = 0.5R
formBackground.BackColor = Color.Black
formBackground.WindowState = FormWindowState.Maximized
formBackground.TopMost = True
formBackground.Location = Me.Location
formBackground.ShowInTaskbar = False
formBackground.Show()
uu.Owner = formBackground
uu.ShowDialog()
formBackground.Dispose()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
formBackground.Dispose()
End Try
End Sub
Dim key = 0
Private Sub suppliersDGV_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles suppliersDGV.CellContentClick
Dim row As DataGridViewRow = suppliersDGV.Rows(e.RowIndex)
Dim low As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
AddSupplier.txtSupplier.Text = row.Cells(0).Value.ToString
AddSupplier.txtPhone.Text = row.Cells(1).Value.ToString
AddSupplier.txtAddress.Text = row.Cells(3).Value.ToString
If AddSupplier.txtAddress.Text = "" Then
key = 0
Else
key = Convert.ToInt32(row.Cells(0).Value.ToString)
End If
Formbackground()
End Sub
I was expecting that when the Addsupplier Form to load with the cell data in the textboxes
I need to create some tabpages and flowlayoutpanels in each tab-pages dynamically in my form loaded. After that i wanna add some button in each flowlayoutpanel.
here is my code:
Public Class Form1
Dim tabcntrl As New TabControl
Dim flp(4) As FlowLayoutPanel
Dim btn As New Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tabcntrl.Dock = DockStyle.Fill
tabcntrl.Alignment = TabAlignment.Left
Me.Controls.Add(tabcntrl)
For i = 1 To 5
tabcntrl.TabPages.Add("TAB" & i - 1)
flp(i - 1).Dock = DockStyle.Fill
tabcntrl.TabPages(i - 1).Controls.Add(flp(i - 1))
Next
button_adding()
End Sub
Private Sub button_adding()
For i = 1 To 5
For j = 1 To i
btn.Text = j
btn.Size = New Size(75, 75)
btn.Visible = True
flp(i - 1).Controls.Add(btn)
Next
Next
End Sub
End Class
Before setting the Dock property of the FlowLayoutPanel, you need to instantiate a FlowLayoutPanel.
...
tabcntrl.TabPages.Add("TAB" & i - 1)
flp(i - 1) = New FlowLayoutPanel ' <== ADD THIS LINE
flp(i - 1).Dock = DockStyle.Fill
...
I have a dynamically created tabpage where I have placed dynamically created a button and label. I'm trying to figure out how to change the label.text with the button.click event. Everything works correctly until I add in the With/End With to change the label text in the button click subroutine. This is my first try at dynamically programming (hobby of mine, so trying to learn), but I do have a specific end goal for this application. Any thoughts on how to fix this problem?
Private Sub initialize_Button()
Dim button_Mybutton As New Button
With button_Mybutton
.Name = "button_My_Button"
.AutoSize = True
.Text = "Calculate"
.Visible = True
.Top = 200
.Left = 10
End With
AddHandler button_Mybutton.Click, AddressOf Me.button_Click
Me.Controls.Add(button_Mybutton)
End Sub
Protected Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("Button Clicked")
With label_Test
.text = "Test Confirmed"
End With
End Sub
Private Sub label_Test_Output()
Dim label_Test As New Label
With label_Test
.Name = "label_Test"
.Top = 200
.Left = 100
.AutoSize = True
.Text = "Label Test"
.Visible = True
End With
Me.Controls.Add(label_Test)
End Sub
Move the declaration of the controls outside the methods, to the class level. Then you can access them from anywhere in the class.
Private button_Mybutton As Button
Private label_Test As Label
Private Sub initialize_Button()
button_Mybutton = New Button()
With button_Mybutton
.Name = "button_My_Button"
.AutoSize = True
.Text = "Calculate"
.Visible = True
.Top = 200
.Left = 10
End With
AddHandler button_Mybutton.Click, AddressOf Me.button_Click
Me.Controls.Add(button_Mybutton)
End Sub
Protected Sub button_Click(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("Button Clicked")
If label_Test IsNot Nothing Then
With label_Test
.text = "Test Confirmed"
End With
End If
End Sub
Private Sub label_Test_Output()
label_Test = New Label()
With label_Test
.Name = "label_Test"
.Top = 200
.Left = 100
.AutoSize = True
.Text = "Label Test"
.Visible = True
End With
Me.Controls.Add(label_Test)
End Sub
You should also check if the label is created when accessing it, since there is a possibility that you can click the button before calling label_Test_Output. Added null check to handler.
created dynamic table layout and add some controls in to that table layout.my code
Protected Overrides Sub OnLoad(e As EventArgs)
MyBase.OnLoad(e)
dynamicTable.ColumnCount = 5
dynamicTable.RowCount = 1
For i = 1 To 5
Dim button= New button()
button.Text = i.ToString()
dynamicTable.SetColumn(button, i)
dynamicTable.Controls.Add(button)
Next
Next
End Sub
Now i added 5 buttons to the table layout.
Now i am going to click on the button. How can i know ,on which button i clicked ?
Try like this
Dim dynamicTable As New TableLayoutPanel
dynamicTable.ColumnCount = 5
dynamicTable.RowCount = 1
For i = 1 To 5
Dim button = New Button()
button.Name = "button" & i
AddHandler button.Click, AddressOf Click1
Button.Text = i.ToString()
dynamicTable.SetColumn(button, i)
dynamicTable.Controls.Add(button)
Next
Me.Controls.Add(dynamicTable)
Private Sub Click1(ByVal sender As System.Object, ByVal e As System.EventArgs)
If sender.Name = "button1" Then
MsgBox("Hi")
End If
End Sub
Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.
This is the code i have for that button:
Dim intXX As Integer = 0
Do Until intXX = intX
userAvatar(intXX).Enabled = False
intXX = intXX + 1
Loop
The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?
How i create the buttons are like this:
private sub createButton()
Dim personAvatar As New PictureBox
With personAvatar
.AutoSize = False '> ^
If intX = 7 Then
thePrviousAvatarImg = 0
End If
If intX <= 6 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
ElseIf intX >= 7 And intX <= 14 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
Else
Exit Sub
End If
.Name = "cmd" & nameOfPerson
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BackgroundImage = Image.FromFile(theAvatarDir)
.Tag = nameOfPerson
.BringToFront()
End With
AddHandler personAvatar.Click, AddressOf personAvatar_Click
Me.Controls.Add(personAvatar)
userAvatar(intX) = personAvatar
intX = intX + 1
End With
End Sub
Thanks for your time and help!
David
You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.
So before you create each picture box, declare an array to store them:
Dim MyPictureBoxes() as PictureBox
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.enabled = False
Next
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Public Class Form1
Dim MyPictureBoxes(4) As PictureBox
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim personAvatar As New PictureBox
Dim intX As Integer = 0
While intX < 5
personAvatar = New PictureBox
With personAvatar
.AutoSize = False
.Left = intX * 100
.Top = 100
.Name = "cmd" & intX
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BringToFront()
End With
Me.Controls.Add(personAvatar)
MyPictureBoxes(intX) = personAvatar
intX = intX + 1
End While
End Sub
Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.Enabled = False
Next
End Sub
End Class
Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.