PASSING TEXTBOX and VARIABLES?!?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lcm84
    New Member
    • Jun 2009
    • 3

    #1

    PASSING TEXTBOX and VARIABLES?!?

    Basically what I am trying to do is cut down the amount of code in my project.

    I have a TextChanged function that is used by all my textboxes. It is called when the user changes anything in any of the text boxes (appoximately 5 per form). Within this _TextChanged function, I am doing additional verification to ensure the user input is not garbage data.

    Code:
            private void verify_KeyPress(object sender, KeyPressEventArgs e)
            {
    //ensures values are only numbers (no periods, letters, special char)
                if (!((e.KeyChar == 48) || (e.KeyChar == 49) ||
    
                    (e.KeyChar == 50) || (e.KeyChar == 51) || (e.KeyChar == 52) ||
    
                    (e.KeyChar == 53) || (e.KeyChar == 54) || (e.KeyChar == 55) ||
    
                    (e.KeyChar == 56) || (e.KeyChar == 57) || (e.KeyChar == 127) ||
    
                    (e.KeyChar == 8)))
    
                    e.Handled = true;
            }
    
    
            private void verify_TextChanged(object sender, EventArgs e)
            {
                textBox1.KeyPress += new KeyPressEventHandler(verify_KeyPress);
    
       [B]         if (textBox1.Text != "")
                {
                    int value = int.Parse(textBox1.Text);
                    if (value >= 0 && value <= 255)
                    {
                        Variables.familyFire = value;
                        textInLabel(value);
                    }
                    else
                    {
                        Variables.familyFire = 0;
                        textBox1.Text = "";
                    }
                }[/B]        }
    I have to write that if statement (if (textBox1.Text) ) for every single text box. Unless I can somehow pass the variable.family Fire and the textBox1.Text to a function.

    like

    Code:
    public void WONTWORK (object varName, object txtBxName)
    {
    if (txtBxName.Text != "")
    {
    int value = int.Parse(txtBxName.Text);
    varName = val.ValidateIp(value);
    if (varName == 0)
    {
    txtBxName.Text = "";
    }

    and call it in my _TextChanged like...
    WONTWORK(textBo x1, Variables.famil yFire);


    ANY IDEAS?!?!
    Last edited by tlhintoq; Jun 5 '09, 09:51 PM. Reason: [CODE] ... your code here ... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You are already passing the textbox to the function.
    Code:
    verify_TextChanged(object [B]sender[/B], EventArgs e)
    The sending object is the control that called the method. In your case, a textbox.

    try this.

    Code:
    if (((textbox)sender).Text != "")
    Casting the sender from an object to a textbox will do the trick for you. Now any textbox that calls this method can be checked for "". Though personally I prefer
    Code:
    if (((textbox)sender).Text != string.empty)
    .

    While you are reducing code.

    Had you considered this...
    Code:
    (!((e.KeyChar == 48) || (e.KeyChar == 49) ||
     
                    (e.KeyChar == 50) || (e.KeyChar == 51) || (e.KeyChar == 52) ||
     
                    (e.KeyChar == 53) || (e.KeyChar == 54) || (e.KeyChar == 55) ||
     
                    (e.KeyChar == 56) || (e.KeyChar == 57) || (e.KeyChar == 127) ||
     
                    (e.KeyChar == 8)))
    ...is just fugly, hard to update, and not very re-usable?

    What about populating a List<int> with your illegal characters, then checking against that?

    Create a class wide property outside of any method
    Code:
    public List<int> illegals = new List<int>();
    In your class constructor populate the list
    Code:
    illegals.AddRange(new int[]{48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 8});
    And reduce your check to this

    Code:
    if (!illegals.Contains((int)e)) e.handled = true;
    You can now have that same check in a lot of different methods since the List "illegals" is available to your entire class.

    Comment

    • lcm84
      New Member
      • Jun 2009
      • 3

      #3
      Thanks, your advice worked perfectly!!! and I did end up streamlining the key_press function.
      -Laura

      Comment

      Working...