Please support our VB.NET advertiser: Programming Forums
Views: 9346 | Replies: 7
![]() |
•
•
Join Date: May 2003
Posts: 36
Reputation:
Rep Power: 6
Solved Threads: 0
Here's an example, but Visual basic gives me an error when trying to run it. Say I wanted to ask the user for their name, then display it like: "Hello there" strName
By the way, my CDs finally came Friday, so I have VB .NET, but this was done in VB6.
--------------
Private Sub cmdStart_Click()
Dim strName As String
strName = txtName.Text
If txtName.Text = "" Then
MsgBox "Please Enter a Name",,"Error"
Else
lblGreeting.Caption = "Hello there" strName
txtName.Text = ""
End If
End Sub
---------------
By the way, my CDs finally came Friday, so I have VB .NET, but this was done in VB6.
--------------
Private Sub cmdStart_Click()
Dim strName As String
strName = txtName.Text
If txtName.Text = "" Then
MsgBox "Please Enter a Name",,"Error"
Else
lblGreeting.Caption = "Hello there" strName
txtName.Text = ""
End If
End Sub
---------------
lblGreeting.Caption = "Hello there" strName
i cant quite remember which you need to us but that line of code should look like this
lblGreeting.Caption = "Hello there" & strName
lblGreeting.Caption = "Hello there" + strName
try them both out, im actually pretty sure that it is "+" so give that a try first
i will have to get back to you on this. i will have to go and look in vb and i am on linux right now so i will go over to my windows partition after while and see what i can come up with for you.
Okay, in response to your first question, it is ALWAYS better to use & when connecting strings.
For your second, you would need to declare an integer or something on top of the forms codebehind (in the general declarations area) and use it as a counter. Then you can have if counter = 1 then do whatever, etc.
For your second, you would need to declare an integer or something on top of the forms codebehind (in the general declarations area) and use it as a counter. Then you can have if counter = 1 then do whatever, etc.
-Ryan Hoffman
ASP.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
ASP.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
•
•
Join Date: May 2003
Posts: 36
Reputation:
Rep Power: 6
Solved Threads: 0
That seems to sound plausible. So I would do something like:
-------
Option Explicit
Dim intCounter As Integer
intCounter = 0
Private Sub cmdStart_Click()
Dim strName As String
strName = txtName.Text
intCounter = intCounter + 1
If intCounter = 1 Then
ElseIf txtName.Text = "" Then
MsgBox "Please Enter a Name",,"Error"
Else
lblGreeting.Caption = "Hello there" strName
txtName.Text = ""
lblGreeting.Caption = "What do you want to talk about?"
End If
If intCounter = 2 Then
ElseIf txtName.Text = "" Then
MsgBox "Please Enter Something to talk about",,"Error"
Else
lblGreeting.Caption = "Well, " & strName & " sounds fun"
txtName.Text = ""
End If
End Sub
---------
Would this be easier using Select Case, or what else could be an option? I'm trying to create a chat bot to talk to for an exercise, and I think things could get messy if I kept doing that.
Edit: Nevermind, that didn't work. It went right to the second case. (If counter = 2) Any Ideas?
-------
Option Explicit
Dim intCounter As Integer
intCounter = 0
Private Sub cmdStart_Click()
Dim strName As String
strName = txtName.Text
intCounter = intCounter + 1
If intCounter = 1 Then
ElseIf txtName.Text = "" Then
MsgBox "Please Enter a Name",,"Error"
Else
lblGreeting.Caption = "Hello there" strName
txtName.Text = ""
lblGreeting.Caption = "What do you want to talk about?"
End If
If intCounter = 2 Then
ElseIf txtName.Text = "" Then
MsgBox "Please Enter Something to talk about",,"Error"
Else
lblGreeting.Caption = "Well, " & strName & " sounds fun"
txtName.Text = ""
End If
End Sub
---------
Would this be easier using Select Case, or what else could be an option? I'm trying to create a chat bot to talk to for an exercise, and I think things could get messy if I kept doing that.
Edit: Nevermind, that didn't work. It went right to the second case. (If counter = 2) Any Ideas?
im sure the case would work great and probably be alot nicer looking. could you post the code you used so we can see why it didnt work. cause it should work great.
i should have waited till you gave me what you had but i really wanted to do it. :oops: but this worked great for me. and i dont think you can initiate a variable in the Option Explicit. you should do it in the form load like i did above
Option Explicit
Dim intCounter As Integer
Private Sub cmdStart_Click()
Dim strName As String
strName = txtName.Text
intCounter = intCounter + 1
Select Case intCounter
Case Is = 1
If txtName.Text = "" Then
MsgBox "Please Enter a Name", , "Error"
Else
lblGreeting.Caption = "Hello there" & strName
txtName.Text = ""
lblGreeting.Caption = "What do you want to talk about?"
End If
Case Is = 2
If txtName.Text = "" Then
MsgBox "Please Enter Something to talk about", , "Error"
Else
lblGreeting.Caption = "Well, " & strName & " sounds fun"
txtName.Text = ""
End If
End Select
End Sub
Private Sub Form_Load()
lblGreeting.Caption = "What is your Name?"
intCounter = 0
End Subi should have waited till you gave me what you had but i really wanted to do it. :oops: but this worked great for me. and i dont think you can initiate a variable in the Option Explicit. you should do it in the form load like i did above
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)





Linear Mode