2011-12-01 52 views
0

夥計們我試圖動態創建每個都有七個單選按鈕的面板。 我得到的面板,但他們只填充1個單選按鈕。我在這裏做錯了什麼? QuestionQuantity是一個整數,它是決定我將創建多少個面板的變量。代碼位於表單加載函數中。在Visual Basic中動態添加Panel和RadioButtons

感謝,

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 

我搞砸與它拿出面板部分,只是使用的單選按鈕才能看到,如果我得到的這七個。我可以得到7,如果我在y方向(.TOP)OFSET他們,但它並不適用於某些原因在x工作(。左)方向

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 

請幫幫我!

我終於明白了。問題是宣佈RadioButton的大小。即使我沒有宣佈更大的空間,它也會太大。

RButton.Size = New System.Drawing.Size(17, 30) 

解決了這個問題。

回答

0
  1. 與Pan.Height = 48裏面不會有太多的控制。
  2. 您將按鈕都添加到面板和控件?
+0

那麼我看到第一個單選按鈕。其他人會是正確的,因爲我增加了RButtin.Left30。我想我首先必須將它們添加到控件中,然後將它們「分配」給面板?! – Patrik

+0

我的bad.maybe在.Left和你的按鈕的.Align之間存在衝突。 – GameAlchemist

+0

我自己運行它並操作代碼。我改變了.top而不是.left,並且我得到了七個單選按鈕。有些東西正在搞亂.left – Patrik

0

您應該使用包含您的七個單選按鈕的UserControl。

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 

如果你當前的代碼粘貼,刪除此(你應該只將它添加到面板):

For y As Integer = 1 To 7 
    '// Controls.Add(RButton) 
    Pan.Controls.Add(RButton) 
Next 

就看到了控制,我猜你是不是「足夠」:

Dim leftMark As Integer = 20 
For y As Integer = 1 To 7 
    '// code 
    RButton.Left = leftMark 
    '//code 

    leftMark += rButton.Width + 4 
Next 
0

將單選按鈕放在網格框中。您可以根據需要動態添加單選按鈕行。如果這對您有用,我會在我回家時發送示例代碼。

相關問題