Cant create 2nd textbox
I'm having problems with this code and I can't figure out why. It works fine the first time through but crashes with a "Parameter is not Valid" error the 2nd time through on this line:
Dim tbx As TextBox = New Windows.Forms.TextBox
The full code is as follows:
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)
Here's the stack trace:
at System.Drawing.Font.GetHeight(Graphics graphics) at System.Drawing.Font.GetHeight() at System.Drawing.Font.get_Height() at System.Windows.Forms.Control.get_FontHeight() at System.Windows.Forms.TextBoxBase.get_PreferredHeight() at System.Windows.Forms.TextBoxBase.get_DefaultSize() at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext) at System.Windows.Forms.TextBoxBase..ctor() at System.Windows.Forms.TextBox..ctor()
Any help is appreciat开发者_JAVA技巧ed.
I know this is an old question but here is my answer.
I suspected the
USING .... END USING section as well.
I have just read that in the feedback too, oh well, never mind.
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
精彩评论