Results 1 to 9 of 9

Thread: [RESOLVED] How to display numbers from textbox objects to labels.

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: How to display numbers from textbox objects to labels.

    Sorry, I am new to VB coding and trying to figure it out.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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.

  5. #5
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    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

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2017
    Posts
    8

    Re: How to display numbers from textbox objects to labels.

    Thanks!! That was extremely helpful! Everything works fine now.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    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
  •  



Click Here to Expand Forum to Full Width