2012-07-03 25 views
13

Tôi đang làm việc trên khoảng không quảng cáo trong Visual Basic 2010 Express Edition. Tôi không biết số lượng trường cần thiết cho khoảng không quảng cáo. Hy vọng của tôi là tôi có thể thêm hộp văn bản/hộp kiểm/nút sử dụng cho các vòng trong chương trình. Có cách nào để thêm điều khiển vào biểu mẫu mà không sử dụng hộp công cụ không?Cách lập trình thêm điều khiển vào biểu mẫu trong VB.NET

Tôi có thể thêm các điều khiển bằng cách khởi tạo chúng trong chương trình không?

Trả lời

15

Có.

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim MyTextbox as New Textbox 
    With MyTextbox 
     .Size = (100,20) 
     .Location = (20,20) 
    End With 
    AddHandler MyTextbox.TextChanged, AddressOf MyTextbox_TextChanged 
    MyForm.Controls.Add(MyTextbox) 
End Sub 

Friend Sub MyTextbox_Changed(sender as Object, e as EventArgs) 
    'Write code here. 
End Sub 
+3

Vui lòng thêm một số giải thích. –

+0

Xin chào, Holger, tôi có thể thêm điều này trên bảng điều khiển không? –

+0

Có thể. Mọi điều khiển đều có thuộc tính Điều khiển mà bạn có thể thêm (hoặc xóa) các điều khiển từ đó. Chỉ cần nhớ tự thêm bất kỳ trình xử lý nào bạn muốn triển khai. Đối với bảng điều khiển của bạn, hãy thay thế "MyForm" trong ví dụ trên bằng tên bảng điều khiển của bạn. –

7
Dim numberOfButtons As Integer 
Dim buttons() as Button 

Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Redim buttons(numberOfbuttons) 
    for counter as integer = 0 to numberOfbuttons 
     With buttons(counter) 
      .Size = (10, 10) 
      .Visible = False 
      .Location = (55, 33 + counter*13) 
      .Text = "Button "+(counter+1).ToString ' or some name from an array you pass from main 
      'any other property 
     End With 
     ' 
    next 
End Sub 

Nếu bạn muốn kiểm tra mà các textbox có thông tin, hoặc có nút radio được nhấp, bạn có thể lặp qua một vòng lặp trong một nút OK.

Nếu bạn muốn có thể để nhấp vào mục mảng cá nhân và họ đã phản ứng với các sự kiện, thêm vào trong vòng lặp Form_Load như sau:

AddHandler buttons(counter).Clicked AddressOf All_Buttons_Clicked 

sau đó tạo

Private Sub All_Buttons_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    'some code here, can check to see which checkbox was changed, which button was clicked, by number or text 
End Sub 

khi bạn gọi: objectYouCall.numberOfButtons = initial_value_from_main_program

response_yes_or_no_or_other = objectYouCall.ShowDialog() 

Đối với nút radio, hộp văn bản, cùng câu chuyện, khác nhau ent kết thúc.

2

Để thêm điều khiển động vào biểu mẫu, hãy làm như sau. Ở đây chúng tôi đang tạo các điều khiển hộp văn bản để thêm động.

Public Class Form1 
    Private m_TextBoxes() As TextBox = {} 

    Private Sub Button1_Click(ByVal sender As System.Object, _ 
           ByVal e As System.EventArgs) _ 
           Handles Button1.Click 

     ' Get the index for the new control. 
     Dim i As Integer = m_TextBoxes.Length 

     ' Make room. 
     ReDim Preserve m_TextBoxes(i) 

     ' Create and initialize the control. 
     m_TextBoxes(i) = New TextBox 
     With m_TextBoxes(i) 
      .Name = "TextBox" & i.ToString() 
      If m_TextBoxes.Length < 2 Then 
       ' Position the first one. 
       .SetBounds(8, 8, 100, 20) 
      Else 
       ' Position subsequent controls. 
       .Left = m_TextBoxes(i - 1).Left 
       .Top = m_TextBoxes(i - 1).Top + m_TextBoxes(i - _ 
        1).Height + 4 
       .Size = m_TextBoxes(i - 1).Size 
      End If 

      ' Save the control's index in the Tag property. 
      ' (Or you can get this from the Name.) 
      .Tag = i 
     End With 

     ' Give the control an event handler. 
     AddHandler m_TextBoxes(i).TextChanged, AddressOf TextBox_TextChanged 

     ' Add the control to the form. 
     Me.Controls.Add(m_TextBoxes(i)) 
    End Sub 

    'When you enter text in one of the TextBoxes, the TextBox_TextChanged event 
    'handler displays the control's name and its current text. 
    Private Sub TextBox_TextChanged(ByVal sender As _ 
    System.Object, ByVal e As System.EventArgs) 
     ' Display the current text. 
     Dim txt As TextBox = DirectCast(sender, TextBox) 
     Debug.WriteLine(txt.Name & ": [" & txt.Text & "]") 
    End Sub 
End Class 
+0

Lưu ý các câu trả lời khác thực sự giải thích mã của họ như thế nào? –

+0

Tôi sẽ thêm chúng vào một 'FlowLayoutPanel' hoặc tự động thêm các ô vào một' DataGridView', nếu biểu mẫu cho phép nó. Bằng cách đó không cần thiết lập giới hạn hoặc vị trí chính xác. Ví dụ hay. – Rachael

6
Public Class Form1 
    Private boxes(5) As TextBox 

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
     Dim newbox As TextBox 
     For i As Integer = 1 To 5 'Create a new textbox and set its properties26.27. 
     newbox = New TextBox 
     newbox.Size = New Drawing.Size(100, 20) 
     newbox.Location = New Point(10, 10 + 25 * (i - 1)) 
     newbox.Name = "TextBox" & i 
     newbox.Text = newbox.Name 'Connect it to a handler, save a reference to the array & add it to the form control. 
     AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged 
     boxes(i) = newbox 
     Me.Controls.Add(newbox) 
     Next 
    End Sub 

    Private Sub TextBox_TextChanged(sender As System.Object, e As System.EventArgs) 
     'When you modify the contents of any textbox, the name of that textbox 
     'and its current contents will be displayed in the title bar 

     Dim box As TextBox = DirectCast(sender, TextBox) 
     Me.Text = box.Name & ": " & box.Text 
    End Sub 
End Class 
Các vấn đề liên quan