How to put GroupBoxes next to each other - c#

I have dynamically genereted group box with label inside
int dlugoscChuja = 0;
string przedmiot = "";//random strings
for (int i = 0; i < 5; i++)
{
GroupBox g = new GroupBox();
g.AutoSize = true;
g.Visible = true;
g.AutoSizeMode = AutoSizeMode.GrowAndShrink;
var lb = new Label();
lb.AutoSize = true;
lb.Location = new Point(6, 16);
lb.Text = przedmiot;
g.Controls.Add(lb);
dlugoscChuja += lb.Width;
if (i != 0)
{
g.Location = new Point((i*(g.Width+g.Padding.Size.Width)-100), 12);
}
else
{
g.Location = new Point(0, 12);
}
this.Controls.Add(g);
}
how i can put those group boxes next to each other?
my kod (i*(g.Width+g.Padding.Size.Width)-100) of course does not work, is there eany proffesional way to place them? If my labels text is longer of course my group box have more width

You can use Flowlayoutpanel inside your groupbox
We can set the direction of items in Flowlayoutpanel to
TopDown
LeftToRight
RightToLeft
BottomUp
Instead of adding labels to groupBox add it to FlowLayoutpanel
//groupbox must outsideloops
GroupBox g = new GroupBox();
g.AutoSize = true;
g.Visible = true;
g.AutoSizeMode = AutoSizeMode.GrowAndShrink;
var flowPanel;
for (int i = 0; i < 5; i++)
{
flowPanel = new FlowLayoutPanel();
flowPanel.Size = g.Size; // set size of flowpanel to groupbox
flowPanel.Dock = DockStyle.Fill; // Fill parent size
flowPanel.FlowDirection = FlowDirection.TopDown;//Use above directions
var lb = new Label();
lb.AutoSize = true;
lb.Location = new Point(6, 16);
lb.Text = przedmiot;
flowPanel.Controls.Add(lb);//lb your labe
}
g.Controls.Add(flowPanel);//finally add flowpanel to groupbox
this.Controls.Add(g);

Related

How to program preset checkboxes which define which radiobutton is checked

Case: I'm trying to create a few checkboxes which define what radiobutton is set by default. I don't have a clue how to approach this, to create the radiobuttons I'm using this xml:
<Lijsten>
<Lijst>
<Titel>Discipline</Titel>
<Waardes>Elektro</Waardes>
<Waardes>Mechanisch</Waardes>
<Waardes>Civiel</Waardes>
<Waardes>Proces</Waardes>
<Waardes>N.v.t.</Waardes>
</Lijst>
<Lijst>
<Titel>Discipline</Titel>
<Waardes>Elektro</Waardes>
<Waardes>Mechanisch</Waardes>
<Waardes>Civiel</Waardes>
<Waardes>Proces</Waardes>
<Waardes>N.v.t.</Waardes>
</Lijst>
<Lijst>
<Titel>Discipline</Titel>
<Waardes>Elektro</Waardes>
<Waardes>Mechanisch</Waardes>
<Waardes>Civiel</Waardes>
<Waardes>Proces</Waardes>
<Waardes>N.v.t.</Waardes>
</Lijst>
</Lijsten>
C# code:
foreach (XmlNode node in nodes)
{
int heightRadioButtons = 0;
WidthPanelsRow1 += 155;
Panel panel = new Panel();
panel.Size = new Size(140, 200);
panel.Location = new Point(WidthPanelsRow1, heightPanelsRow1);
panel.Name = "panel" + count.ToString();
panel.BackColor = Color.LightGray;
Label lbl = new Label();
lbl.Text = node["Titel"].InnerText;
lbl.Location = new Point(0, 0);
lbl.Font = font1;
panel.Controls.Add(lbl);
int counterLastRadioButton = 0;
XmlNodeList waardeNodes = node.SelectNodes("Waardes");
foreach (XmlNode wNode in waardeNodes)
{
counterLastRadioButton += 1;
heightRadioButtons += 20;
RadioButton rb = new RadioButton();
rb.Text = wNode.InnerText;
rb.Location = new Point(5, heightRadioButtons);
rb.Name = node["Titel"].InnerText;
if (waardeNodes.Count - 1 < counterLastRadioButton)
{
rb.Checked = true;
}
panel.Controls.Add(rb);
}
this.Controls.Add(panel);
}
What I'm trying to achieve: Create a checkbox dynamically from the xml, with per "Titel" the default "Waardes". So if I check that checkbox, the radiobutton of that "Titel" will change to that "value".

How to add radio buttons dynamically in Windows form?

I need to add radio buttons dynamically in my windows form and in horizontal mode.
for (int i = 0; i <= r.Count; i++)
{
RadioButton rdo = new RadioButton();
rdo.Name = "id";
rdo.Text = "Name";
rdo.ForeColor = Color.Red;
rdo.Location = new Point(5, 30 );
this.Controls.Add(rdo);
}
You could do something like this:
FlowLayoutPanel pnl = new FlowLayoutPanel();
pnl.Dock = DockStyle.Fill;
for (int i = 0; i < 4; i++)
{
pnl.Controls.Add(new RadioButton() { Text = "RadioButton" + i });
}
this.Controls.Add(pnl);
You could also add the FlowLayoutPanel in the designer and leave that part out in the code.
To get the selected RadioButton use a construct like this:
RadioButton rbSelected = pnl.Controls
.OfType<RadioButton>()
.FirstOrDefault(r => r.Checked);
To use this the FlowLayoutPanel needs to be known in the calling method. So either add it to the Form in the designer (Thats what I would prefer) or create it as an instance member of the form and add it at runtime (this has no benefit).
You can do something like this
//This is my dynamic data list
List<ItemType> itemTypeData = new List<ItemType>()
RadioButton[] itemTypes = new RadioButton[ItemType.Count];
int locationX = 0;
for (int i = 0; i < ItemType.Count; i++)
{
var type = ItemType[i];
itemTypes[i] = new RadioButton
{
Name = type.Code,
Text = type.Code,
AutoSize = true,
Font = new System.Drawing.Font("Calibri", 11F, FontStyle.Regular),
Location = new Point(156 + locationX, 88),
};
this.Controls.Add(itemTypes[i]);
locationX += 80;
}
This works fine for me

How to set different events for dynamically created element

Im trying to create event with different index for dynamically created GroupBox. With my actual code event for every groupbox is that same. How can i make event with different index for every groupbox? My Code:
public void LoadGry()
{
// GroupBox groupbox = new GroupBox();
Label nazwagry = new Label();
for(int i = 0; i < myCollection.Count; i++)
{
GroupBox groupbox = new GroupBox();
groupbox.Text = myCollection[i];
groupbox.Size = new Size(290, 131);
groupbox.Location = new Point(6, 150 * (myCollection.Count - i - 1));
groupbox.ForeColor = Color.White;
Label label1 = new Label();
label1.Text = groupbox.Text;
label1.AutoSize = true;
label1.Location = new Point(groupbox.Location.X + 80, groupbox.Location.Y + 20);
groupbox.Controls.Add(label1);
Gry.Controls.Add(label1);
PictureBox picturebox = new PictureBox();
picturebox.Location = new Point(groupbox.Location.X + 5, groupbox.Location.Y + 20);
picturebox.Size = new Size(75, 75);
picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
picturebox.LoadAsync(myCollection3[i]);
groupbox.Click += new EventHandler(delegate {groupboxclick(groupbox, picturebox, i);});
Label label2 = new Label();
label2.Text = "Status: " + "Aktualny";
label2.ForeColor = Color.Green;
label2.AutoSize = true;
label2.Location = new Point(label1.Location.X, label1.Location.Y + 20);
Gry.Controls.Add(label2);
Label zapiszopis = new Label();
zapiszopis.Text = myCollection4[i];
zapiszopis.Visible = false;
Gry.Controls.Add((Control)groupbox);
//MessageBox.Show("pokaz mi wysokosc");
}
}
private void groupboxclick(GroupBox groupbox, PictureBox picturebox, int itest)
{
groupbox.ForeColor = Color.Aqua;
this.pictureBox1.BackgroundImage = picturebox.BackgroundImage;
opishacka.Text = myCollection4[itest];
}
The problem is that the event setup is using the variable K value. For use the number instead you probably needs to create an expression manually to use the current value in each case.
BUT
You can easily do what you want using the following properties to attach values to controls.
1-) Tag in WinForms & WPF:
// Setup
pictureBox.Tag = i;
// Event
int i = (int) pictureBox.Tag;
2-) ViewState in WebForms
// Setup
ViewState[pictureBox.UniqueID] = i;
// Event
int i = (int) ViewState[pictureBox.UniqueID];
You can use many other techniques. I only post one for each popular framework. I guest that you are in a WinFors project.
Hope this help!

Restrict the numbers of controls in a winform

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);
}
}
}
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
}
}
}

TableLayoutPanel changing it's sizing

I have a few tablelayoutpanels in my program in order to lay out different controls in rows. Until recently these were all working correctly, but after a change in server this is no longer the case. A potential cause is a change in screen resolution as I noticed similar problems on other computers running my program.
An example of this problem is shown below
Before the data rows are loaded (from database), the sizing is incorrect. A measurement using the VS designer shows that the size in figure 2 is correct. Interestingly, if I add a blank row to this table then the resizing doesn't happen.
public JobOpMainTable()
{
DoubleBuffered = true;
AutoSize = true;
AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
DoubleBuffered = true;
BackColor = Color.White;
CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
headers();
}
private void headers()
{
string[] names = { "Operation", "Est Lab Hrs", "Est UM Hrs", "Act Lab Hrs", "Act UM Hrs" };
for (int i = 0; i < names.Length; i++) header(names[i], i);
}
private void header(string name, int col)
{
Panel pan = new Panel();
pan.Width = widths[col];
pan.Height = 25;
pan.BackColor = Color.Black;
pan.Dock = DockStyle.Fill;
pan.Margin = new System.Windows.Forms.Padding(0);
TextBox txt = new TextBox();
txt.Font = new System.Drawing.Font("Microsoft sans serif", 8, FontStyle.Bold);
txt.Text = name;
txt.ReadOnly = true;
txt.BackColor = Color.Black;
txt.ForeColor = Color.White;
txt.Dock = DockStyle.Fill;
txt.BorderStyle = System.Windows.Forms.BorderStyle.None
pan.Controls.Add(txt);
Controls.Add(pan, col, 0);
}
private int newRow()
{
int row = Controls.Count / 5;
for (int i = 0; i <= 4; i++)
{
TextBox txt = new TextBox();
txt.Width = widths[i];
txt.BackColor = Color.White;
txt.BorderStyle = System.Windows.Forms.BorderStyle.None;
txt.ReadOnly = true;
txt.Dock = DockStyle.Fill;
txt.Margin = new Padding(0);
txt.Enter += new EventHandler(enterCell);
txt.Leave += new EventHandler(leaveCell);
Controls.Add(txt, i, row);
}
return row;
}
Above is the code I believe is relevant to the problem. The resizing occurs the first time newRow() is called when loading in data: each new box makes the column wider. Again though, the odd thing is this doesn't occur if I add a row from the constructor

Categories