-2

i have a button in my form and for every button click it is adding groupBox. but i want a winform contain only 8 groupboxes. when the numbers of groupBox reach 8 it will automatically do Visible=false all 8 before groupBox and again adds a groupBox named(groupBox9). what must i do?

private void butonYeni_Click(object sender, EventArgs e)
{
        //creating Font
        Font font = new Font("Microsoft Sans Serif", 10.0f, FontStyle.Bold);
        Font font2 = new Font("Microsoft Sans Serif", 9.0f, FontStyle.Bold);

        int sayGB = 0;

        foreach (Control c in this.Controls)
        {
            if (c.GetType() == typeof(GroupBox))
            {
                sayGB++;
            }



            for (int i = sayGB; i < 1000; i++)
            {
                //creating groupbox
                GroupBox Group = new GroupBox();
                Group.Width = 767;
                Group.Height = 179;
                Group.Text = "Soru & Cevap";
                Group.Font = font;
                Group.ForeColor = Color.Maroon;
                Group.Location = new Point(200,66);
                //creating label
                Label Soru = new Label();
                Soru.Text = "Soru: ";
                Soru.Font = font2;
                Soru.ForeColor = Color.Maroon;
                Soru.Location = new Point(6,33);
                Soru.Width = 53;
                Soru.Height = 13;
                //creating textbox
                TextBox soruText = new TextBox();
                soruText.Width = 685;
                soruText.Height = 20;
                soruText.Font = font2;
                soruText.ForeColor = Color.Black;
                soruText.Multiline = true;
                soruText.Location = new Point(70,31);
                //creating label
                Label Cevap = new Label();
                Cevap.Text = "Cevap:";
                Cevap.Font = font2;
                Cevap.ForeColor = Color.Maroon;
                Cevap.Location = new Point(6, 92);
                Cevap.Width = 53;
                Cevap.Height = 25;
                //creating textbox
                TextBox cevapText = new TextBox();
                cevapText.Width = 685;
                cevapText.Height = 69;
                cevapText.Font = font2;
                cevapText.ForeColor = Color.Black;
                cevapText.Multiline = true;
                cevapText.Location = new Point(70,67);
                //creating button
                Button btn = new Button();
                btn.Width = 75;
                btn.Height = 25;
                btn.Text = "Kaydet";
                btn.BackColor = Color.Maroon;
                btn.Font = font2;
                btn.ForeColor = Color.White;
                btn.Location = new Point(682,148);
                //kontrolleri ekleme
                Group.Controls.Add(btn);
                Group.Controls.Add(Soru);
                Group.Controls.Add(soruText);
                Group.Controls.Add(Cevap);
                Group.Controls.Add(cevapText);
                this.Controls.Add(Group);
        }
    }
}
Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58
paradoxIST
  • 31
  • 7
  • You mean as soon as you go to add a 9th group box you want to hide the previous 8? show your code (`btn_click` in particular) – Sayse Jun 10 '14 at 13:00
  • 2
    Show the code you did and tell what's the problem. – Gusman Jun 10 '14 at 13:00
  • i cant add my code because the reputation problem, but i will tell how i count the group box. with "For Each" i can count the type of groupBox (Control c in this.control and then if c.gettype()==typeof(GroupBox) then with int Counter=0 Counter++) it gives me number of GroupBoxes. i can name the groupBoxes like group.name="Group"+Counter; but the problem is i can add until 8 groupBoxes to a winform but when 9th click it is adding groupBox On it – paradoxIST Jun 10 '14 at 13:23
  • Underneath the tags on your question there is an "edit" button, you don't need any reputation to use it – Sayse Jun 10 '14 at 13:28
  • yeah that won't work. You (usually) can not change a collection while you are enumerating on it (meaning, you can't change a collection inside a foreach loop that is enumerating that collection). Maybe you could describe what you want your code to do in the end. – Steffen Winkler Jun 11 '14 at 08:03

1 Answers1

0

define an integer class variable and increase it everytime the button is clicked. After increasing it, check if it's greater than 8. If it is, set your stuff to Visible = false.

Mock up:

public class MyClass
{
      private int groupboxCounter = 0;
      public MyClass()
      {
      }

      private void btn_click(...)
      {
           // add a new groupbox here
           groupboxCounter++;
           if (groupboxCounter > 8)
           {
                //make stuff invisible here
           } 
      }
}
Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58
  • You shouldnt need to add any new variables, just get the count of groupboxes on the form. – Sayse Jun 10 '14 at 13:12
  • @Sayse I'm not sure if he keeps track of them and/or if there are other elements on his form. So just counting children could get kind of complicated. With the information available that's the only 'easy' answer I could come up with. – Steffen Winkler Jun 10 '14 at 13:15
  • 1
    The OP doesn't need to keep track of them, the form does that. `this.Controls.OfType().Count()` – Sayse Jun 10 '14 at 13:16
  • thanks for answer my question but i want to ask your a question; for 9th groupBox;what must i write the if event? – paradoxIST Jun 10 '14 at 13:26
  • @Sayse huh, neat. Haven't heared of that function before. Thank you for sharing. – Steffen Winkler Jun 10 '14 at 13:33
  • @paradoxIST uh, what? – Steffen Winkler Jun 10 '14 at 13:34
  • i can count the number of groupBoxes even naming each of them like GroupBox+Counter but my problem is when number of groupbox reach 8 it will all of 8 groupbox visible and add 9th groupbox to top-left again. how can i achive this? – paradoxIST Jun 10 '14 at 14:02
  • i didnt add naming of groupBox because if you have a solution on the code you change the coding.. – paradoxIST Jun 10 '14 at 14:04
  • @paradoxIST please edit your question according to what you actually want to know. – Steffen Winkler Jun 10 '14 at 19:01