changing if statement during runtime when new control is added

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hoadiem
    New Member
    • Mar 2013
    • 5

    changing if statement during runtime when new control is added

    this is for excel .

    I have a form with 2 combobox and 1 textbox

    combo 1 : user can select a column ; let say column A
    combo 2 : user can select an operator ; let say = (equal)
    textbox : user can enter value ; let say 2

    so my code is like

    Code:
    if (column A == tb.text)
    {
        execute code here
    }
    but the problem i have is during run time i allow user to add more controls to the form

    Code:
    if ((column A == tb.text) && (column B == tb2.text)) [B]&& //may be more if user add[/B]
    {
       execute code here 
    }
    how do i handle this 'cause i have no idea how many will the user add to it, and the code should change when user add new control to it.

    if possible , please give me sample code

    much thanks.
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey hoadiem, i assumed number of items in comboBox1 and number of textBoxes are equal in your form... You can use like this,

    Code:
    void Check_Click(object sender, EventArgs e)
    {
        bool flag = true;
    
        var textBoxes = from textBox in this.Controls.OfType<TextBox>()
                                select textBox;
    
        int i = 0;
    
    	foreach (var item in textBoxes)
    	{
    		if (!comboBox1.Items[i].Equals(item.Text))
            {
    			flag = false;
                break;
            }
            
            i++;
        }
    	
        if (flag)
    	{
    		MessageBox.Show("Equal");
    	}
        else
    	{
    		MessageBox.Show("Not Equal");
    	}
    }

    Comment

    Working...