I have the following code that I used in VB6 that pulls text from a textbox and puts it into a variable called FirstName. Not sure what I am doing wrong. But I haved tried assigning the text to a variable using VB.NET and I keep getting an exception error that does not give me any clue to how fix the code.
Can anyone tell me how I need to rewrite this for VB.NET to accept it?
Dim FirstName As String
FirstName = txtFName.Text
Assign Textbox to VariablePut text from textbox into a variable
Page 1 of 1
5 Replies - 23855 Views - Last Post: 14 January 2006 - 11:21 PM
Replies To: Assign Textbox to Variable
#2
Re: Assign Textbox to Variable
Posted 13 January 2006 - 07:22 PM
Hmm. That should work, although the naming convention used in .NET looks like this:
The "Me" in the instruction refers to the current form. Although it will compile and run without it as long as you are only coding for the current or main form.
If the text box is on another form, then you need to replace the "Me" with the actual name of the other form.
What is exception error that you are getting??
If you post your code, I can take a closer look at it.
Dim FirstName As String FirstName = Me.txtFName.Text
The "Me" in the instruction refers to the current form. Although it will compile and run without it as long as you are only coding for the current or main form.
If the text box is on another form, then you need to replace the "Me" with the actual name of the other form.
What is exception error that you are getting??
If you post your code, I can take a closer look at it.
#3
Re: Assign Textbox to Variable
Posted 13 January 2006 - 08:39 PM
Thanks for the reply. Below is the code for my entire form. As written right now all 6 of my variable assignments (ie. FirstName = txtFName.Text) give a build error that says Declaration Expected. I am so lost right now. I do not know what to do.
Public Class Form1
Dim FirstName As String
Dim LastName As String
Dim Dates As String
Dim Time As String
Dim User As String
Dim PC As String
Dim Valid As Boolean
FirstName = txtFName.Text
LastName = txtLName.Text
Dates = DisplayDate.Text
Time = DisplayTime.Text
User = DisplayUsername.Text
PC = DisplayPCName.Text
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If (FirstName Is Nothing) Then
MsgBox("Please enter First Name")
Valid = False
Else
Valid = True
End If
If (LastName Is Nothing) Then
MsgBox("Please enter Last Name")
Valid = False
Else
Valid = True
End If
If Valid = True Then
MsgBox("All fields have been entered")
End If
End Sub
End Class
#4
Re: Assign Textbox to Variable
Posted 13 January 2006 - 09:55 PM
Okay...so I made the declaration expected error go away by moving the variable assignments to within the sub routine instead of outside within the entire form. That has changed from VB6 then. Not sure how you could assign them for the entire form to use.
#5
Re: Assign Textbox to Variable
Posted 13 January 2006 - 10:14 PM
OK, a couple of things I see here that are problems. First if you are going to declare your variables at the class level. Then you need to use the Private identifier, the Dim identifier is for module-level variables only.
In other words, the variable declaration would be inside the click event procedure and would only exist during the execution of that particular method. If you declare it as Private at the class level then all your procedures within the class have access to the variables.
The next problem I see is in your IF/Then statements. The way I see it looks like you are only checking to see if the text box is empty. Don't check it using 'Is Nothing', although it compiles it doesn't do anything relevant or so it seemed to me.
Easiest way to check is like this:
Last thing I see is your variable assignment statements. They need to be located inside the Click Event procedure, as well.
Here is the revised code:
In other words, the variable declaration would be inside the click event procedure and would only exist during the execution of that particular method. If you declare it as Private at the class level then all your procedures within the class have access to the variables.
The next problem I see is in your IF/Then statements. The way I see it looks like you are only checking to see if the text box is empty. Don't check it using 'Is Nothing', although it compiles it doesn't do anything relevant or so it seemed to me.
Easiest way to check is like this:
If (FirstName = "") Then
MsgBox("Please enter First Name")
Valid = False
Else
Valid = True
End If
If (LastName = "") Then
MsgBox("Please enter Last Name")
Valid = False
Else
Valid = True
End If
Last thing I see is your variable assignment statements. They need to be located inside the Click Event procedure, as well.
Here is the revised code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FirstName As String
Dim LastName As String
Dim Dates As String
Dim Time As String
Dim User As String
Dim PC As String
Dim Valid As Boolean
FirstName = Me.txtFName.Text
LastName = Me.txtLName.Text
Dates = Me.DisplayDate.Text
Time = Me.DisplayTime.Text
User = Me.DisplayUserName.Text
PC = Me.DisplayPCName.Text
If (FirstName = "") Then
MsgBox("Please enter First Name")
Valid = False
Else
Valid = True
End If
If (LastName = "") Then
MsgBox("Please enter Last Name")
Valid = False
Else
Valid = True
End If
If Valid = True Then
MsgBox("All fields have been entered")
End If
End Sub
#6
Re: Assign Textbox to Variable
Posted 14 January 2006 - 11:21 PM
Thank u for your help with this code this is why I say this is best site to be... You guys r great.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|