943,472 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 25258
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 14th, 2003
0

Using string variables in a label or text box?

Expand Post »
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
---------------
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

VB.NET Syntax (Toggle Plain Text)
  1. lblGreeting.Caption = "Hello there" strName

i cant quite remember which you need to us but that line of code should look like this

VB.NET Syntax (Toggle Plain Text)
  1. lblGreeting.Caption = "Hello there" & strName
or
VB.NET Syntax (Toggle Plain Text)
  1. lblGreeting.Caption = "Hello there" + strName

try them both out, im actually pretty sure that it is "+" so give that a try first
Team Colleague
Reputation Points: 36
Solved Threads: 2
PFO Founder
big_k105 is offline Offline
308 posts
since May 2003
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

They both work. I looked it up on a book I had, I guess before when I was searching I was just looking in the wrong place. Both + and & are used for string concatenation. Thanks for the help.
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

I also had another queston, what if I wanted to use that same button for a different procedure. Like if I wanted to write an event procedure for the second time it was clicked.
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

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.
Team Colleague
Reputation Points: 36
Solved Threads: 2
PFO Founder
big_k105 is offline Offline
308 posts
since May 2003
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

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.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

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?
Reputation Points: 13
Solved Threads: 0
Light Poster
Mr Gates is offline Offline
36 posts
since May 2003
Jul 14th, 2003
0

Re: Using string variables in a label or text box?

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.

VB.NET Syntax (Toggle Plain Text)
  1. Option Explicit
  2. Dim intCounter As Integer
  3.  
  4. Private Sub cmdStart_Click()
  5. Dim strName As String
  6. strName = txtName.Text
  7. intCounter = intCounter + 1
  8.  
  9. Select Case intCounter
  10. Case Is = 1
  11. If txtName.Text = "" Then
  12. MsgBox "Please Enter a Name", , "Error"
  13. Else
  14. lblGreeting.Caption = "Hello there" & strName
  15. txtName.Text = ""
  16. lblGreeting.Caption = "What do you want to talk about?"
  17. End If
  18. Case Is = 2
  19. If txtName.Text = "" Then
  20. MsgBox "Please Enter Something to talk about", , "Error"
  21. Else
  22. lblGreeting.Caption = "Well, " & strName & " sounds fun"
  23. txtName.Text = ""
  24. End If
  25. End Select
  26. End Sub
  27.  
  28. Private Sub Form_Load()
  29. lblGreeting.Caption = "What is your Name?"
  30. intCounter = 0
  31. End Sub

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
Team Colleague
Reputation Points: 36
Solved Threads: 2
PFO Founder
big_k105 is offline Offline
308 posts
since May 2003
Jun 10th, 2010
0
Re: Using string variables in a label or text box?
how to use intcounter in textbox
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeela12345 is offline Offline
2 posts
since Jun 2010
Jun 10th, 2010
0

intcounter

how to use intcounter in textbox
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jeela12345 is offline Offline
2 posts
since Jun 2010
Message:
Previous Thread in VB.NET Forum Timeline: SQL Database Access
Next Thread in VB.NET Forum Timeline: Need help reading excel cell values using VB.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC