Programmatically generate panel with its element on a flowlayoutpanel in VB - vb.net

I have created a flowlayoutpanel that renders panels inside on it for navigation pruposes. Now, I am doing the add of elements programmatically do reduce slow of VB 2012 application on editing codes. But then, I have an unexpected result on adding a new panel on my flowlayoutpanel.
Here is my code on creating the new panel:
Dim AppPanel As New Panel
Dim AppTableLayoutPanel As New TableLayoutPanel
Dim RecordCountPanel As New Panel
Dim RecordCountLabel As New Label
Dim RecordNameLabel As New Label
Dim AddButton As New Button
AppPanel.Width = 259
AppPanel.Height = 43
AppPanel.Margin = New Padding(0, 0, 0, 5)
AppPanel.BackColor = SystemColors.InactiveBorder
RecordCountPanel.BackColor = Color.SteelBlue
RecordCountPanel.Margin = New Padding(0)
RecordCountPanel.Dock = DockStyle.Fill
RecordCountLabel.Anchor = System.Windows.Forms.AnchorStyles.Left
RecordCountLabel.Text = "245" '
RecordCountLabel.Width = 70
RecordCountLabel.Height = 42
RecordCountLabel.Padding = New Padding(0, 6, 0, 0)
RecordCountLabel.TextAlign = ContentAlignment.MiddleCenter
RecordCountLabel.Font = New Font("Microsoft Sans Serif", 12)
RecordCountLabel.ForeColor = Color.White
'RecordCountLabel.BackColor = Color.Orange
RecordCountLabel.Location.X.Equals(4)
RecordCountLabel.Location.Y.Equals(12)
AppTableLayoutPanel.Dock = DockStyle.Fill
AppTableLayoutPanel.ColumnCount = 3
AppTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 33.18F))
AppTableLayoutPanel.Controls.Add(RecordCountPanel, 0, 0)
AddButton.Dock = DockStyle.Fill
AddButton.Font = New Font("Microsoft Sans Serif", 14)
AddButton.Text = "+"
RecordNameLabel.Anchor = System.Windows.Forms.AnchorStyles.Left
RecordNameLabel.Text = "Request Item Logs"
RecordNameLabel.Width = 150
RecordNameLabel.Height = 42
RecordNameLabel.Padding = New Padding(0, 6, 0, 0)
RecordNameLabel.TextAlign = ContentAlignment.MiddleLeft
RecordNameLabel.Font = New Font("Microsoft Sans Serif", 10)
AppTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 66.82F))
AppTableLayoutPanel.Controls.Add(RecordNameLabel, 1, 0)
AppTableLayoutPanel.ColumnStyles.Add(New ColumnStyle(SizeType.Absolute, 53.0F))
AppTableLayoutPanel.Controls.Add(AddButton, 2, 0)
RecordCountPanel.Controls.Add(RecordCountLabel)
AppPanel.Controls.Add(AppTableLayoutPanel)
Main.FlowlayoutPanel.Controls.Add(AppPanel)
The text on the 2nd column is not showing
The button on the 3rd column shows unusual
In Addition:
If I remove AppTableLayoutPanel.Controls.Add(RecordCountPanel, 0, 0), the other elements will be rendered normally.

Related

In one line I need to change the font type and also underline the font

This is the code I tried but does not work . .
Dim lab As New Label
lab.Font = New System.Drawing.Font("Courier New", 8.25!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, 0)
Well, it does work. Did you add it to a form? Use Form.Controls.Add(Control)
You said "In one line", so...
Me.Controls.Add(New Label With {.Font = New System.Drawing.Font("Courier New", 8.25!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, 0), .Location = New Point(10, 10), .Text = "abcDEF"})
but here it is a bit more readable
Dim lab As New Label()
lab.Font = New System.Drawing.Font("Courier New", 8.25!, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, 0)
lab.Location = New Point(10, 10)
lab.Text = "abcDEF"
Me.Controls.Add(lab)

Access added (sub) control in VB .NET

With this code I dynamicly add a panel with two panels inside (a header and a data panel). Within the Data panel there is also a label that I like to access.
Now I like to access the label inside the data panel but can't reach it with:
test_label.text = "this a second test"
Here the dynamicaly added panels
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim newPanelMain As Panel = New Panel With {
.Location = New Point(200, 500),
.Name = "test",
.Size = New Size(500, 500)
}
Dim newPanelHeader As Panel = New Panel With {
.Name = newPanelMain.Name & "_header",
.BackColor = Color.Orange,
.Dock = DockStyle.Top,
.Height = 50
}
newPanelMain.Controls.Add(newPanelHeader)
Dim newPanelData As Panel = New Panel With {
.Name = newPanelMain.Name & "_data",
.Dock = DockStyle.Fill,
.BorderStyle = BorderStyle.FixedSingle
}
newPanelMain.Controls.Add(newPanelData)
Dim newPanelSize As Panel = New Panel With {
.Name = newPanelMain.Name & (("_size")),
.BackColor = Color.Red,
.Height = 20,
.Width = 20,
.Location = New Point(newPanelData.Width - 20, newPanelData.Height - 20)
}
newPanelData.Controls.Add(newPanelSize)
Dim newLabel As Label = New Label With {
.Text = "This is a test",
.Name = newPanelMain.Name & (("_label")),
.Location = New Point(0, 0),
.AutoSize = True
}
newPanelData.Controls.Add(newLabel)
Me.Controls.Add(newPanelMain)
End Sub
Declare newLabel as a member variable:
Private newLabel As Label
So you can use this code:
Me.newLabel = New Label With {
.Text = "This is a test",
.Name = newPanelMain.Name & (("_label")),
.Location = New Point(0, newPanelHeader.Height + 10),
.AutoSize = True
}
newPanelData.Controls.Add(Me.newLabel)
And then change its .Text property using:
Me.newLabel.Text = "this a second test"
In a different situation (e.g. if you have multiple Labels created at runtime) remember that you can use Control.ControlCollection class:
CType(CType(Me.Controls("test"), Panel).Controls("test_data"), Panel).Controls("test_label").Text = "this is a third test"

Swap positions between three Panels

I have three panel type objects (A, B, and C), which have been dynamically generated within another panel type control.
My question is, how can I exchange panel B to the position of panel A and panel A to the position of panel B? This will be triggered by a click on a ToolStripMenuItem.
What I had thought, was to go through the arrangement of panels to know who exists and from there to work them, is that correct?
For Each obj As Control In Panel1.Controls
MsgBox(obj.Name)
Next
This is the code that I use to move to right:
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub
This is the code I use to generate objects dynamically:
Private Sub TileNavItem5_ElementClick(sender As Object, e As NavElementEventArgs) Handles TileNavItem5.ElementClick
Dim pos As Int32 = cInt(TextBox38.Text)
Dim poslabel As Int16 = cInt(TextBox42.Text)
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & Val(TextBox37.Text)
}
AddHandler pb.Click, AddressOf myClickHandler_b
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Section " & CInt(TextBox37.Text),
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & CInt(TextBox37.Text)
}
pb.Location = New Point(0, 0)
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
For j = 1 To 4
Dim pbdoors As New Panel With
{
.Width = 114,
.Height = 98,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpUnit,
.Name = "Unit" & Val(TextBox37.Text) & j
}
AddHandler pbdoors.Click, AddressOf myClickHandler_doors
pbdoors.Location = New Point(posX, posY)
pb.Controls.Add(pbdoors)
posY = (posY - 100)
Next
Panel1.AutoScrollPosition = New Point(Panel1.HorizontalScroll.Maximum, Panel1.VerticalScroll.Maximum)
TextBox37.Text = CInt(TextBox37.Text) + 1
TextBox38.Text = Val(TextBox38.Text) + 120
End Sub
You just need to find the controls. Swapping is the easy part.
Finding controls is also easy if you use Control.Controls.Find(String, Boolean). But you must at least know the control's name.
The difficulty comes in here
' TextBox37.Text = CInt(TextBox37.Text) + 1 ' implicit conversion from int to string
TextBox37.Text = (CInt(TextBox37.Text) + 1).ToString()
where you must find the control by name and you have built some integer into the name. Can you keep track of how many times 1 is added to TextBox37.Text?
If you can, you can pass it into this function, and the swapping will be performed
Private Sub swap(index1 As Integer, index2 As Integer)
' build the panel names
Dim p1Name = $"Panel{index1}"
Dim p2Name = $"Panel{index2}"
' find the panels
Dim p1 = DirectCast(Panel1.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
If p1 Is Nothing Then Throw New ArgumentException("index1")
Dim p2 = DirectCast(Panel1.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
If p2 Is Nothing Then Throw New ArgumentException("index2")
' swap the panels
Dim temp = p2.Location
p2.Location = p1.Location
p1.Location = temp
End Sub
swap(1, 2) will swap panel 1 with 2. swap(4, 6) will swap the panel 4 with 6. This logic is not included in your question (i.e. how many times is TileNavItem5_ElementClick called?), so you know better how to incorporate it. I hope it works for you.

Add an extra control to a button in VB.Net

I have a code that creates 30 buttons at run time on a form which works excellently
For i = 0 To 30
btn = New Button
Dim old As Padding = btn.Margin
Dim old2 As Padding = btn.Padding
btn.Tag = "X1"
btn.Name = "Drane"
btn.Text = "The power of trying"
btn.Width = 95
btn.Height = 120
btn.Margin = New Padding(10, 10, 10, 10)
btn.Padding = New Padding(0, 4, 0, 2)
btn.TextAlign = ContentAlignment.BottomCenter
btn.ImageAlign = ContentAlignment.TopCenter
btn.Font = New Font("Lucida Sans Unicode", 11)
btn.ForeColor = Color.Black
btn.BackColor = Color.White
Next i
Now I'm trying to add an extra control to hold the ids for buttons that I created at run time
btn.id = i
which would hold the ids of each particular button so I can get the value in other functions in my application. I know I have to add this somehow as a control but I just don't know how.
Please any help will be greatly appreciated in resolving this
This will extend the Button class and add an ID property.
Public Class ButtonMod
Inherits Button
Public Property ID As Integer
End Class
Then you just create them in a loop as you were doing already:
For i As Integer = 1 To 30
Dim btn As New ButtonMod
Dim old As Padding = btn.Margin
Dim old2 As Padding = btn.Padding
btn.Tag = "X1"
btn.Name = "Drane"
btn.Text = "The power of trying"
btn.Width = 95
btn.Height = 120
btn.Margin = New Padding(10, 10, 10, 10)
btn.Padding = New Padding(0, 4, 0, 2)
btn.TextAlign = ContentAlignment.BottomCenter
btn.ImageAlign = ContentAlignment.TopCenter
btn.Font = New Font("Lucida Sans Unicode", 11)
btn.ForeColor = Color.Black
btn.BackColor = Color.White
btn.ID = i
Next

How do I put something in front of an image?

Using VB.net VS2010 and Winforms, I am simply trying to add both a picturebox and a label to a panel, but the label will not go in front of the picture box. How can I set the label as the foremost object? Using the code below, the label always ends up behind the picturebox.
Public Class MyTab
Inherits System.Windows.Forms.Panel
Public myText As New Label
Public tab_top_left as New Picturebox
Public Sub New(ByVal tab_object As tab_properties_object)
With tab_top_left
.BackgroundImage = My.Resources.tab_top_left
.Parent = Me
.Location = New Point(0, 0)
.SendToBack()
.Width = 20
.Height = 19
.Name = "tab_top_left"
End With
Dim TextString As String = tab_object.top_tab_text
myText.Font = CustomFont.GetInstance(Main.main_font_size_up3, FontStyle.Regular)
myText.ForeColor = Color.FromArgb(255, 0, 0, 0)
myText.BackColor = Color.FromArgb(255, 204, 204, 204)
myText.Text = TextString
myText.Location = New Point(0, 0)
myText.Width = 200
myText.Height = 40
myText.UseCompatibleTextRendering = True
myText.BorderStyle = BorderStyle.None
myText.Name = "tab_" & myText.Text
myText.Parent = Me
myText.BringToFront()
Me.Width = myText.Left + textSize.Width + 15
Me.Height = 40
Me.Name = "_" & TextString
Me.Location = New Point(0, 0)
End Sub
End Class