简体   繁体   中英

c# how to unlock multiple text boxes

I would like to unlock majority, but not all text boxes on the form. Currently I'm using this method that unlocks all text boxes:

    private void UnlockVnos(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).ReadOnly = false;
                ((TextBox)c).BackColor = Color.FromArgb(255, 255, 192);
            }
        }
    }

My question is: how can I exclude particular text boxes that I don't want to be unlocked (I have to loop through some 50 text boxes and unlock all of them, except some 10 of them that have to stay locked. The first thing that came to my mind is to set the text boxes 'Tag' property, but somehow I can't make it work in my method. Any help appreciated.

For this, I set the textbox tags of the boxes I did not want to unlock to "DoNotUnlock".

        foreach (Control c in this.Controls)
        {
            if (c is TextBox && (string)c.Tag != "DoNotUnlock")
            {
                ((TextBox)c).ReadOnly = false;
                ((TextBox)c).BackColor = Color.FromArgb(255, 255, 192);
            }
        }

With a dictionary of keys. I'm going for a Dictionary for more versatility, where the Bool could be used in combination with the key. A code sample will be added for that shortly.

    private void UnlockVnos()
    {
        Dictionary<string, bool> mytags = new Dictionary<string, bool>();
        mytags.Add("DoNotUnlock", false);
        mytags.Add("StayAwayFromThisBox", false);
        mytags.Add("DontEvenDateUnlockThis", false);

        foreach (Control c in this.Controls)
        {
            if ((c is TextBox && c.Tag == null || !mytags.Keys.Contains((string)c.Tag)))
            {
                ((TextBox)c).ReadOnly = false;
                ((TextBox)c).BackColor = Color.FromArgb(255, 255, 192);
            }
        }
    }

3rd sample using the bool in the dictionary

    private void UnlockVnosAgains()
    {
        //here we have a Dictionary of all the tags you want to handle.
        //True for boxes which should be readonly, false for boxes which should not be.
        Dictionary<string, bool> mytags = new Dictionary<string, bool>();
        mytags.Add("SomeTag1", false);//leave it alone
        mytags.Add("SomeTag2", true);//make it readonly
        mytags.Add("SomeTag3", true);//make it readonly
        mytags.Add("SomeTag4", false);//leave it alone
        mytags.Add("DoNotUnlock", true);//make it readonly

        foreach (Control c in this.Controls)
        {
            //if C is a textbox, and the Tag is NOT null and the dictionary contains the tag
            if ((c is TextBox && c.Tag != null && mytags.Keys.Contains((string)c.Tag)))
            {
                ((TextBox)c).ReadOnly = mytags[(string)c.Tag];//assign the appropriate bool from the dictionary
                ((TextBox)c).BackColor = Color.FromArgb(255, 255, 192);//do your color thing... wink wink, this one could be stored along with your true or false too
            }
        }
    }

I assume that you have same tag value for all of the textboxes you dont want to unlock.

Try following:

private void UnlockVnos(Control control)
{
    foreach (Control c in control.Controls)
    {
        if (c is TextBox)
        {
            var tBox = (TextBox)c;
            var tag = Convert.ToString(tBox.Tag);

            if (tag !="YourTagValue")){ //Take care of case-sensitivity
               tBox.ReadOnly = false;
               tBox.BackColor = Color.FromArgb(255, 255, 192);
            }
        }
    }
}
private void UnlockVnos(Control control)
{
    foreach (Control c in control.Controls)
    {
        if (c is TextBox)
        {
            var textbox = (TextBox)c;

            textbox.ReadOnly = textbox.Tag == "myTag";
            textbox.BackColor = Color.FromArgb(255, 255, 192);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM