I'm trying to make a form where the user can add group boxes with other controls inside.
groupboxes and other controls are all created dynamically.
Program image. *the button "Adicionar Pergunta" adds a new group box
Inside the groupbox is a combobox where the user can choose the preset of controls inside the groupbox.
each preset have different controls or controls in different locations of the group box.
my problem is that when the user change the preset of one groupbox all the other combobox stop working.
Here is the my script:
Variables:
int groupboxcount = 2;
RadioButton rb1 = new RadioButton();
RadioButton rb2 = new RadioButton();
TextBox tx1 = new TextBox();
TextBox tx2 = new TextBox();
GroupBox gb;
ComboBox cb;
RadioButton rb_1;
RadioButton rb_2;
RadioButton rb_V;
RadioButton rb_F;
TextBox tx_1;
TextBox tx_2;
Button Click:
{
gb = new GroupBox();
NumericUpDown Nud1 = new NumericUpDown();
cb = new ComboBox();
cb.SelectedIndexChanged += new EventHandler(cb2_selectec_index_changed);
RichTextBox RichTextBox2 = new RichTextBox();
Label labelA1 = new Label();
Label labelA2 = new Label();
Label labelA3 = new Label();
gb.Name = "groupBox" + groupboxcount;
gb.Text = "Pergunta ";
gb.Height = 182;
gb.Width = 520;
gb.Location = new Point(groupBox1.Location.X, gb.Location.Y + gb.Height + 125);
Nud1.Height = 20;
Nud1.Width = 41;
Nud1.Location = new Point(6, 37);
Nud1.Value = groupboxcount;
cb.Items.Add("Enunciado");
cb.Items.Add("Pergunta normal");
cb.Items.Add("Escolha multipla");
cb.Items.Add("Verdadeiro/Falso");
cb.Height = 21;
cb.Width = 121;
cb.Location = new Point(53, 36);
RichTextBox2.Height = 80;
RichTextBox2.Width = 249;
RichTextBox2.Location = new Point(211, 37);
labelA1.Text = "Nº";
labelA2.Text = "Tipo de pergunta";
labelA3.Text = "Pergunta";
labelA1.Location = new Point(6, 19);
labelA2.Location = new Point(59, 19);
labelA3.Location = new Point(208, 19);
labelA1.Width = 20;
this.Controls.Add(gb);
gb.Controls.Add(Nud1);
gb.Controls.Add(cb);
gb.Controls.Add(RichTextBox2);
gb.Controls.Add(labelA1);
gb.Controls.Add(labelA2);
gb.Controls.Add(labelA3);
button1.Location = new Point(button1.Location.X, gb.Location.Y + button1.Height + 200);
groupboxcount++;
}
Combobox cb2 selected index changed:
{
if (cb.SelectedIndex == 0)
{
//Apenas mostra enunciado
}
if (cb.SelectedIndex == 1)
{
//recebe resposta
}
if (cb.SelectedIndex == 2)
{
rb_1 = new RadioButton();
rb_2 = new RadioButton();
tx_1 = new TextBox();
tx_2 = new TextBox();
rb_1.Text = "opção 1";
rb_1.Location = new Point(120, 75);
rb_2.Text = "opção 2";
rb_2.Location = new Point(120, 100);
tx_2.Location = new Point(5, 100);
tx_1.Location = new Point(5, 75);
this.gb.Controls.Add(rb_1);
this.gb.Controls.Add(rb_2);
this.gb.Controls.Add(tx_1);
this.gb.Controls.Add(tx_2);
}
else
{
this.gb.Controls.Remove(rb_1);
this.gb.Controls.Remove(rb_2);
this.gb.Controls.Remove(tx_1);
this.gb.Controls.Remove(tx_2);
}
if (cb.SelectedIndex == 3)
{
rb_V = new RadioButton();
rb_F = new RadioButton();
rb_V.Text = "Verdadeiro";
rb_V.Location = new Point(120, 75);
rb_F.Text = "Falso";
rb_F.Location = new Point(120, 100);
this.gb.Controls.Add(rb_V);
this.gb.Controls.Add(rb_F);
}
else
{
this.gb.Controls.Remove(rb_V);
this.gb.Controls.Remove(rb_F);
}
}
I would appreciate if you could help me.
Related
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);
I have a list of items that is constantly updated from live readings, the newest items should be added on the top of the list.
is it possible to have an item template for the list view with 2 rows and 2 column for each item. (like attached photo).
I searched online i could not find a solution.
my second approach was to create Main Panel and append Panels to it sub panels to it containing the labels.I am only able to add the items in the bottom of the Main panel since i can calculate the location of the last added item.
Panel pan = new Panel();
pan.Name = "panel" + counter;
pan.BorderStyle = BorderStyle.FixedSingle;
Label label1 = new Label();
label1.AutoSize = true;
label1.Location = new System.Drawing.Point(0, 0);
label1.Name = "label" + counter;
label1.Size = new System.Drawing.Size(35, 13);
label1.TabIndex = 1;
label1.Text = "label" + counter;
Label label2 = new Label();
label2.AutoSize = true;
label2.Location = new System.Drawing.Point(0, 20);
label2.Name = "label2" + counter;
label2.Size = new System.Drawing.Size(35, 13);
label2.TabIndex = 2;
label2.Text = "label2" + counter;
Label label3 = new Label();
label3.AutoSize = true;
label3.Location = new System.Drawing.Point(40, 0);
label3.Name = "label3" + counter;
label3.Size = new System.Drawing.Size(35, 13);
label3.TabIndex = 3;
label3.Text = "label3" + counter;
Label label4 = new Label();
label4.AutoSize = true;
label4.Location = new System.Drawing.Point(40, 20);
label4.Name = "label4" + counter; ;
label4.Size = new System.Drawing.Size(35, 13);
label4.TabIndex = 4;
label4.Text = "label4" + counter;
pan.Location = new Point(0, counter * 50);
pan.Size = new Size(100, 50);
pan.Controls.Add(label1);
pan.Controls.Add(label2);
pan.Controls.Add(label3);
pan.Controls.Add(label4);
MainPanel.Controls.Add(pan);
counter++;
Thanks
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".
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
}
}
}
I am trying to add different objects to a GroupBox. When I click 'Add Title' button I can get the Combobox and Textbox to appear in the Groupbox.
What I want to now happen is when they click 'Add question' I want to be able to add the questions objects (Combobox, Textbox) to the same Groupbox as the last added title.
C# CODE:
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
gp.Content = sp;
spStandard.Children.Add(gp);
}
private void ViewQuestions(StackPanel sp)
{
gp.Content = sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
ViewQuestions(sp);
}
I have tried to have an attempted but when I click 'Add Question' it just overwrites the Title's objects.
Any help would be amazing.
I am using WPF, and these are created at runtime.
Thanks.
EDIT4:
I have had to move
StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
GroupBox gp;
Just below the
public partial class CreateNewStandard : Page
{
So it is visible to all the methods. Then I get this error.
http://i.stack.imgur.com/f9uqF.png
Try this
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
outerSp.Children.Add(sp);
gp.Content = outerSp;
spStandard.Children.Add(gp);
}
private void ViewQuestions(StackPanel sp)
{
var stackPanel=gp.Content as StackPanel;
if(stackPanel!=null)
{
stackPanel.Children.Add(sp);
}
else
gp.Content=sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
outerSp.Children.Add(sp);
ViewQuestions(sp);
}
You are overwriting it .Though you need to add them to the already existing stackPanel. The code is not tested I am trying to give you an idea .I hope this will help.