-
Dec 15th, 2017, 11:31 PM
#1
Thread Starter
New Member
[RESOLVED] How to display numbers from textbox objects to labels.
I have a textbox where a person inputs a phone number and have it where, with the click of a button, a label will display a message saying "Phone number is: " but when it comes up as blank.
Dim intPhoneNumber As String
intPhoneNumber = txtPhone.Text
lblResults.Text = "Phone Number Is: " & intPhoneNumber
-
Dec 15th, 2017, 11:47 PM
#2
Re: How to display numbers from textbox objects to labels.
The only explanation is that, when that code is executed, the specified TextBox is empty. Either you're referring to the wrong TextBox or you're executing the code at the wrong time.
On an unrelated note, I hate Hungarian Notation at the best of times but if you don't use it properly then it's worse than useless. Why do you have A String variable with an "int" prefix? If you're not going to use the proper prefix for the type then don't use prefixes at all. Otherwise your code actually becomes misleading.
-
Dec 16th, 2017, 12:08 AM
#3
Thread Starter
New Member
Re: How to display numbers from textbox objects to labels.
Sorry, I am new to VB coding and trying to figure it out.
-
Dec 16th, 2017, 02:53 AM
#4
Re: How to display numbers from textbox objects to labels.
I'd suggest setting a breakpoint on that code, which you can do with the F9 key. That will show you when it gets executed. If it does get executed at the right time, you can then test the contents of that TextBox at the time.
-
Dec 16th, 2017, 06:15 AM
#5
Re: How to display numbers from textbox objects to labels.
Where do run that code? It needs to be in the click event of your button.
-
Dec 16th, 2017, 01:48 PM
#6
Thread Starter
New Member
Re: How to display numbers from textbox objects to labels.
So the program I'm running displays text box's for the user to input information and when the submit button is clicked it displays that information. I just need to know how to display numbers that are inputted to display in the message.
Code:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim strFirstName As String
Dim strLastName As String
Dim strEmail As String
Dim intLabNumber As Integer
Dim intStationNumber As Integer
Dim intPhoneNumber As String
strFirstName = txtFname.Text.Trim
strLastName = txtLname.Text.Trim
strEmail = txtEmail.Text.Trim
intPhoneNumber = txtPhone.Text
lblResults.Text = "An order has been placed for: " & strFirstName & " " & strLastName & "<br>" _
& "Email Address: " & strEmail & "<br>" _
& "Phone Number: " & intPhoneNumber & "<br>" _
& "Lab Number " & intLabNumber & " and station Number " & intStationNumber
End Sub
Last edited by Shaggy Hiker; Dec 16th, 2017 at 01:58 PM.
Reason: Added CODE tags
-
Dec 16th, 2017, 02:04 PM
#7
Re: How to display numbers from textbox objects to labels.
I edited your post to add [CODE][/CODE] tags, which makes the code more readable. You can do this by pressing the # button and pasting the code between the resulting tags.
<br> would be an HTML element, which makes me wonder if this whole thing is part of an ASP.NET question, in which case it isn't really in the right forum. However, you may well have at least one problem. A person entering a phone number is entering a string, not an integer. You have that string being implicitly converted to an integer, but that won't necessarily work. If the user enters spaces, hyphens, or parenthesis, all of which may happen in a phone number, then that implicit conversion will crash with an Invalid Conversion Exception. You need the phone number to be a string. If you want to validate it, use RegEx for that, but leave it a string, or else you will force the user to enter as you want...or crash.
My usual boring signature: Nothing
-
Dec 16th, 2017, 02:18 PM
#8
Thread Starter
New Member
Re: How to display numbers from textbox objects to labels.
Thanks!! That was extremely helpful! Everything works fine now.
-
Dec 16th, 2017, 09:45 PM
#9
Re: [RESOLVED] How to display numbers from textbox objects to labels.
Shaggy's post proves what I said earlier: your code is misleading. Shaggy is under the impression that the text entered by the user is being implicitly converted to an Integer because of the very fact that the variable you assign it to is named as though it is type Integer but it is not. I already pointed that out and yet you haven't changed your code and now exactly what I said would happen has happened. Look at your own code:
Code:
Dim strFirstName As String
Dim strLastName As String
Dim strEmail As String
Dim intLabNumber As Integer
Dim intStationNumber As Integer
Dim intPhoneNumber As String
Do you not see the issue there?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|