Why don't my Labels show in VB.NET Form - vb.net

After reading this question, I wrote some code to create a label for each attribute of an xml element.
The problem is that when I run the project, my form only displays the first label. I've checked in the immediate window as well as debug window and all of the labels are loaded to the form, but none of them are displayed. Help?
Here's the code that runs when the form loads.
Dim doc As New XmlDocument()
doc.Load("xmlfile")
Dim ability As XmlNode = doc.GetElementsByTagName("ability").Item(0)
Dim numberofLabels = ability.Attributes.Count
ReDim labels(numberofLabels)
For counter As Integer = 0 To numberofLabels - 1
labels(counter) = New Label
labels(counter).Visible = True
labels(counter).Text = ability.Attributes.Item(counter).Name
labels(counter).Location = New System.Drawing.Point(10, 30 + counter * 10)
Me.Controls.Add(labels(counter))
Next

You should be using some layout manager, to help you with control positioning. Doing it manually is not worth the pain. Try using TableLayoutPanel or FlowLayoutPanel. Both can be docked or anchored to a parent control, so everything behaves very smoothly. Otherwise you are looking to write a lot of positioning/resizing code, and then maintaining it later.

Change the value of 10 in the original code line for a new point to a bigger value such as 40, so that new labels could appear separated visually:
labels(counter).Location = New System.Drawing.Point(10 + counter, 30 + counter * 40)

Related

System.Drawing.Pen - lines disappear when Labels are placed on Form

I want to draw TextBoxes/Labels on my form in code and connect them with lines - based on data that I have stored in a datatable ("treedata"). If I use the following code everything works fine:
For i = 0 To treedata.Rows.Count - 1
Dim tb As New TextBox
hor = treedata.Rows(i)(11)
vern = ver + 120 * treedata.Rows(i)(4)
tb.Text = "sometext"
tb.Location = New Point(hor, vern)
Form8.Controls.Add(tb)
posofmodif = treedata.Rows(i)(10)
vero = treedata.Rows(i)(6)
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Green)
Dim formGraphics As System.Drawing.Graphics
myPen.SetLineCap(LineCap.RoundAnchor, LineCap.ArrowAnchor, DashCap.Flat)
formGraphics = Form8.CreateGraphics()
formGraphics.DrawLine(myPen, Convert.ToSingle(posofmodif), Convert.ToSingle(vero), Convert.ToSingle(hor), Convert.ToSingle(vern))
myPen.Dispose()
formGraphics.Dispose()
Next
However I would like to use labels instead of TextBoxes because it makes no sense to use heavier TextBoxes in this case. But when I simply replace
Dim tb As New TextBox
by
Dim tb As New Label
the labels do appear on the Form as expected but the lines connecting them appear only for a moment and then turn invisible.
I first thought that the problem might be caused by labels being over or below the lines but even when I make sure that no line is crossing any label it happens.
Does anyone have an idea what I could do to avoid this?
This is your problem: Form8.CreateGraphics(). That method is volatile, as it creates a Graphics instance that does not survive the scope in which it's used.
You need to be using the Paint event for whatever control on which you intend to draw. The form, the label...whatever that is. The Paint event provides a Graphics object for you to use, and it gets called whenever the drawing needs to be refreshed.
Because the event fires frequently, you need to be mindful of what you do there. Heavy lifting in a Paint handler can slow an app down considerably.

Table layout panel inc#

I have to create dynamic table layout panel with some controls with auto sized rows and and fixed columns size.
My problem is that i want to show whole checkbox text .
Any help
My code is
Dim textBox2 As New CheckBox()
textBox2.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
textBox2.AutoSize = True
textBox2.Dock = DockStyle.Top
'' textBox2.Size = New Point(200, 90)
Dim lbl1 As New Label()
lbl1.Location = New Point(10, 10)
lbl1.Text = "Yoer.vb"
lbl1.AutoSize = True
lbl1.Location = New Point(120, 50)
lbl1.Dock = DockStyle.Top
'' dynamicTableLayoutPanel.Padding = New Padding(2, 17, 4, 5)
dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
dynamicTableLayoutPanel.Controls.Add(textBox2, 1, 0)
Me.dynamicTableLayoutPanel.SetColumnSpan(textBox2, 5)
If you mean you want the table to size to the controls within it, then:
dynamicTableLayoutPanel.AutoSize = True
I know this is old, but I stumbled across it and figured I'd throw my 2 cents in in case someone else comes along.
Note: I'm using Visual Studio 2015 with .NET 4.6. Functionality may differ between versions.
The problem is that the really long text is not word-wrapping to fit within the table or form. Instead, it is set to Dock = DockStyle.Top. This will cause it to make a single line that continues on and gets clipped, similar to a single-line textbox.
If you want it to automatically word wrap, you'll need to use Dock = DockStyle.Fill. Now, this doesn't completely resolve the problem if your row or table isn't large enough to display the text. Since all of the rows are set to AutoSize, it will only do the bare minimum to fit the control vertically. It doesn't care if text gets clipped off. The end result, using your example code against a 6-column, 10-row table, is this:
Since there isn't a word wrap property, you'll need to manually fit it. Now, to do this, you'll need to change the row to be Absolute instead of AutoSize. To figure out how big to make it, you can pretty much rely on PreferredSize. This reveals a much wider Width than the existing regular Width. From that, we can determine how many lines it would take if we wrap it.
This is what my code ended up looking like:
Dim h As Single = 0
Dim chk As New CheckBox()
chk.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
chk.AutoSize = True
chk.Dock = DockStyle.Fill
Dim lbl1 As New Label()
lbl1.Text = "Yoer.vb"
lbl1.AutoSize = True
lbl1.Dock = DockStyle.Top
dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
dynamicTableLayoutPanel.Controls.Add(chk, 1, 0)
dynamicTableLayoutPanel.SetColumnSpan(chk, 5)
' Find the preferred width, divide by actual, and round up.
' This will be how many lines it should take.
h = Math.Ceiling(chk.PreferredSize.Width / chk.Width)
' Multiply the number of lines by the current height.
h = (h * chk.PreferredSize.Height)
' Absolute size the parent row to match this new height.
dynamicTableLayoutPanel.RowStyles.Item(0) = New RowStyle(SizeType.Absolute, h)
The changes included delaring a height variable, renaming the CheckBox variable, setting its Dock to Fill, removing the Location from lbl1, and adding in size calculation. The output:
This isn't perfect since the height includes the checkbox itself, and the checkbox takes up padding, so there can be too much or too little height calculated. There are other calculations that may need to be considered. But, this is a starting point.

Adding multiple pictureboxes to a form programmatically in vb.net

So I have written a short section of code to add 6 pictureboxes to a form in random locations. It adds each picturebox to a collection, then loops through the collection and adds them to the form control. The bizarre issue is that the code only works when I step through it line by line in debug mode. If I just compile and run the code then only 1 picturebox is added to the form, but if I step through the code line by line then all 6 pictureboxes are successfully added to the form in random locations. Can anyone tell me why the hell this happening? It's driving me pretty nuts. Code below:
For i As Integer = 0 To 5
Dim pic As New PictureBox
Dim rnd As New Random
pic.Location = New Point(rnd.Next(200, 300), rnd.Next(200, 300))
pic.Size = New Size(5, 5)
pic.BackColor = Color.White
pic.Visible = True
pic.BringToFront()
_picCollection.Add(pic)
Next
For Each item As PictureBox In _picCollection
Controls.Add(item)
Next
ShowDialog()
Open to suggestions of how to do this better / in a way that actually works properly.
Had to declare RND object outside of loop. Thanks tinstaafl!

Generate a new panel containing same check-boxes for each node in treeview VB.NET (Images Attached)

I want to generate a new panel with a click on each node in a treeview. But this each newly generated panel will have same check-boxes. In addition these check boxes are linked to groupboxes with a checked condition.
I can do this for 4-5 nodes by adding a panel for each node. But I have around 90 such nodes.
Kindly refer following images,
there are 90 such nodes.
Is there any easier way such as loop etc. to achieve this? or hard way (add panel for each node and use show/hide) is the only way?
Help will be really appreciated.
Cheers,
You can use a loop for this.
This is a sample:
First, you need to define some global variables:
Dim Panels(100) As Panel
Dim CheckBox1(100) As CheckBox
Dim CheckBox2(100) As CheckBox
Dim CheckBox3(100) As CheckBox
Dim Label1(100) As Label
Then, you should initialize controls on Form_Load() event:
For i As Integer = 0 To 100
'Initialize Controls
Panels(i) = New Panel()
CheckBox1(i) = New CheckBox()
CheckBox2(i) = New CheckBox()
CheckBox3(i) = New CheckBox()
Label1(i) = New Label()
'Set properties
CheckBox1.Left = 100
CheckBox2.Left = 100
CheckBox3.Left = 100
CheckBox1.Top = 100
CheckBox2.Top = 200
CheckBox3.Top = 300
Label1.Left = 100
Label1.Top = 50
Label1.Text = "ID : " & NodeNames(i) 'You should replace NodeNames(i) with a variable that you are using for the name of nodes.
'Add Controls to panel
Panels(i).Controls.Add(CheckBox1(i))
Panels(i).Controls.Add(CheckBox2(i))
Panels(i).Controls.Add(CheckBox3(i))
Panels(i).Controls.Add(Label1(i))
'Set visiblity of panel to false
Panels(i).Visible = False
'Add panel to the form.
Me.Controls.Add(Panels(i))
Next
And when you need to show a panel, you should do this:
Panels(i).BringToFront()
Panels(i).Visible = True
But having many panels in RAM is not such a good idea. I suggest you to use just one panel and modify it by code for each node. Because if you have many panels and many controls inside it, your application may use a large amount of RAM.

Dynamic PictureBox in Visual Basic

I've created a form with a PictureBox on it and would like to dynamically create another PictureBox on the form while the program runs (to the left of the static one). I've written this code:
Dim temp As PictureBox
temp = New PictureBox
temp.Image = StaticPictureBox.Image
temp.Visible = True
temp.Top = StaticPictureBox.Top
temp.Width = StaticPictureBox.Width
temp.Height = StaticPictureBox.Height
temp.Left = StaticPictureBox.Left - 20
temp.BringToFront()
When I run this code I can detect that the temp PictureBox does get created. However, it is not rendered onto the form. It seems like it's there but is invisible.
Does anyone have an idea of what I'm doing wrong?
You need to add it to the form's control collection:
Me.Controls.Add(temp)
Why don't you just remove that code and place a picturebox next to the other one and set:
newpicturebox.visible = false
Then whenever you have the action completed you have it change:
newpicturebox.visible = true
I know this is old but... you got an error here:
temp.Left = StaticPictureBox.Left - 20
should be:
temp.Left = StaticPictureBox.right + 20
or:
temp.Left = StaticPictureBox.right
hope it helped.