PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 [RESOLVED] this code isn't doing what it's suppose to (easy fix)-VBForums
Results 1 to 23 of 23

Thread: [RESOLVED] this code isn't doing what it's suppose to (easy fix)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Resolved [RESOLVED] this code isn't doing what it's suppose to (easy fix)

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If Me.TextBox1.Text = "" Then
                MessageBox.Show("You didn't add a level")
            Else : Form1.Button1.Text = Me.TextBox1.Text
                Close()
            End If
        End Sub
    this only shows first letter of entered text in textbox, also how can I make this so you can only type in two digit numbers into the textbox? and no letters ect.

    and is there something you can type in quotes which means any number/letter or anything in general as long as there is at least 1 letter/digit in a textbox to allow/disallow something?

    edit: Font was too big for the button to fit on. Lol.

    also used maskedtoolbox
    Last edited by MattyR; Feb 12th, 2012 at 02:30 PM.

  2. #2
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: this code isn't doing what it's suppose to (easy fix)

    you need to add your business logic to say that if the textbox1 text length if less than 2 characters, then prompt an error
    if you are not wanting any letters, then go through each character and see if it is a digit using Char.IsDigit

    example:

    for each currentChar as Char in Me.TextBox1.Text
    if not Char.IsDigit(currentChar)
    MessageBox.Show("Character not a digit")
    end if
    next


    you then make a contridication in your last sentence. do you want to only allow numbers or any text?

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,486

    Re: this code isn't doing what it's suppose to (easy fix)

    If you want to limit the number of characters the user can enter into a TextBox then you should set the MaxLength property. You should already know that because, if you're using a TextBox, you should have read the documentation for the TextBox class to see what properties, methods and events it has. If you don't look then you'll never find.

    As for limiting what the user can type into the control, you might like to follow the CodeBank link in my signature and check out my Numeric Text Box thread. You may be able to use the code as is or adapt it to your needs if it's not exactly what you want.

  4. #4
    Member
    Join Date
    Jan 2012
    Posts
    32

    Re: this code isn't doing what it's suppose to (easy fix)

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If Me.TextBox1.Text = "" Then
                MessageBox.Show("You didn't add a level")
            Else : Form1.Button1.Text = Me.TextBox1.Text
                Close()
            End If
        End Sub
    try rewriting it as such and see if it works

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.TextBox1.Text = "" 
                 if not Char.IsDigit(currentChar)
                 then.me.textbox("You didn't add a level")
                 Close()
            End If
        End Sub
    I think it would work.

  5. #5
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: this code isn't doing what it's suppose to (easy fix)

    no. you have not declare currentChar in your snippet so there will be a compile time error.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  6. #6
    Member
    Join Date
    Jan 2012
    Posts
    32

    Re: this code isn't doing what it's suppose to (easy fix)

    Quote Originally Posted by Techno View Post
    no. you have not declare currentChar in your snippet so there will be a compile time error.
    Do you mind coming to look at my code? I'm very new to this coding thing so maybe you coils point me in the right direction.

    http://www.vbforums.com/showthread.php?t=668965

  7. #7
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: this code isn't doing what it's suppose to (easy fix)

    Quote Originally Posted by Matthew.Kaulfers View Post
    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            If Me.TextBox1.Text = "" Then
                MessageBox.Show("You didn't add a level")
            Else : Form1.Button1.Text = Me.TextBox1.Text
                Close()
            End If
        End Sub
    try rewriting it as such and see if it works

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.TextBox1.Text = "" 
                 if not Char.IsDigit(currentChar)
                 then.me.textbox("You didn't add a level")
                 Close()
            End If
        End Sub
    I think it would work.
    This code has many boo-boo's. It clears the textbox then checks if currentChar (which is nothing) is a digit. Then uses then.me.textbox... WHat the heck is that?
    Close(). What are you both trying to do with that?
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  8. #8
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: this code isn't doing what it's suppose to (easy fix)

    It sounds like you could use the MaskedTextBox control.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  9. #9
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: this code isn't doing what it's suppose to (easy fix)

    You can use the KeyDown event of the textbox and detect if the key pressed is numerical. If not, suppress it!

    vb Code:
    1. If (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) Or (e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9) Then
    2.     'Numerical key
    3.     e.SuppressKeyPress = False
    4. Else
    5.     'Other key
    6.     e.SuppressKeyPress = True
    7. End If
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  10. #10
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: this code isn't doing what it's suppose to (easy fix)

    I believe that it is a bit of an overkill to "inspect" the key pressed and allowing it to be accepted or not. For example you may use 1 textbox for "every type of input" but depending on the current type of input (think of navigating to sections on a form) you would do the correct validation process i.e telephone number input, address input, name input, age input etc...
    the scenario here could get complex/flexible so checking it on every key press/stroke would be an overkill than just checking it right at the end of the input via button click for example

    Other suggestions are slightly better.
    you can use the masked textbox, set the maxlength property and finally on text changed (or after button click), to see if the characters entered are a digit or not
    Last edited by Techno; Jan 5th, 2012 at 05:28 PM.

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  11. #11
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Re: this code isn't doing what it's suppose to (easy fix)

    Quote Originally Posted by Techno View Post
    I believe that it is a bit of an overkill to "inspect" the key pressed and allowing it to be accepted or not. For example you may use 1 textbox for "every type of input" but depending on the current type of input (think of navigating to sections on a form) you would do the correct validation process i.e telephone number input, address input, name input, age input etc...
    the scenario here could get complex/flexible so checking it on every key press/stroke would be an overkill than just checking it right at the end of the input via button click for example

    Other suggestions are slightly better.
    you can use the masked textbox, set the maxlength property and finally on text changed (or after button click), to see if the characters entered are a digit or not
    You're right: use a MaskedTextBox and set the mask to '00'.

    I rarely use MaskedTextboxes and thought it was only for passwords; didn't know that kind of validation was possible. I will definitely be using them in the future!
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  12. #12
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: this code isn't doing what it's suppose to (easy fix)

    indeed. you can also create your own custom mask to suit your needs

    http://msdn.microsoft.com/en-us/libr...edtextbox.aspx

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Re: this code isn't doing what it's suppose to (easy fix)

    only problem then is that, I know i'm being EXTREMELY picky here but I like perfection to my standards... the one problem is that the maskbox then as two spaces which you have to delete or highlight before you can enter numbers, which I don't like lol sorry to be fussy i'm sure it's a simple fix, i've already tried things like on maskedbox1_click text = nothing or text = "" but that didn't work, also tried form2_load+activated = maskbox1.text = "" or nothing but they didn't work, any ideas guys?

  14. #14
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: this code isn't doing what it's suppose to (easy fix)

    How are you stting up the maskTextBox? With mask "00" it should put two underscores on the textBox but when you enter the cursor is in the start of the input area and you can type the numbers right there. You should not need to delete anything.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Re: this code isn't doing what it's suppose to (easy fix)

    hi kaliman

    it puts spaces instead of underscores and I cant type anything in the field unless I delete the spaces

  16. #16
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: this code isn't doing what it's suppose to (easy fix)

    what's wrong with using a numeric up down control? It only accepts numeric values, you can set the maximum property to 99 to set the number of digits allowed to be entered to only 2 (covers 00 - 99) without the need for any input validation code or messing around with text masks.
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Re: this code isn't doing what it's suppose to (easy fix)

    ahhh I like your thinking, I might consider that, But for now I will just use the masktextbox as i'm curious to find out the fix for this =p

  18. #18
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: this code isn't doing what it's suppose to (easy fix)

    It should not be doing that. Without looking at your project I tend to think some of the tweaking you have done in the load, or activate events or the like have messed up things. Do this to check please: Just add a new MaskedTextBox, change its mask to "00", that is double zero without the quotes, Run you app and see if it does what the other one is doing. It should just put two underscores on the TextBox, put the cursor at the start of it, and let you type any digits you want. (only two).
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Re: this code isn't doing what it's suppose to (easy fix)

    Yeahh I know why it does that now, I didn't like the underscores being there lol, so I changed to spaces haha

  20. #20
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: this code isn't doing what it's suppose to (easy fix)

    LOL, but even if you change the PromptChar to a space, your cursor should be at the start and let you type the digits over the spaces. (20 posts, so much for an easy fix lol)
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  21. #21

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Re: this code isn't doing what it's suppose to (easy fix)

    lol no no this is going wayyyyyyy off topic, the original question is surely an easy fix... which I don't think I got answered lol but i'm going to need an answer to that soon though as I'm about to start working on that section lol.
    But yeah if its a space and you click on the box it always starts at the end although I found that if I insert maskedtextbox1.text = nothing into the click event it put the cursor to the front doesn't delete the spaces tho ;\

  22. #22
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    103,486

    Re: this code isn't doing what it's suppose to (easy fix)

    You've ignored the easy fix. Did you read post #3?

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Posts
    89

    Re: this code isn't doing what it's suppose to (easy fix)

    Yeah I did read it, infact at that time I already had a textbox in my form and it was already set to a maxlength property lol, the first thing I did was reviewed the properties.

    But about checking your thread, I did click the link but didn't attempt to find it, as I thought it was a broken link and took me to the visual basic forum but now i've just realised it doesn't and im reading your thread on numeric text boxes xD.

    ps. the easy fix was the code part lol refer to "this only shows first letter of entered text in textbox" which is right after the code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width