Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i need to know how can i do that for every control like textbox and combobox,is there any way to make every tab key pressing and check for control is empty or not is empty then focus on it. and on mouse click also check the selected control is empty or not if empty than also focus on it

What I have tried:

//protected override bool ProcessTabKey(bool forward)
       //{
       //    Control ctl = this.ActiveControl;
       //    if (ctl != null && ctl is ComboBox)
       //    {
       //        ComboBox cb = (ComboBox)ctl;
       //        if (cb.Text.Length == 0)
       //            return true;
       //    }
       //    if (ctl != null && ctl is TextBox)
       //    {
       //        if (txtDocumentNo.Text.Length == 0)
       //            return true;
       //    }
       //    return base.ProcessTabKey(forward); // process TAB key as normal
       //}
Posted
Updated 16-Jun-19 23:51pm
Comments
Richard MacCutchan 30-May-19 7:38am    
That code will not even compile as it is all commented out. Try and compile it and then explain what happens when you run your application.
Member 13750004 30-May-19 8:49am    
Yes, it works but it can not work for mouse click if I click on any other control which is chooses by mouse click
Richard MacCutchan 30-May-19 9:58am    
You need to show the code and explain exactly what happens.
[no name] 30-May-19 9:14am    
If this code is working did you tried calling this method on mouse events?
j snooze 30-May-19 17:11pm    
why wouldn't you want to do what you do with the combo box on the text box. Wouldn't you rather have it
Textbox tx = (Textbox)ctl;
if (tx.Text.Length == 0)....

Why have a specific textbox name?

1 solution

If you're talking about data validation, you can use ErrorProvider Class (System.Windows.Forms) | Microsoft Docs[^]

For further details, please see: How to: Display Error Icons for Form Validation with the Windows Forms ErrorProvider Component | Microsoft Docs[^]

Note: if there's a set of different rules for each validation, i'd create a dictionary of ErrorProviders for each control you want to validate. For example, if you need to check if entered value is a number, you need to create a "NumberErrorProvider", if you want to check if phone no. is valid, create "PhoneNoErrorProvider", etc...
Finally, you can create custom method (for example: IsFormValid) which steps through the collection ErrorProviders and returns true/false.

C#
public partial class Form1 : Form
{
    private Dictionary<Control, ErrorProvider> validators = new Dictionary<Control, ErrorProvider>();

    public Form1()
    {
        InitializeComponent();

        validators.Add(textBox1, new ErrorProvider(this){//other properties here});
        validators.Add(comboBox1, new ErrorProvider(this){//...});
    }


    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        if (IsValidNumber(textBox1.Text)) //to do: add IsValidNumber method
            validators[textBox1].SetError(textBox1, "Field 'Number' has to be a valid number!");
        else
            validators[textBox1].Clear();
    }

    //other code here....

    private bool IsFormValid()
    {
        foreach (Control c in validators.Keys)
        {
            if(validators[c].GetError(c) !="")
                return false;
        }
        return true;
    }

}


This is a basic concept for WinForms data validatation. You can implement your own.

Good luck!
 
Share this answer
 
Comments
Member 13750004 26-Jun-19 6:17am    
thanks
Maciej Los 26-Jun-19 6:53am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900