Sort / positioning pictureboxes like a grid in visual basic? - vb.net

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)

Related

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

Items added to ControlCollection not showing

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

label doesn't appear on dynamically created form vb.net

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.

binding navigator to next item in vb.net

I am still new to visual basic 2010.
I would like to change the option of binding navigator because it always move to the next row.
How could I make it to move to the next 9 rows?
For example is this:
I load form main and fill 9 rows of data by adding code:
Dim inc As Integer = 0
label1.Text = dataSet.Tables("DPT").Rows(inc + 0).Items(2)
label2.Text = dataSet.Tables("DPT").Rows(inc + 1).Items(2)
.....
label9.Text = dataSet.Tables("DPT").Rows(inc + 8).Items(2)
but when I debug and press next item, all the value of label1.Text ---> label9.Text are filled with rows number two.... Could you help me guys?
There is alot wrong that I see you have going on here... Check out my edits...
Dim inc As Integer = 0 'This isn't doing anything...
label1.Text = dataSet.Tables("DPT").Rows(inc + 0).Items(2) 'Your always referencing item 2
label2.Text = dataSet.Tables("DPT").Rows(inc + 1).Items(2)
label9.Text = dataSet.Tables("DPT").Rows(inc + 8).Items(2)
First change all of your labels text to this...
label1.Text = dataSet.Tables("DPT").Rows(1).Item(1) 'Change the number for your row and item as you go down... Or better yet use your Item name instead of the number...

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.