Patrik
Patrik

Reputation: 19

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.

Upvotes: 0

Views: 17800

Answers (3)

Jake Pangan
Jake Pangan

Reputation: 33

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.

Upvotes: 0

LarsTech
LarsTech

Reputation: 81655

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

Upvotes: 1

GameAlchemist
GameAlchemist

Reputation: 19294

  1. with a Pan.Height = 48 there's not going to be much controls inside.
  2. You add the button both to the panel an to Controls ?

Upvotes: 0

Related Questions