无法创建第二个文本框

时间:2010-06-14 22:48:56

标签: vb.net winforms textbox

我遇到了这段代码的问题,我无法弄清楚原因。它第一次工作正常,但第二次在此行崩溃时出现“参数无效”错误:

Dim tbx As TextBox = New Windows.Forms.TextBox

完整代码如下:

Dim tbx As TextBox = New Windows.Forms.TextBox
tbx.Name = tbxName
tbx.Size = New System.Drawing.Size(55, 12)
tbx.BorderStyle = BorderStyle.None
tbx.TextAlign = HorizontalAlignment.Center
Using f As Font = tbx.Font
  tbx.Font = New Font(f.FontFamily, 8, FontStyle.Bold)
End Using
tbx.Location = New System.Drawing.Point(xCords, 44)
Select Case tbx.Name
  Case "tbxBulk01" : tbx.Text = Bulk01Label
  Case "tbxBulk02" : tbx.Text = Bulk02Label
End Select
Me.Controls.Add(tbx)

这是堆栈跟踪:

  

在System.Drawing.Font.GetHeight(图形图形)     在System.Drawing.Font.GetHeight()     在System.Drawing.Font.get_Height()     在System.Windows.Forms.Control.get_FontHeight()     在System.Windows.Forms.TextBoxBase.get_PreferredHeight()     在System.Windows.Forms.TextBoxBase.get_DefaultSize()     在System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)     在System.Windows.Forms.TextBoxBase..ctor()     在System.Windows.Forms.TextBox..ctor()

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,但这是我的答案。

我怀疑

使用....结束使用部分。

我刚刚在反馈中读过,哦,好吧,没关系。

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim xCoords As Integer = 80
        Dim myTextBox As TextBox = New TextBox

        For index As Integer = 1 To 2
            myTextBox = New TextBox
            myTextBox.Name = "MyTextBox" & index.ToString
            myTextBox.Size = New System.Drawing.Size(55, 12)
            myTextBox.BorderStyle = BorderStyle.None
            myTextBox.TextAlign = HorizontalAlignment.Center
            myTextBox.Font = New System.Drawing.Font(myTextBox.Font.FontFamily, 8, FontStyle.Bold)
            myTextBox.Location = New System.Drawing.Point(index * xCoords, 44)
            Select Case index
                Case 1 : myTextBox.Text = "Bulk01Label"
                Case 2 : myTextBox.Text = "Bulk02Label"
            End Select
            Me.Controls.Add(myTextBox)
        Next

    End Sub
End Class