[C#]Shortening this little bit ;]

Joined
Jul 26, 2006
Messages
3,626
Reaction score
1,006
I've got 12 checkboxes, each enabling/disabling a textbox (but that isn't the question).
This is the piece of code I need help with:
PHP:
            f1_enable.Enabled = false;
            f2_enable.Enabled = false;
            f3_enable.Enabled = false;
            f4_enable.Enabled = false;
            f5_enable.Enabled = false;
            f6_enable.Enabled = false;
            f7_enable.Enabled = false;
            f8_enable.Enabled = false;
            f9_enable.Enabled = false;
            f10_enable.Enabled = false;
            f11_enable.Enabled = false;
            f12_enable.Enabled = false;

Now when pressing the 'Run' button these checkboxes have to be disabled.
This is quite some code and I think it can be shortened but how?

I also have this:
PHP:
        private void f1_enable_CheckedChanged(object sender, EventArgs e)
        {
            if (f1_enable.Checked == true)
            {
                f1_text.Enabled = true;
            }
            else
            {
                f1_text.Enabled = false;
            }
        }

Once for every F-key I use (so 12 times...) I really want that shortened too.

Thanks,

~Elite.
 
Last edited:
Well, I'm not fantasic at c#, but the way I can think to shorten it would be more cpu intensive (Only by a tiny bit, nobody would ever notice), but is that really the method you want to program? Sacrificing performance just to make the code look shorter?

Cause of the numeric method you named your check boxes, you could use a for loop to disable each one, and as for the enabled, could use one function and, I think you can get the senders name from the sender object variable, and use a string split to get the number, but all in all, I'd stick the way it is, might take a tiny bit longer to compile, and be a little longer to read through, but it'll provice far better performance than either of these mesures, even if not noticable to the human eye.

Also, would
Code:
        private void f1_enable_CheckedChanged(object sender, EventArgs e)
        {
                f1_text.Enabled = f1_enable.Checked;
        }
not work? As checkboxes checked value is a boolean also?


Shortest code is not always the best code.
 
Last edited:
True that ;]
You're way better at c# than I am anyway (from the language you used in your post) :P
Also, please explain how I would go about using a for-loop with the checkboxes...;
PHP:
for (int i = 1; i < 12; i++)
{
    f(insert int i here in some way)_enable.Enabled = false;
}
 
Well, uncertain, but, using something similar to what I use for thread safe changing of form objects, like...
Code:
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            if (oControl.InvokeRequired)
            {
                SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                oControl.Invoke(d, new object[] { oControl, propName, propValue });
            }
            else
            {
                Type t = oControl.GetType();
                PropertyInfo[] props = t.GetProperties();
                foreach (PropertyInfo p in props)
                {
                    if (p.Name.ToUpper() == propName.ToUpper())
                    {
                        p.SetValue(oControl, propValue, null);
                    }
                }
            }
        }

With that it'd go something like...

Code:
[COLOR=#000000][COLOR=#007700]for ([/COLOR][COLOR=#0000BB]int i [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#0000BB]i [/COLOR][COLOR=#007700]< [/COLOR][COLOR=#0000BB]12[/COLOR][COLOR=#007700]; [/COLOR][COLOR=#0000BB]i[/COLOR][COLOR=#007700]++)
{[/COLOR][/COLOR]
SetControlPropertyValue("nameofobject", "Visible", false);
}

Or perhaps this will help you a little.

http://bytes.com/topic/c-sharp/answers/232590-get-object-name

I'd really not suggest looping, I've never done it, so makes it hard to assist.
 
Its called putting your controls in an array.

That can shorten the code to disable them all to a simple:
Code:
for(int i=0; i<12; i++)
    f_enable[i].Enabled = false;

Where F1 is f_enable[0], etc.

Look it up, you can make Control Arrays.
It worked really well in VB6, but in .NET they changed it a little. (Yes C# design is the same as VB. Oh gasp)
 
Last edited:
I'd go with putting them in an array, too. Simple and efficient.

If you're going to ask for something's name at run time, for it to work the compiler will obviously have to dump more metadata (that is, variable names in the least) into the executable. Now I don't know squat about C#, but mixing up programming and run time isn't cool to me.
 
I would go even further, saying that they are probably already in the array. What i mean is those check boxes are on some control ? You can access them through ParentControl.Controls collection if you have something more than those on this control You can always add a filter like is typeof(Checkbox).
 
hym, let it be.

Ok, imagine You have a form, on the form you drop a Panel inside the panel you drop all your check boxes, got it ? We can easily access them, no matter how many of them You will drop example below
Code:
//CheckBox = type, cb = local variable, Panel1 = it's your panel, Panel1.Controls = collection ( array ) of all controls ( btns, check boxes etc wich are dropped inside your pretty panel )
foreach(CheckBox cb in Panel1.Controls) 

{
   cb.enabled = false;
}

If You would like to write it as a method You could possibly do it like that
Code:
public void SetStates(bool state)
{
   foreach(Checkbox cb in Panel1.Controls)
   {
      cb.enabled = state;
   }
}

Forgive me if there are any syntax errors i wasn't writing it in the Visual Studio itself, just directly in post-box.
 
Back