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.
Related
So I've been having problems adding labels from a record structure into a groupbox that stack below each other vertically. I could use a Flow Layout Panel but I have multiple labels I need to add from data in a record structure which I want to be able to scroll later with a vertical scrollbar as the autoscroll will only work with one panel only, and not all of them.
For some reason the program puts only one label in, and the others don't seem to be visible even though they have been created (I used a messagebox to check). Could someone please help me to put them in, as I am fairly new to programming and need help.
For context, the program loads 'materials' from an xml file and stores it in a record structure, then this bit of the program creates labels and radio buttons dynamically and puts it in a groupbox, so that it can be arranged manually on a pretty grid that will all become scrollable by using a scrollbar later.. Because the data I am working with is all related, and can have strings of multiple sizes (names and suppliers of materials for example) I didn't want to attempt to add whitespace to make the grid work but with a single label (not that the others would show beneath the first for some reason)
CODE:
'sets up Labels for editing materials
prgFunctions.loadMat()
Dim counter As Integer
Dim newMatIDLabel(numMatFile) As Label
Dim newMatRdb(numMatFile) As RadioButton
Dim lastPos As Integer
'testing to see if materials load GET RID OF LATER
' For counter = 1 To numMatFile
'ListBox1.Items.Add(materials(counter).matName)
' Next
'create the labels with information
For counter = 1 To numMatFile
'ID
newMatIDLabel(counter) = New Label
newMatIDLabel(counter).Name = "lblMatIDNum" & counter
newMatIDLabel(counter).Text = materials(counter).matID
newMatIDLabel(counter).Parent = gbxMaterials
' MsgBox(newMatIDLabel(counter).Name & " " & newMatIDLabel(counter).Text)
Next
'create the checkboxes NOW RADIO BUTTONS
For counter = 1 To numMatFile
newMatRdb(counter) = New RadioButton
newMatRdb(counter).Name = "chkMatSelectNum" & counter
newMatRdb(counter).Text = ""
newMatRdb(counter).Parent = gbxMaterials
Next
'matID locations
lastPos = 10
For counter = 1 To numMatFile
'SOMEHOW MOVE IT INTO THE GROUPBOX INSTEAD, as issues arise everywhere
newMatIDLabel(counter).Location = New Point(7, lastPos + 10)
lastPos = lastPos + 10
Next
Why not just:
lastPos = 10
For counter = 1 To numMatFile
Dim label As New Label
label.Name = "chkMatSelectNum" & counter
label.Text = materials(counter).matID
label.Location = New Point(7, lastPos)
label.Visible = True
gbxMaterials.Controls.Add(label)
lastPos += label.Height
Next
Based on F0r3v3r-A-N00b's answer:
The label has a height of 23, so it seems it is hiding the others.
This works for me:
Dim lastPos As Integer = 20
For counter As Integer = 1 To numMatFile
Dim label As New Label
label.Name = "chkMatSelectNum" & counter
label.Text = materials(counter).matId
label.AutoSize = True
label.Visible = True
label.Location = New Point(7, lastPos)
gbxMaterials.Controls.Add(label)
lastPos += 17
Next
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
I need to be able to create dynamic radio buttons and label for each row in a dataTable. The code i have written so far produces radio buttons which are linked so i select multiple buttons at once. Rather, the radio buttons de-select upon pressing another.
Do
For count = 0 To NoOfRecords - 1
For i = 1 To NoOfRecords - 1
Dim XCoord, YCoord As Integer
XCoord = 269
YCoord = 172
Dim XCoord1, YCoord1 As Integer
Dim Labeli As Label
Dim RadioButtonsi As RadioButton
XCoord1 = 269 : YCoord1 = 172
For count1 = 0 To NoOfRecords - 1
Labeli = New Label
Labeli.Location = New System.Drawing.Point(XCoord, YCoord)
YCoord = YCoord + 120
Labeli.Text = count1 & ". " & dataTable.Rows(count1).Item(0)
Me.Controls.Add(Labeli)
RadioButtonsi = New RadioButton
RadioButtonsi.Location = New System.Drawing.Point(XCoord1, (YCoord1 + 20))
YCoord1 = YCoord1 + 120
RadioButtonsi.Text = dataTable.Rows(count1).Item(1)
Me.Controls.Add(RadioButtonsi)
Next
Next
Next
Loop Until count = NoOfRecords
So just to summarize, I need to be able to create controls for each row in the dataTable which aren't linked.
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
I'm staring at my code for hours now and I don't understand what's going on. I'm creating a form with a number of checkboxes and labels, based on a textbox value from another form.
This is the code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim lb As Label
Dim cb1 As CheckBox
Dim cb As CheckBox
Dim i As Integer
Dim j As Integer
For i = 1 To CInt(Aantal.Text) - 1
indivwoningen.Width = indivwoningen.Size.Width + 21
indivwoningen.Button1.Location = New Point(indivwoningen.Button1.Location.X + 21, indivwoningen.Button1.Location.Y)
lb = New Label
indivwoningen.Controls.Add(lb)
lb.Text = i + 1
lb.Font = indivwoningen.Label23.Font
lb.Location = New Point(indivwoningen.Label23.Location.X + 21 * i, indivwoningen.Label23.Location.Y)
For j = 1 To 18
cb1 = New CheckBox
indivwoningen.Controls.Add(cb1)
cb = indivwoningen.Controls.Find("CheckBox" & j & "00", False)(0)
cb1.Location = New Point(cb.Location.X + 21 * i, cb.Location.Y)
cb1.Width = cb.Width
cb1.Text = cb.Text
If i < 10 Then
cb1.Name = "CheckBox" & j & "0" & i
Else
cb1.Name = "CheckBox" & j & i
End If
Next
Next
indivwoningen.Show()
End Sub
The created form has got two flaws:
Only the first created label is visible
The checkboxes aren't properly aligned.
I don't understand what's going on. Can someone help me?
EDIT: Here are pictures before I create extra controls and after
Set the Autosize property to True for the labels and set the Height of the checkboxes to the same height of your reference checkbox
For i = 1 To CInt(Aantal.Text) - 1
....
lb = New Label
indivwoningen.Controls.Add(lb)
lb.Text = i + 1
lb.Font = indivwoningen.Label23.Font
lb.Autosize = True
lb.Location = New Point(indivwoningen.Label23.Location.X + 21 * i, indivwoningen.Label23.Location.Y)
For j = 1 To 18
cb1 = New CheckBox
indivwoningen.Controls.Add(cb1)
cb = indivwoningen.Controls.Find("CheckBox" & j & "00", False)(0)
cb1.Location = New Point(cb.Location.X + 21 * i, cb.Location.Y)
cb1.Width = cb.Width
cb1.Height = cb.Height
cb1.Text = cb.Text
If i < 10 Then
cb1.Name = "CheckBox" & j & "0" & i
Else
cb1.Name = "CheckBox" & j & i
End If
Next
Next
Well, for the checkbox the explanation seems to be easy. The new checkbox has, by default, an Heigth of 24 pixels while the one drawn on the form as a smaller Height. So, because the check square is centered inside the Height of the Checkbox it appears to be not aligned to the reference checkbox.
For the labels the problem is of the same kind. Without setting the Autosize, the labels are created with a default size of 100x23 pixels. This means that the label with text "2" extends its size to cover the position of the labels with text "3","4","5","6", while the label with text "3" covers the label with text "7" and so on.
In any case setting AutoSize seems to be the default behavior, followed also in the Form.Designer.vb file where the controls are created following your design time instructions.
You could also try to set the Size of the dynamically created labels to the same size of the reference label and the effect is the same.