I want to create a new TabPage on my TabControl. The new TabPage generates a new ListView and 3 TextBoxes. And I want to insert content to this. My problem: I don't know how to access the ListViews and TextBoxes for every new TabPage.
Here is my code:
// Create new Tab on Tabcontroll
private void createNewTabwithNotebookName()
{
TabPage myTabpage = new TabPage(NewNotebook.notebookname);
tcMainWindow.TabPages.Add(myTabpage);
}
public void CreateListviewAndTextboxes()
{
ListView listView1 = new ListView();
listView1.Bounds = new Rectangle(new Point(10, 10), new Size(159, 400));
listView1.View = View.List;
listView1.LabelEdit = false;
listView1.AllowColumnReorder = true;
listView1.CheckBoxes = false;
listView1.FullRowSelect = true;
listView1.GridLines = true;
ColumnHeader column1 = new ColumnHeader();
column1.Width = 159;
column1.TextAlign = HorizontalAlignment.Left;
listView1.Columns.Add(column1);
int i = tcMainWindow.TabCount - 1;
// Add the ListView to the control collection.
this.tcMainWindow.TabPages[i].Controls.Add(listView1);
TextBox textbox1 = new TextBox();
textbox1.Bounds = new Rectangle(new Point(240, 20), new Size(350, 20));
textbox1.Multiline = true;
this.tcMainWindow.TabPages[i].Controls.Add(textbox1);
TextBox textbox2 = new TextBox();
textbox2.Bounds = new Rectangle(new Point(240, 60), new Size(350, 250));
textbox2.Multiline = true;
this.tcMainWindow.TabPages[i].Controls.Add(textbox2);
TextBox textbox3 = new TextBox();
textbox3.Bounds = new Rectangle(new Point(240, 350), new Size(350, 20));
textbox3.Multiline = true;
this.tcMainWindow.TabPages[i].Controls.Add(textbox3);
}
Every ListView/TextBox should get other content. Please help.
Try this code:
Loop through TabPages controls and find your desired controls:
//method for all tab pages
private void AllTabPages()
{
foreach (TabPage pg in tcMainWindow.TabPages)
FillControls(pg);
}
//method for individual tab page
private void FillControls(TabPage pg)
{
foreach (Control c in pg.Controls)
{
if (c is ListView)
{
//do something
ListView lv = c as ListView;
lv.Items.Add("abc");
lv.Items.Add("def");
}
else if (c is TextBox)
{
//do something
c.Text = "Add Some Text";
}
}
}
If you want to search control within specific tab page call FillControls(TabPage pg) method:
FillControls(this.tcMainWindow.TabPages[i]);
Related
I am design a custom message box. This message box will show a list of string. But it's not work. Can you hep me to fix it.
You can see in the picture, I have a list with 7 items. When i click the "Grade Project", the Message box don't show any item.
And this is the result I need
//Main form
private void btnGrade_Click(object sender, EventArgs e)
{
List<string> result = new List<string>();
result.Add("True");
result.Add("False");
result.Add("True");
result.Add("False");
result.Add("True");
result.Add("False");
result.Add("False");
MsgBox.Show(result,"Project 1",MsgBox.Buttons.OK);
}
This is code in msgbox form
//MsgBox form
public partial class MsgBox : Form
{
private static MsgBox _msgBox;
// Header, Footer
private Panel _plHeader = new Panel();
private Label _lblTitle;
private Panel _plFooter = new Panel();
private Panel _plIcon = new Panel();
// Panel
private FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
// button
private List<Button> _buttonCollection = new List<Button>();
// Kết quả
private static DialogResult _buttonResult;
// Message
private List<String> _lblMessage;
private MsgBox()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.BackColor = Color.FromArgb(45, 45, 48);
this.StartPosition = FormStartPosition.CenterScreen;
this.Padding = new System.Windows.Forms.Padding(3);
this.Width = 800;
// Header
_lblTitle = new Label();
_lblTitle.ForeColor = Color.White;
_lblTitle.Font = new System.Drawing.Font("Segoe UI", 18);
_lblTitle.Dock = DockStyle.Top;
_lblTitle.Height = 60;
// Message
_lblMessage = new List<string>();
for (int i = 0; i < _lblMessage.Count; i++)
{
TextBox txt = new TextBox(); //Create Textbox có name txt
txt.Text = _lblMessage[i];
txt.ForeColor = Color.Red;
txt.Font = new System.Drawing.Font("Segoe UI", 30);
this.Controls.Add(txt); //add control txt
}
_flpButtons.FlowDirection = FlowDirection.RightToLeft;
_flpButtons.Dock = DockStyle.Fill;
_plHeader.Dock = DockStyle.Fill;
_plHeader.Padding = new Padding(20);
// _plHeader.Controls.Add(_lblMessage);
_plHeader.Controls.Add(_lblTitle);
_plFooter.Dock = DockStyle.Bottom;
_plFooter.Padding = new Padding(20);
_plFooter.BackColor = Color.FromArgb(37, 37, 38);
_plFooter.Height = 80;
_plFooter.Controls.Add(_flpButtons);
// Add controls vào form
this.Controls.Add(_plHeader);
//this.Controls.Add(_plIcon);
this.Controls.Add(_plFooter);
}
public static DialogResult Show(List<String> message, string title, Buttons buttons)
{
_msgBox = new MsgBox();
_msgBox._lblMessage = message;
_msgBox._lblTitle.Text = title;
_msgBox._plIcon.Hide();
MsgBox.InitButtons(buttons);
_msgBox.ShowDialog();
return _buttonResult;
}
Thank you for your watching.
Something wrong with your codes:
private MsgBox()
{
...
// Message
_lblMessage = new List<string>();
...
}
Your _lblMessage will always be an empty list so you see no message at all.
You can change your codes like this:
private MsgBox(List<String> messages)
{
...
// Message
_lblMessage = messages;
...
}
public static DialogResult Show(List<String> message, string title)
{
_msgBox = new MsgBox(message);
//_msgBox._lblMessage = message;
....
}
And also, you'd better set TextBox position otherwise all the TextBox will overlap with each other.
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.
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 creating List of Panel. Each panel Contain a Label and Button . I have also a Button(button1). I want to change the label text of (panels[0]) when click button1. How can I do this.This is my c# code:
List<Panel> panels = new List<Panel>();
private void Panel()
{
var x = 0;
for (int i = 1; i < 5; i++)
{
x += 60;
var panel1 = new Panel() { Size = new Size(60, 140), Location = new Point(x, 100), BorderStyle = BorderStyle.FixedSingle };
panel1.Name = "pan" + i;
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = i.ToString();
lbl.Location = new Point(10, 20);
panel1.Controls.Add(lbl);
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
panel1.Controls.Add(button);
panels.Add(panel1);
Controls.Add(panel1);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var p in panels)
{
}
}
Output:
But i want, When i Click button1 it will change Label text of zero index panels(I have pointed it using red mark).
Can anyone help me...
Ok, so you've got a button and a label within a panel. When you click a button of a panel, you wanna do something to the label in the same panel, right ?
So
private void BtnClick(object sender, EventArgs e) {
var button = (Button)sender;//you've got the button clicked
var panel = button.Parent;//you've got the panel.
//var label = panel.Controls.OfType<Label>().FirstOrDefault();//but don't think you get this in c# 3.0
var label = GetFirstLabel(panel);
if (label != null)
label.Text = "something";
}
private Label GetFirstLabel(Control parent) {
foreach (var control in parent.Controls) {
if (control is Label) return control as Label;
}
return null;
}
Usage
When you add your buttons, you can now do
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
button.Click += BtnClick;
And this will work on all panels.
If you don't store reference to that Label then you can find it in controls of the first Panel by type for example:
panels[0].Controls.OfType<Label>().First().Text = "New Text";
or by name
panels[0].Controls.OfType<Label>().Single(l => l.Name == "lbl1").Text = "New Text";
you can do this with a simple method:
private void button1_Click(object sender, EventArgs e)
{
Label l = panels[0].Controls.Find("lbl1", false).FirstOrDefault() as Label;
l.Text = "TEXT";
}
I am developing a windows application containing 3 split containers(two panels each,
total 6 panels).
Now I want to add 3 labels dynamically in each panel.One solution I am trying to use for loop and access all splitcontainers and their panels but I dont know how to use for loop for accessing splitcontainer.
Can I use for loop for this? Also I want to add controls to all panels(6) at the same time. How to do this.
Thanks in advance..!!
This is what I have done...
foreach (SplitContainer sp in this.Controls)
{
Label tileTitle = new Label();
tileTitle.Text = "OneClick";
tileTitle.Visible = true;
tileTitle.Location = new Point(10, 10);
sp.Panel1.Controls.Add(tileTitle);
}
foreach (Control c in this.Controls)
{
if (c is SplitContainer)
{
Label tileTitle = new Label();
tileTitle.Text = "OneClick";
tileTitle.Visible = true;
tileTitle.Location = new Point(10, 10);
Label tileTitle2 = new Label();
tileTitle2.Text = "OneClick";
tileTitle2.Visible = true;
tileTitle2.Location = new Point(10, 10);
((SplitContainer)c).Panel1.Controls.Add(tileTitle);
((SplitContainer)c).Panel2.Controls.Add((tileTitle2));
}
}
Try to use the Controls.OfType extension to get only the controls of SplitContainer type
foreach (SplitContainer sp in this.Controls.OfType<SplitContainer>())
{
Label title = MakeLabel("OneClick", new Point(10, 10);
sp.Panel1.Controls.Add(title);
Label title1 = MakeLabel("OneClick", new Point(10, 10);
sp.Panel2.Controls.Add(title1);
}
private Label MakeLabel(string caption, Point position)
{
Label lbl = new Label();
lbl.Text = caption;
lbl.Location = position;
lbl.Visible = true;
return lbl;
}
edit
Steve, you add the same label to panel1 and panel2. i fixed the variable name in the add method of panel2.
i have done this in the same way as Steve did it, but i'm using a TableLayoutPanel to store all the splitcontainers, because you can add more than one SplitContainer with Dockstyle.Fill at the same time.
private void Form1_Load(object sender, EventArgs e)
{
foreach (SplitContainer sc in this.tableLayoutPanel1.Controls.OfType<SplitContainer>())
{
Label title = MakeLabel("OneClick", new Point(10, 10));
sc.Panel1.Controls.Add(title);
Label title1 = MakeLabel("TwoClick", new Point(10, 10));
sc.Panel2.Controls.Add(title1);
}
}
private Label MakeLabel(string caption, Point position)
{
Label lbl = new Label();
lbl.Text = caption;
lbl.Location = position;
lbl.Visible = true;
return lbl;
}
the solution works perfectly as seen here: http://imageshack.us/photo/my-images/838/splitcontainer.png/