Items added to ControlCollection not showing - vb.net

i am trying to add some Checkboxes to a panel with this code:
For t = 0 To taglist.Count - 1 'list(of string)
'new ComboBox
Dim cbx As New CheckBox
'some options
cbx.ThreeState = True
cbx.CheckState = CheckState.Indeterminate
cbx.Name = "cbxTag" & cnt
cnt += 1
cbx.Text = taglist.Item(t)
'cbx.Top = (y + 2) * 10
'cbx.Left = x * 30 + 10
cbx.Location = New Point(x * 60 + 5, (y) * 20 + 20)
'Add it to controls
SplitContainer2.Panel1.Controls.Add(cbx)
' Positioning stuff
If x * 60 + 65 < SplitContainer2.Panel1.Width Then
x += 1
Else
x = 0
y += 1
End If
Next
Using debug i see that the items got added to the collection and the position seems right (in a visible position).
The problem: Only the first item of each row appears on the form.
Edit: The problem seems to be something with the x koordinate.
Thanks in advance

The problem was the size of the checkbox. Unlike to a checkbox added in the designer the Autosize property is FALSE when creating a checkbox with Dim cbx As New CheckBox.
The result was that they got created with a size around 80 - 100 and appeared to be overlaping each other in a way they were not visible ( the background of the first checkbox hid the checkbox of the second and so on.)
so the solution to my problem was :
cbx.AutoSize = True

Related

Dynamically created checkboxes in Windows Form only works when offsetting in the negative x direction

I'm creating this windows form and I want to dynamically create an array of checkboxes. Here is my code:
Dim Checkboxes(NumberOfRows - 1, NumberOfColumns - 1)
Dim Posx, Posy As Integer
Posx = 10
Posy = 10
For RowCount = 0 To NumberOfRows - 1
For ColumnCount = 0 To NumberOfColumns - 1
Dim checkbox = New CheckBox
Checkboxes(RowCount, ColumnCount) = checkbox
Controls.Add(checkbox)
checkbox.Location = New Point(Posx, Posy)
Posx += 20
Next
Posy += 20
Posx = 10
Next
The resulting form looks like this:
However, the checkboxes in the x-direction are not being shown. It works well in the y-direction though.
If I change Posx += 20 to Posx -= 20, the dynamically created checkboxes appear, like so (array of checkboxes get cut off by the form's left boundary):
Why do the dynamically created checkboxes only appear when offsetting in the negative x-direction, while the y-direction is fine. Why does offsetting in the positive x-direction not work? How do I fix this?

Sort / positioning pictureboxes like a grid in visual basic?

I have 1 textbox with dynamic integer number as text (can be 1 to 35) (number of products) also 35 hidden pre-created pictureboxes (picturebox1,picturebox2,etc) ('cause i dont know how to create at runtime and reference (handle) later propperly without errors) then, with a loop i set to visible just the dynamic number of pictureboxes i need (depending number of products)
For i As Integer = 1 To NumberOfProducts.Text
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).Visible = True
End if
next
then, i create a system to determine best (table) picture width/hieght dimentions depending the number of products available... to display like a grid with the best screen-fill (into a vertical display).... for 31-35 products the grid must be 5columns*7rows, for 21-24 products must be 4x6, 3prod. must be 2x2, etc...
If Val(NumberOfProducts.Text) > 30 Then
grosso = Me.Width / 5
lungo = Me.Height / 7
Horix.Text = "5"
Vertix.Text = "7"
ElseIf Val(NumberOfProducts.Text) > 20 Then
grosso = Me.Width / 4
lungo = Me.Height / 6
Horix.Text = "4"
Vertix.Text = "6"
ElseIf Val(NumberOfProducts.Text) > 2 Then
grosso = Me.Width / 2
lungo = Me.Height / 2
Horix.Text = "2"
Vertix.Text = "2"
ElseIf Val(NumberOfProducts.Text) > 1 Then
grosso = Me.Width / 1
lungo = Me.Height / 2
Horix.Text = "1"
Vertix.Text = "2"
ElseIf Val(NumberOfProducts.Text) = 1 Then
grosso = Me.Width / 1
lungo = Me.Height / 1
Horix.Text = "1"
Vertix.Text = "1"
End If
Then a list of the textboxes getting "grosso" value as ".width" and "lungo" as ".height".... 'cause i think its the most semi-automated way to assign the same setting to all pictureboxes at my form..
i create the Horix.text and vertix.text values just if it could be used as reference later... I suppose i must to locate the first picturebox (the same position for all grid possibilities as reference to the other pictureboxes position)
PictureBox1.Location = New Point(0, 0)
At this point i dont know what to do!!, to set the pictureboxes positions simulting a grid!! i belive i must use a loop after positioning the first picturebox, loop from box#2 to numberofproducts available... subtracting 1 to horix.text (maximum items at each row) and check on each loop if its ">" than horix... then a jump referencing the last picturebox (.top + picturebox.height... and also substract 1 to vertix.text value too... until 0 but:
i dont know how to set the new position referencing the last picturebox on the same row!! i've tried:
for i as integer = 2 to numberofproducts.text
If Me.Controls.ContainsKey("PictureBox" & i) Then
Me.Controls("PictureBox" & i).left = "PictureBox" & i -1).left
End if
next
but it dont works, i get ".left is not controls property" or some like that then i also dont know how to add and determine when to jump to the next row or column... any suggestion?? (code sample)

Arranging Buttons inside a Panel vb.net

I am trying to put 25 Buttons inside a Panel in VB.Net 2015 Classic Form Like the Picture but it is not working . could you please help... bellow my code
Dim i As Integer
For i = 1 To 25
newButton = New Windows.Forms.Button
newButton.AutoSize = True
newButton.Name = "btnButton" & i
newButton.Text = "Button " & i
newButton.Top = i * 5
newButton.Left = i * 25
newButton.Size = New Size(95, 70)
AddHandler newButton.Click, AddressOf ButtonClicked
Panel1.Controls.Add(newButton)
Next
Your code is creating the buttons, and the problem is that they are not arranged properly so what you need to do is arrange them in rows and columns. here i help you to do this;
Here this snippet will tell you how to arrange them in 5 columns and n rows:
Dim x As Integer = 5 ' x co-ordinate of the point
Dim y As Integer = 5 ' y co-ordinate of the point
For i = 1 To 25
If i Mod 5 = 0 Then ' For starting next row after column
y += 100 ' 100 is not mandatory change as per size of button
x = 0
Else
x += 100 ' 100 is not mandatory change as per size of button
End If
Dim p As Point = New Point(x, y)
Dim newButton = New Windows.Forms.Button
newButton.Location = p
//do the rest of formatting here
Panel1.Controls.Add(newButton)
Next

Fitting runtime buttons inside a form

I have a number of buttons between 5-20 and it's variable each time the form loads based on the user settings. I am trying to fit all these buttons on my form no matter what the size of the form is. The buttons are generated during runtime. I would like the first button to be 20 points from the top bar (at any size) and the rest of the buttons simply to fit in the form. This is what I have now but I have to maximize the form to view them all and also while I'm expanding the form the space between the buttons decreases and they overlap with each other whereas they should keep a relative distance. Any ideas?
Dim iButtonWidth, iButtonHeight, iVerticalSpace As Integer
If UserButtons.Count > 0 Then
iButtonHeight = Me.Size.Height - (Me.Size.Height * 0.85)
iButtonWidth = Me.Size.Width - (Me.Size.Width * 0.5)
iVerticalSpace = iButtonHeight / 3
For Each btn In UserButtons
btn.Size = New System.Drawing.Size(iButtonWidth, iButtonHeight)
btn.Location = New Point(20, 20 + btn.TabIndex * iVerticalSpace * 3)
Next
End If
Here's a quick example using the TableLayoutPanel to play with:
Public Class Form1
Private UserButtons As New List(Of Button)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static R As New Random
Dim NumButtons As Integer = R.Next(5, 21) ' "a number of buttons between 5-20 and it's variable each time"
UserButtons.Clear()
For i As Integer = 1 To NumButtons
Dim btn As New Button()
btn.Text = i.ToString("00")
btn.Dock = DockStyle.Fill ' Optional: See how you like it with this on vs. off
UserButtons.Add(btn)
Next
DisplayButtons()
End Sub
Private Sub DisplayButtons()
TableLayoutPanel1.SuspendLayout()
TableLayoutPanel1.Controls.Clear()
TableLayoutPanel1.ColumnStyles.Clear()
TableLayoutPanel1.ColumnCount = 5 ' Fixed Number of Columns
For i As Integer = 1 To TableLayoutPanel1.ColumnCount
TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 911)) ' the size doesn't matter here, as long as they are all the same
Next
' Variable Number of Rows:
Dim RowsRequired As Integer = ((UserButtons.Count - 1) \ TableLayoutPanel1.ColumnCount) + 1 ' Integer Division
TableLayoutPanel1.RowStyles.Clear()
TableLayoutPanel1.RowCount = RowsRequired
For i As Integer = 1 To TableLayoutPanel1.RowCount
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Percent, 911)) ' the size doesn't matter here, as long as they are all the same
Next
TableLayoutPanel1.Controls.AddRange(UserButtons.ToArray)
TableLayoutPanel1.ResumeLayout()
End Sub
End Class
First of all what kind of container are the buttons in? You should be able to set the container's AutoScroll property to true - then when controls within it spill out of the visible bounds you will get a scroll bar.
Then also what you could do is draw each button in more of a table with a certain number next to each other before dropping down to the next line (instead of just 1 button on each line). If that is an option that works for you then you could get more buttons within the visible space.
I happen to have an example to do the same thing with picture boxes (and text boxes under each picture box). Hope this helps:
Dim point As New Point(0, 0)
'create 11 picture and text boxes-you can make this number the number your user selects.
Dim box(11) As PictureBox
Dim text(11) As TextBox
Dim i As UInt16
For i = 0 To 11 'or whatever your number is
box(i) = New PictureBox
box(i).Width = 250 'your button width
box(i).Height = 170 'your button height
box(i).BorderStyle = BorderStyle.FixedSingle
box(i).Location = point
layoutsPanel.Controls.Add(box(i)) 'my container is a panel
text(i) = New TextBox
text(i).Height = 50
text(i).Width = 250
point.Y += box(i).Height
text(i).Location = (point)
layoutsPanel.Controls.Add(text(i))
point.Y -= box(i).Height 'reset Y for next picture box
'Put 4 picture boxes in a row, then move down to next row
If i Mod 4 = 3 Then
point.X = 0
point.Y += box(i).Height + text(i).Height + 4
Else
point.X += box(i).Width + 4
End If
Next

Dynamically adding Panel and RadioButtons in Visual Basic

Guys I'm trying to dynamically create panels which are filled with seven radio buttons each.
I get the panels but they are only filled with 1 radio button each. What am I doing wrong here? QuestionQuantity is an Integer and is the variable that determines how many panels I will be creating. The code is in the form load function located.
Thanks,
Dim Pan As Panel
Dim RButton As RadioButton
For x As Integer = 1 To QuestionsQuantity Step 1
Pan = New Panel
Pan.Name = "Panel" & Convert.ToString(x)
Pan.Left = 300
Pan.Top = 100 + 52 * (x - 1)
Pan.Height = 48
Pan.Width = 280
Pan.BackColor = Color.Coral
Controls.Add(Pan)
For y As Integer = 1 To 7 Step 1
RButton = New RadioButton
RButton.Name = "RadioButton" & Convert.ToString(x) & Convert.ToString(y)
RButton.Left = 1 + 30 * (y - 1)
RButton.Top = 10
RButton.Text = Convert.ToString(y)
RButton.CheckAlign = System.Drawing.ContentAlignment.BottomCenter
RButton.TextAlign = System.Drawing.ContentAlignment.TopCenter
RButton.UseVisualStyleBackColor = True
Controls.Add(RButton)
Pan.Controls.Add(RButton)
Next
Next
I messed with it and took out the Panel section and just used the RadioButtons in order to see if i get seven of these. I can get seven if i ofset them in the y direction (.top) but it does not work for some reason in the x (.left) direction
For y As Integer = 1 To 7 Step 1
RButton = New RadioButton
RButton.Name = "RadioButton1" & Convert.ToString(y)
RButton.Left = 20 + (y * 30)
RButton.Top = 10
RButton.Text = Convert.ToString(y)
RButton.CheckAlign = System.Drawing.ContentAlignment.BottomCenter
RButton.TextAlign = System.Drawing.ContentAlignment.TopCenter
RButton.UseVisualStyleBackColor = True
Controls.Add(RButton)
Next
Please help me!
I finally got it. The problem was declaring the size of the RadioButton. It will be too big if not declared even if i make the spacing bigger.
RButton.Size = New System.Drawing.Size(17, 30)
That solved the problem.
You should be using a UserControl that contains your seven radio buttons.
For x As Integer = 1 To QuestionsQuantity
Dim pan As New QuestionUserControl
Pan.Name = "Panel" & Convert.ToString(x)
Pan.Left = 300
Pan.Top = 100 + 52 * (x - 1)
Pan.Height = 48
Pan.Width = 280
Pan.BackColor = Color.Coral
Me.Controls.Add(Pan)
Next
If sticking with your current code, remove this (you should be only adding it to the panel):
For y As Integer = 1 To 7
'// Controls.Add(RButton)
Pan.Controls.Add(RButton)
Next
As far as seeing the control, I'm guessing you aren't going "right" enough:
Dim leftMark As Integer = 20
For y As Integer = 1 To 7
'// code
RButton.Left = leftMark
'//code
leftMark += rButton.Width + 4
Next
with a Pan.Height = 48 there's not going to be much controls inside.
You add the button both to the panel an to Controls ?
Put the radiobuttons in a gridbox. You can dynamically add rows of radiobuttons as you wish. If this will work for you, I'll send a sample code when I get home.