hey guys,

recently i came across a code to parse the name of an enum and was wondering if there's anything similar to doing it for a textbox:

the coding looks something like this:

int result = (int)Enum.Parse(typeof(Cars.Engines), inputfixed, true);

i would actually like to use the above code structure in a for loop and go through a collection of controls, now i do not want to use foreach(Control ctrl...) because i have 3 controls which i want to go through with each loop and they have to be picked up one after another.

example:
first loop, only go through first column, second loop only go through 2nd column of controls and so on.

what i would like to do is define something like:

for(int i=0; i < something; i++)
{
if txt_from_[i] == something
if txt_to_[i] == something
if chkb_[i] == something
do something
}

Recommended Answers

All 12 Replies

Sure walk through all the components of a form checking to see if their of type textbox.

that's what i do not want to do.

i do not want to use this:

foreach(Control ctrl in pnlSomething.Controls)

i need some way to directly call the text boxes so i can compare their values. maybe i should give an in depth explanation of the problem:

for example:
i have three corresponding controls per column: txt_from_1, txt_to_1, and chkbx_1, txt_from_2 and so on
now when i loop through the controls, i need to get the value of txt_from_1 and compare it immediately with txt_from_1, and then based on this comparison set the value of chkbx_1.

if i use the [foreach] and [is textbox], i'll be looping through the entire control regardless of rows and columns. what i want to is to check the 3 values of each column by calling them directly. but i do not know how to specify a control's name during run time and call it. i cant do something like:

for(int i=0; i < 10; i++)
if(("txt_from" + [i]).Text == ("txt_to" + [i]).Text)

therefore i need someway to make the above happen.

Well, it depends a little on how you made those boxes, if they are premade on your form, then you'd know the names before you started.

if they are generated, you could make an array of textboxes, and just walk the array of text boxes..

yes, the text boxes are created during design. :)

i really wanted to use the particular method i mentioned because it shortens the code and makes it more flexible to code. imagine having to use 8 similar methods to check 8 columns that only differ by the last number. ie: txt_from_1, txt_from_2.

once again i CANNOT loop through the entire control because i need immediate comparison between the controls. what i want to do is to substitute the number at the back with the next control's number after going through one column. ie: txt_from_.

this way i just need to place the controls in a for loop to get their comparison as well as values.

I see what you mean in someways, however, either way you end up with some kind of loop.

if you wanted to change your code so you dont do something like

if (textbox_from_1.Text == textbox_to_1)
if (textbox_from_2.Text == textbox_to_2)
if (textbox_from_3.Text == textbox_to_3)

etc, you would still end up with a loop
i guess you have tried the obvious of such as :

for (int i = 1; i<3; i++)
            {
                if (TextBox)Controls["textbox_from_"+if.ToString()].Text == (TextBox)Controls["textbox_to_"+if.ToString()].Text)
            }

Maybe try

this.Controls.IndexOfKey("txt_to" + i.ToString());

if the index is not -1 then you can use this.Controls and it will be the textbox

no can do. i need to directly call the controls in a for loop and not check them. i'll give you my full code so you'll get what i want to do:

for(int i = 0; i < columnlength; i++)
{
     string x = txt_from_[i].Text; //this is of course illegal, i want something like this though
     string y = txt_to_[i].Text;
     bool yn = chkb_[i].Checked;
    //if string y equals to something, then
    for (int j =0; j < y.length; j++)
    {
        //write boolean n times based on y's length
    }
}

as you can see, i cant achieve this using foreach(Control in pnlSomething.Controls), it'll just perhaps get to txt_from_1, then jump on to txt_from_2 and not to txt_to_1 which i want it to. and i cant do this by checking the index either.

thanks for your replies btw.

To be frank, I don't understand your code on what to be done but hope this would help you...

sample code.

private void loopSample()
{
    foreach (Control ctrl in this.Controls)
    {
        if (ctrl is TextBox)
        {
            if (ctrl.Text.Contains("Hello World"))
            {
                MessageBox.Show(ctrl.Text);
            }
        }
    }
}

yeah i know its hard to understand. lets put it this way then. for example:

this is the arrangement of the text boxes in my form:

txtfrom_1 txtfrom_2 txtfrom_3
txtto_1 txtto_2 txtto_3

lets say we use the foreach(Control ctrl in this.Controls), how would it go? it'll surely do something like this:

txtfrom_1 --> txtfrom_2 --> txtfrom_3 --> txtto_1

what i want is
txtfrom_1 --> txtto_1 --> txtfrom_2 --> txttto_2

thanks.

I showed you that code.

whoops! sorry i overlooked the code. thanks LizR!

Hey LizR, when I try what you suggested, I get the following error:

Error 1 'System.Windows.Forms.Control' is a 'type' but is used like a 'variable'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.