Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Dynamic Checkboxes with a eventhandler

$
0
0
Hey!

I need some help..
I have created two panels.. One with dynamic checkboxes and one with other dynamic panels in it..

Im trying to create that if dynamic checkbox.check == true. Then it creates a dynamic panel in the other panel.
The problem comes when the first checkbox and third checkbox are checked. Then it gets a space between them..
I want them after each other..


    

public void HittaAllaJobb() { lstChckBox = new List<CheckBox>(); for (int i = 0; i < test.Count(); i++) { box = new CheckBox(); box.Name = i.ToString(); box.Font = new Font(box.Font.FontFamily, 8); box.AutoSize = true; box.Text = test[i].Name; box.Location = new Point(0, (i * 25)); box.CheckedChanged += new EventHandler(Checkbox_Click); lstChckBox.Add(box); CheckboxPanel.Controls.Add(box); } } void Checkbox_Click(object sender, EventArgs e) { for (int i = 0; i < lstChckBox.Count(); i++) { if (lstChckBox[i].Checked == true) { panel = new Panel(); panel.Name = "VirituellPanel " + i; panel.AutoSize = true; panel.Size = new Size(249, 80); panel.Location = new Point(0, (i * 100)); panel.BorderStyle = BorderStyle.FixedSingle; SchemalaggareInfoPanel.Controls.Add(panel); lblRubrik_1 = new Label(); lblRubrik_1.Name = "VirituellLblRubrik1 " + i; lblRubrik_1.Font = new Font(box.Font.FontFamily, 8); lblRubrik_1.AutoSize = true; lblRubrik_1.Text = "Nästa körning är:"; lblRubrik_1.Location = new Point(0, 0); panel.Controls.Add(lblRubrik_1); lblRubrik_2 = new Label(); lblRubrik_2.Name = "VirituellLblRubrik2 " + i; lblRubrik_2.Font = new Font(box.Font.FontFamily, 8); lblRubrik_2.AutoSize = true; lblRubrik_2.Text = "Senaste körning:"; lblRubrik_2.Location = new Point(0, 25); panel.Controls.Add(lblRubrik_2); lblRubrik_3 = new Label(); lblRubrik_3.Name = "VirituellLblRubrik3 " + i; lblRubrik_3.Font = new Font(box.Font.FontFamily, 8); lblRubrik_3.AutoSize = true; lblRubrik_3.Text = "Status:"; lblRubrik_3.Location = new Point(0, 50); panel.Controls.Add(lblRubrik_3); var task = ts.GetTask(test[i].Name); string StatusText = ""; lblVärde_1 = new Label(); lblVärde_1.Name = "VirituellLblVärde1 " + i; lblVärde_1.Font = new Font(box.Font.FontFamily, 8); lblVärde_1.AutoSize = true; lblVärde_1.Text = task.NextRunTime.ToString(); lblVärde_1.Location = new Point(lblRubrik_1.Location.X + lblRubrik_1.Size.Width, lblRubrik_1.Location.Y); panel.Controls.Add(lblVärde_1); lblVärde_2 = new Label(); lblVärde_2.Name = "VirituellLblVärde2 " + i; lblVärde_2.Font = new Font(box.Font.FontFamily, 8); lblVärde_2.AutoSize = true; lblVärde_2.Text = task.LastRunTime.ToString(); lblVärde_2.Location = new Point(lblRubrik_2.Location.X + lblRubrik_2.Size.Width, lblRubrik_2.Location.Y); panel.Controls.Add(lblVärde_2); if (task.IsActive == true) { StatusText = "Aktiverad"; } else { StatusText = "Inaktiverad"; } lblVärde_3 = new Label(); lblVärde_3.Name = "VirituellLblVärde3 " + i; lblVärde_3.Font = new Font(box.Font.FontFamily, 8); lblVärde_3.AutoSize = true; lblVärde_3.Text = StatusText; lblVärde_3.Location = new Point(lblRubrik_3.Location.X + lblRubrik_3.Size.Width, lblRubrik_3.Location.Y); panel.Controls.Add(lblVärde_3); pictureBox = new PictureBox(); pictureBox.Size = new Size(20, 20); pictureBox.Location = new Point(lblVärde_3.Location.X + lblVärde_3.Size.Width + 5, lblVärde_3.Location.Y - 3); if (task.IsActive == true) { pictureBox.Image = Resources.GreenPicture; } else { pictureBox.Image = Resources.Gul; } panel.Controls.Add(pictureBox); }

                else if(lstChckBox[i].Checked == false)
                {
                    Control ctrl;
                    for (int a = SchemalaggareInfoPanel.Controls.Count - 1; a >= 0; a--)
                    {
                        ctrl = SchemalaggareInfoPanel.Controls[a];
                        if (ctrl.Name == "VirituellPanel " + i)
                        {
                            SchemalaggareInfoPanel.Controls.RemoveAt(a);
                        }
                    }
                }

} }




Ask if you dont understand..



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>