imnoobcoder
imnoobcoder

Reputation: 23

How to open items one by one

I have this code:

private void button3_Click(object sender, EventArgs e)
    {
        textBox3.Visible = true;
        textBox4.Visible = true;
        textBox5.Visible = true;
        textBox6.Visible = true;
    }

When I press the button3 all textboxes open.
But I want them to open one by one.

Upvotes: 1

Views: 90

Answers (3)

Idle_Mind
Idle_Mind

Reputation: 39132

Here's another way you could approach this.

Nice and compact:

private void button3_Click(object sender, EventArgs e)
{
    TextBox[] TBs = { textBox1, textBox2, textBox3, textBox4 };
    TextBox tb = TBs.Where(x => !x.Visible).FirstOrDefault();
    if (tb != null) { tb.Visible = true; }
}

Upvotes: 1

EgoPingvina
EgoPingvina

Reputation: 823

Example for my comment:

public partial class Form1 : Form
{
    private List<TextBox> textBoxes;

    private int iterator = 0;

    public Form1()
    {
        InitializeComponent();

        this.textBoxes = new List<TextBox>
            {
                this.textBox1,
                this.textBox3,
                this.textBox2,
                this.textBox4
            };
        this.textBoxes.ForEach(tb => tb.Visible = false);
    }

    private void buton_Click(object sender, EventArgs e)
    {
        if (this.iterator >= this.textBoxes.Count())
        {
            // Do something that you need, then all textboxes are visible

            return;
        }

        this.textBoxes[this.iterator++].Visible = true;
    }
}

In ctor you control the order of showing your textboxes by manipulating of them order in textBoxes.

Upvotes: 0

Alaa Mohammed
Alaa Mohammed

Reputation: 392

Use this code :

 private void button3_Click(object sender, EventArgs e)
        {
            
            if (!textBox3.Visible)
            { textBox3.Visible = true; return; }
            if(!textBox4.Visible)
            { textBox4.Visible = true; return; }
            if (!textBox5.Visible)
            { textBox5.Visible = true; return; }
            if (!textBox6.Visible)
            { textBox6.Visible = true; return; }
        }

Upvotes: 1

Related Questions