Skip to content

Passing Current Component to a function

Jonathan

Asked Mar 31, 2009, 5:01 AM EDT3 answers
Share
LinkedInX
Hello I am writting a simple app and I have run into a problem that I can't google the answer to. I am probably asking wrong, dunno. I have multiple textboxes on a tabbed component. I am using the validating command to make sure the user inputted a value and if not input one. There are lots of these text boxes and they all have the same event code that differ only by the component using it. but I can not figure out how to pass a component with functionality to a module so I can reuse the same code and cut down on my repitition. Any advice? I know its simple but with out a data type I can only think of overly complex things. THanks for anything

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    If your using Windows Forms Im assuming its the TextBox control, just create a function which takes a TextBox as an argument and pass in the TextBox object you wish to alter or get data for.

    private void DoSomethingWithTextBox(TextBox textBox)
    {
       // Alter the TextBox object here, its passed by reference automatically so
       // if you change it here it will update the orignal one.
    }
    0
  • 0
    I see how that would work and it is simple.  How do I encorporate this into the validating event?  Do I have to make a module and then call it from the Visual generated event code?
    0
  • 0
    The validating event? Just handle the TextBox.Validating event... something like:

    this.myTextBox.Validating = new System.CancelEventHandler(myTextBox_Validating);

    private void myTextBox_Validating(object sender, CancelEventArgs e)
    {
       // call the validating function in here...
       ValidateTextBoxInput(this.myTextBox);
    }

    Its easiest if in the designer you select the text box, then go to the Properties pane and click the little lightning bolt at the top and scroll down and double click the Validating event and it will attach the event in the Designer.cs code file and create the event handler in your forms code behind. From there you just do the validating.
    0