Using the name of a textbox obtained in a function

Ismael

Member
Joined
Mar 20, 2020
Messages
18
Programming Experience
1-3
Hi, folks.
I have a form with many textboxes, some for strings and some for integers. I want to create a function that, when the user tries to save the data, tests the contents of the numeric textboxes to be sure to pass correct values to the DB. The function I made was the following, to test only one textbox:
C#:
private bool TestAll(ref string _FieldName)
{
    if (!int.TryParse(tb_Num.Text, out int Test))
    {
        _FieldName = "tb_Num";
        return false;
    }
    return true;
}
What I want to do, if the textbox contains other than numbers is to set focus on this textbox and send a message to the user. My problem is that I don't know how to use the variable _FieldName contentes to focus it.
Can anybody help me?
Thanks.
 
Last edited by a moderator:
Every one of your topics, you neglect to using code tags. Please use code tags when posting to the forum.

CodeTags.gif


You don't need all of that irrelevant code.

You only need to use the events for Give focus and Lost focus.

Use int.TryParse(tb_Num.Text, out int Test)

Depending what the data type for _FieldName is, depends solely on what out int Test should be.

Google search : int.TryParse MSDN

I would expect someone with 1-3 years experience to know the answer to your topic.
 
In other words: Suppose I have fields Tb_1, Tb_2 and Tb_3. Suppose the FieldName variable contains the value "Tb_2". How can I use the FieldName variable to access the Tb_2 textbox?
That's my point.
 
By calling focus on the control...
TextBox Tb2 = new TextBox();
Tb2.Focus();

Appears I've overlooked your post. Appologies, I've been running 2 days without sleep due to a heavy schedule. I've been using the forums as a means to escape continuous working, when I should really just sleep.

I wasn't paying attention to your parameter...

You don't need a bool function. Just use void. Change string to control. Call focus on the control, pass in the control and not a string value :
C#:
        private void TestAll(Control _FieldName)
        {
            if (!int.TryParse(_FieldName.Text, out int Test))
            {
                _FieldName.Focus();
                MessageBox.Show("Non numeric");
            }
        }
Call it with :
TestAll(tb_Num);
 
Last edited:
I too recommend the use of the Validating event. You can hook up multiple controls to a single event handler.
 
As suggested, the Validating event handler is where you validate a control. In the method, you set e.Cancel to true if validation fails and the control will not lose focus until valid data is entered. When it comes time to use the data, call ValidateChildren on the form and that will validate every control, which covers those that never received focus. That method returns a bool that indicates whether validation was successful or not.
 
If you want to create an event handler for multiple controls, select all those controls in the designer, open the Properties window, click the Events button and then double-click the event. After selecting one or more controls, you can also select an existing event handler from the drop-down, if you need to add more controls to the same handler.
 
Admittedly my reply doesn't provide the correct way to validate data since I am not using any validation events. While my reply does still provide the same affect in terms of holding focus on the control you send to the method, my method undermines the proper usage for validation and its respective events.
 
As I said above, I thought you'd prefer to keep your own code, and that's why I done it the way that I did. Of course it works the same, and I've no doubt you settled for my solution, as it requires little change. But It's not the correct way to implement your corrections.

The events mentioned by the folks above are the correct way you should go about implementing your corrections for your code.

Just in case you overlooked John's links, I've brought his post forward for you, as this is why these events exist in the first place. For situations just like this.

The night I posted that code, I was exhausted and shouldn't have posted that as an alternative solution, because its not the right way to go about it despite providing the same outcome as validation events. Being a man of principles, I would prefer you to follow those set by Microsoft's own documentation, and not what I wrote. If you need any help, post back and let us know.

Why not use Control.Validating Event (System.Windows.Forms)
sender parameter identifies the control if you handle multiple controls with same event handler.
Read more about User Input Validation - Windows Forms
 
Sheepings. I've tried the Validating solution also and it worked fine. As both solutions worked, but you tell me that the correction you proposed to my function is not Elegant, I'll try to implement the validating solution. Once again, thanks. And try to get some sleep. :)
 
Back
Top Bottom