none
read only two correct characters. RRS feed

  • Question

  • This method helps me to read with a scanner, he only needs to read the ones that begin with "3S" and "3s",

    remove these two characters ("3S") and copy the rest of the reading. a different code,

    that does not bring "3S", what I need is that it reads the reading in the textbox,

    but it does not bring the condition that erases the reading and says "incorrect code".

    private void opcion1(TEXTBOX txt) { string s = txt.Text; string a = "3S"; string b = "3s"; if (s != "") { if (s.Length >= 2) { if (s.StartsWith(a) || s.StartsWith(b)) { if (s.Length > 2) { //MessageBox.Show(s.Length.ToString()); s = s.Substring(2); } else s = ""; } txt.Text = s; } }

    }


    • Edited by Ronald_11 Thursday, May 24, 2018 10:58 PM error de escritura
    • Moved by CoolDadTx Friday, May 25, 2018 4:31 PM Winforms related
    Thursday, May 24, 2018 7:22 PM

All replies

  • Seems reasonable to me, if a little verbose. If the value in the textbox starts with 3S then you are stripping it out and setting the textbox back to that value. I assume you partially copied code here because the method declaration isn't right but otherwise it seems reasonable.

    If you're getting an error then please post the error. If you're simply not getting the response you expected then post what input you are giving it. Stepping through the code would also reveal what the issue is.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 24, 2018 9:14 PM
  • Basically what I need is that the reading from the scanner be read in the textbox, once read, if this reading brings "3S", it removes it and leaves the rest of the code. If the reading taken does not bring "3S", it will clean the content. In the code shown it removes the "3S", and only leaves the rest of the content but I occupy a condition that does not make me pass another code that does not bring "3S".
    Thursday, May 24, 2018 9:39 PM
  • "but I occupy a condition that does not make me pass another code that does not bring "3S""

    I don't understand what you're trying to say here. Please provide input/output examples to clarify what you want.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 24, 2018 10:01 PM
  • With the scanner I read for "3S20123", the condition says that if in the first two
    characters it brings "3S" the reading is correct, it removes the "3S" and it remains 20123.
    When the code you read is "1P0213", it will not be approved because they are only the
    who bring "3S", delete the code and wait for a new code that brings "3S".
    Thursday, May 24, 2018 10:07 PM
  • Greetings Ronald.

    I'm not sure what you want either, Is this it?

    private void opcion1(TEXTBOX txt) 
    {
    string s = txt.Text;
            string a = "3S";
            string b = "3s";
            string code = "Incorrect code";
    
            if (s != "")
            {
                if (s.Length >= 2)
                {
                    if (s.StartsWith(a) || s.StartsWith(b))
                    {
                        if (s.Length > 2)
                        {
                            //MessageBox.Show(s.Length.ToString());
                            code = s.Substring(2);
                        }
                        else
                            code = "";
                    }
    
                }
            }
    
            txt.Text = code;

    Friday, May 25, 2018 1:53 AM
  • Ok, so if the value doesn't start with 3S you want to wipe the textbox but right now it isn't doing that. Here's a cleaned up version, try it instead.

    private void opcion1 ( Textbox txt )
    {
       string requiredPrefix = "3S";
    
       //Assume you want to give them time to type in the value
       if (txt.Text.Length >= 2)
       {
           if (txt.Text.StartsWith(requiredPrefix, StringComparison.OrdinalIgnoreCase))
              txt.Text = txt.Text.Substring(2);
           else //Wipe the value
              txt.Text = "";
       };   
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 25, 2018 2:25 AM
  • Maybe you mean that you have a TextChanged event handler, which calls your function, and therefore a valid prefixed sequence is then considered invalid and is removed. In this case, you have to disable TextChanged during this call. This can be done with a boolean member.


    Friday, May 25, 2018 5:41 AM
  • It does not work for me, it does not show me anything in the textbox.
    Friday, May 25, 2018 2:44 PM
  • I suspect it isn't the code you posted but rather how you're calling it. Are you calling the posted method whenever the TextChanged event is raised? If so then you're in a catch 22.

    User types in 3 : less than 2 chars so method ignores it

    User types in S : starts with 3S so it is replaced by an empty string

    User types in 5 : less than 2 chars so method ignores it

    User types in 8 : does not start with 3S so it is replaced by an empty string

    To get your method to work you'd need to wait until the user has finished typing the entire value and then run the validation. This would be better handled in the Validating event provided you're not calling it on each text change.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 25, 2018 2:55 PM
  • Yes, this method I am calling every time there is a change in the text changed event. in the code that you publish, each reading in the textbox reads it and analyzes it, if you bring the 3S it removes it and leaves only the remaining code. The only thing that I need is a conditioner that reads the code and analyzes it, if it brings 3S it is correct and if not it brings it to be deleted, always using the code that it publishes.
    Friday, May 25, 2018 4:07 PM
  • Then this is really a winforms issue. You aren't going to be able to call this method when the text changes for the reasons I mentioned earlier. Nothing would ever be valid.

    Your method needs to be called only after the user has entered the entire value and then it should be validated against what they entered and not what the textbox currently contains (otherwise it is invalid again after you've stripped off the prefix).

    I don't know your UI but I might recommend you solve this a different way. Perhaps have the user enter the value and then you simply verify the value behind the scenes and show a valid/not valid indicator so you're never modifying the value directly. In your code you can always (if it is valid) get the "correct" value for programmatic use. This would leave the original value in the UI and likely solve your problem.

    I'm moving this to the Winforms forums so others may be able to provide a better winforms approach.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, May 25, 2018 4:30 PM
  • I agree with Michael ... you definitely do not want to call this code in the TextChanged event handler. The best place would probably be in the Validated event handler. And, as Michael also suggested and I agree with ... you should do something else to indicate an incorrect value ... like maybe set the background color of the TextBox to Red.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Saturday, May 26, 2018 4:37 PM
  • With the scanner I read for "3S20123", the condition says that if in the first two
    characters it brings "3S" the reading is correct, it removes the "3S" and it remains 20123.
    When the code you read is "1P0213", it will not be approved because they are only the
    who bring "3S", delete the code and wait for a new code that brings "3S".

    Are you saying you want 3S20123 to be 20123 and you also want 1P0213 to be 1P0213? Such that only codes scanned that begin with 3S have the 3S removed but all other scanned that do not begin with 3S are left alone in the TextBox?

    Or if you want to discard anything that doesn't begin with 3S that should be simpler.

    Regardless you should scan first and not necessarily use the text changed event for altering the scanned code. There must be more code regarding the use of the scanner to know when it has applied text to the TextBox such that you could then call a sub or function to handle the text in the TextBox perhaps. And perhaps instead use a string variable to work on after the scanner has passed text to a string. Then once the string is corrected place its string in the TextBox.


    La vida loca

    Saturday, May 26, 2018 5:25 PM