How to get the values from dynamic NumericUpDown in C#?
Here is my code,
for (int i=0; i<n;i++)
{
NumericUpDown notele = new NumericUpDown();
notele.Name = "note" + i.ToString();
notele.Location = new System.Drawing.Point(280, 208 + (30 * i));
notele.Size = new System.Drawing.Size(40, 25);
notele.BackColor = System.Drawing.SystemColors.ControlDark;
notele.Maximum = new decimal(new int[] {10,0,0, 0});
this.Controls.Add(notele);
}
Access your controls using the Control collection of your form and pass to it the name of your numericUpDown control:
var numericUpDown = this.Controls["note0"] as NumericUpDown;
if(numericUpDown != null)
{
var value = numericUpDown.Value;
}
You can still utilise the ValueChanged event:
...
notelle.ValueChanged += UpDnValueChangedHandler;
this.Controls.Add(notele);
}
private void UpDnValueChangedHandler(object sender, EventArgs e)
{
// sender references the control which value was changed:
NumericUpDown notelle = sender as NumericUpDown;
// further processing goes here
}
Related
I am unable to get the controls count on the newly created form on the newly created button, I have created 5 controls but only one is showing. If I cannot get the total control count then I cannot also get the control type, name etc.
private void button2_Click(object sender, EventArgs e)
{
Form frm = new Form();
frm.Text = "new form";
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.AutoSize = true;
Button btn = new Button();
btn.Text = "ok";
tlp.Controls.Add(btn, 0, 4);
frm.Controls.Add(tlp);
for (int i = 3, ii = 0; i >= 0; i--, ii++)
{
TextBox tbx = new TextBox();
tlp.Controls.Add(tbx, 0, ii);
}
frm.Show();
string str = frm.Controls.Count.ToString();
btn.Click += (s, args) =>
{
MessageBox.Show(frm.Text);
MessageBox.Show(ActiveForm.Text);
MessageBox.Show(str);
};
}
In your code the only Control that you've added to your form is a TableLayoutPanel that contains TextBox controls. That is why the count is 1.
I'm printing out items from the list each to the label, and a button nearby for removing them from the list. The removal button doesn't seem to work.
private void cart_Click(object sender, EventArgs e)
{
krepselioPanel.Visible = !krepselioPanel.Visible;
krepselioPav.Visible = !krepselioPav.Visible;
int i = 0;
double s = 0;
foreach (Patiekalas preke in prekes)
{
Label prekiulist = new Label();
prekiulist.Location = new Point(0, 26 * i);
prekiulist.Text = preke.GetPatiekalas() + " | " + preke.GetKaina() + "€";
prekiulist.Size = new Size(200, 20);
krepselioPanel.Controls.Add(prekiulist);
s += Convert.ToDouble(preke.GetKaina());
Button removeButton = new Button();
removeButton.Text = "x";
removeButton.Location = new Point(200, 26 * i);
removeButton.Font = new Font(FontFamily.GenericSansSerif, 9);
removeButton.Size = new Size(20, 22);
removeButton.Click += removeButton_Click;
removeButton.Tag = preke;
krepselioPanel.Controls.Add(removeButton);
i++;
}
Label suma = new Label();
suma.Location = new Point(krepselioPanel.Right - 140, 0);
suma.Font = new Font(FontFamily.GenericSansSerif, 13);
suma.Text = "Total: " + s + "€";
suma.Size = new Size(130, 25);
krepselioPanel.Controls.Add(suma);
}
private void removeButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Patiekalas preke = (Patiekalas)b.Tag;
prekes.Remove(preke);
cart_Click(sender, e);
cart_Click(sender, e);
}
}
Just for the interest sake I changed the functions removeButton_Click line
prekes.Remove(preke);
to
prekes.Add(preke);
and this creates a new entry to the list but removal doesn't work however.
The code doesn't seem to remove the previous controls from the panel so each time the controls are added new ones are created. If you have only one item in the cart then the next time nothing seems to happen, nothing added nor removed, but if there's multiple items they will start to multiply.
So remove the controls from the panel and then add new ones and the item will disappear.
This is a follow up question for my previous question.
However in this question, I created such a Control so whenever I click on the button, two TextBoxes will appear (Indefinite count/click) in a row. Whenever I input again a value, they will do the addition and put the result in the third TextBox of their row.
private void buttonNewRow_Click(object sender, EventArgs e)
{
int count = 1;
DateTimePicker date = new DateTimePicker();
count = panel1.Controls.OfType<DateTimePicker>().ToList().Count;
date.Location = new Point(0, 15 * count);
date.Size = new Size(91, 20);
panel1.Controls.Add(date);
//Textbox 1
TextBox textboxTranspo = new TextBox();
textboxTranspo.Location = new Point(576, 45 * count);
textboxTranspo.Size = new Size(81, 20);
panel1.Controls.Add(textboxTranspo);
//Textbox 2
TextBox textboxDaily = new TextBox();
textboxDaily.Location = new Point(663, 45 * count);
textboxDaily.Size = new Size(81, 20);
panel1.Controls.Add(textboxDaily);
//Textbox 1 + Textbox 2 result
TextBox textboxTotal = new TextBox();
textboxTotal.Location = new Point(772, 45 * count);
textboxTotal.Size = new Size(100, 20);
panel1.Controls.Add(textboxTotal);
}
How can I accomplish this?
You can add event handlers like this:
private void buttonNewRow_Click(object sender, EventArgs e)
{
DateTimePicker date = new DateTimePicker();
int count = panel1.Controls.OfType<DateTimePicker>().ToList().Count;
date.Location = new Point(0, 15 * count);
date.Size = new Size(91, 20);
panel1.Controls.Add(date);
//Textbox 1
TextBox textboxTranspo = new TextBox();
textboxTranspo.Location = new Point(576, 45 * count);
textboxTranspo.Size = new Size(81, 20);
panel1.Controls.Add(textboxTranspo);
//Textbox 2
TextBox textboxDaily = new TextBox();
textboxDaily.Location = new Point(663, 45 * count);
textboxDaily.Size = new Size(81, 20);
panel1.Controls.Add(textboxDaily);
//Textbox 1 + Textbox 2 result
TextBox textboxTotal = new TextBox();
textboxTotal.Location = new Point(772, 45 * count);
textboxTotal.Size = new Size(100, 20);
panel1.Controls.Add(textboxTotal);
EventHandler textChanged = (o, e) => {
int intTranspo, intDaily;
if (int.TryParse(textboxTranspo.Text, out intTranspo)
&& int.TryParse(textboxDaily.Text, out intDaily))
textboxTotal.Text = (intTranspo + intDaily).ToString();
};
textboxTranspo.TextChanged += textChanged;
textboxDaily.TextChanged += textChanged;
}
I will address my comment above as an answer.
First you have to define a UserControl and named it whatever you like but let us name it Uc1.
In the designer drag the datetimepicker and the 3 textboxes to the Uc1. Supposed we name the control dt1 for datetimepicker and txtTranspo for textbox1 and txtDaily for textbox2 and txtTotal for the last one. And since we need to compute the value of txtTranspo and txtDaily i want to add 1 button named btnCompute for computation logic for simplicity sake.
then inside the Uc1
class Uc1
{
public Uc1()
{
InitializeComponent();
this.wireEvents();
}
// This will handle all controls events for well organized form sakes.
private void wireEvents()
{
this.btnCompute.Click += OnBtnComputeEvent;
}
private void OnBtnComputeEvent(object sender, EventArgs e)
{
// i used int since i do not know exactly how to do this.
int transpo = Int32.Parse(this.txtTranspo.Text);
int daily = Int32.Parse(this.txtDaily.Text);
double total = ((transpo + daily) / 100 * 100) // <--- this is just a random computation.
this.txtTotal.Text = string.Format("{0:c}", total); // this will format the total to a currency.
}
}
Then build the solution. or click the combination of Ctrl + Shift + B. After this the Uc1 will appear to your toolbox as a new control.
Then you can simply do this to your own logic.
private void buttonNewRow_Click(object sender, EventArgs e)
{
Uc1 c = new Uc1();
c.Dock = DockStyle.Top;
c.BringToFront();
this.panel.Controls.Add(c);
}
You are good to go.
I have a checkedListBox in a TabControl
What I want is to create a label and NumericUpDown dynamically, when User check an item of checkedListBox it will show the new label and NumericUpDown
Then , when it Unchecked this item ,The numericUpDown will be clear (empty).
Conclusion: As many checked items , as many w've create labels and NumericUpDowns.
Please, how will I do that ??
For each checkbox item in your checkedListBox in properties switch to events and create subscriber checkBoxName_CheckStateChanged for event CheckStateChanged.
The code in the sucriber can be like this:
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
var source = sender as CheckBox;
if (source.Checked == true)
{
this.numericUpDown1.Text = "TextWhenChecked";
this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
}
else
{
this.numericUpDown1.Text = "TextWhenUnchecked";
this.label1.Text = "TextWhenUnchecked";
}
}
You fill the strings as you want. These are only examples.
To have only checkBox checked at a time look at here: https://stackoverflow.com/a/24693858/6650581.
What you need to do is creating Label and NumericUpDown manually and show it by adding to Controls collection. A TableLayoutPanel can help you arranging controls without setting Size and calculate Location manually.
Here is an example:
public class MainForm : Form
{
private CheckedListBox checkedListBox;
private TableLayoutPanel tableLayoutPanel;
public MainForm()
{
InitializeComponent();
//Fill checkedListBox and create controls
for( int i = 0; i <= 5; i++ )
{
checkedListBox.Items.Add( i.ToString() );
Label lbl = new Label()
{
Name = "lbl" + i,
Text = "Label " + i,
Visible = false
};
NumericUpDown num = new NumericUpDown()
{
Name = "num" + i,
Value = i,
Visible = false
};
tableLayoutPanel.Controls.Add( lbl, 0, i );
tableLayoutPanel.Controls.Add( num, 1, i );
}
}
private void checkedListBox_ItemCheck( object sender, ItemCheckEventArgs e )
{
if( e.NewValue == CheckState.Checked )
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = true;
tableLayoutPanel.Controls["num" + e.Index].Visible = true;
}
else
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = false;
((NumericUpDown)tableLayoutPanel.Controls["num" + e.Index]).Value = 0M;
}
}
private void InitializeComponent()
{
this.checkedListBox = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// checkedListBox
//
this.checkedListBox.Location = new System.Drawing.Point(8, 8);
this.checkedListBox.Name = "checkedListBox";
this.checkedListBox.Size = new System.Drawing.Size(200, 100);
this.checkedListBox.TabIndex = 1;
this.checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck);
//
// tableLayoutPanel
//
this.tableLayoutPanel.AutoScroll = true;
this.tableLayoutPanel.ColumnCount = 2;
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.Location = new System.Drawing.Point(8, 112);
this.tableLayoutPanel.Name = "tableLayoutPanel";
this.tableLayoutPanel.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel.TabIndex = 2;
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(223, 227);
this.Controls.Add(this.tableLayoutPanel);
this.Controls.Add(this.checkedListBox);
this.Name = "MainForm";
this.ResumeLayout(false);
}
}
I want to be able to assign each checkbox to it's own richtextbox
I'm making the richtextboxes, then I'm making the checkboxes but how can I "link" them together?
for example :
// richtextbox 1 - > checkbox 1 = false
// richtextbox 2 - > checkbox 2 = true
// richtextbox 3 - > checkbox 3 = true
// richtextbox 4 - > checkbox 4 = false
this is my code:
int n = TodoItems.Count;
RichTextBox[] RichtextBoxes = new RichTextBox[n];
CheckBox[] Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = true;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += new EventHandler(this.CheckedChange);
TodoList.Controls.Add(Checkboxes[i]);
}
I have modified your code please see below, you can access your desired rich text box on checkbox click.
RichTextBox[] RichtextBoxes { get; set; }
CheckBox[] Checkboxes { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
int n = TodoItems.Count;
RichtextBoxes = new RichTextBox[n];
Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB-" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = false;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB-" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += checkBox1_CheckedChanged;
TodoList.Controls.Add(Checkboxes[i]);
}
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
string cbName = cb.Name;
int sbNumber = int.Parse(cbName.Split('-')[1]);
RichtextBoxes[sbNumber].Visible = true; // you can get desired richtextbox here and can any thing with it :)
}
I have found the answere, and I'll post it to make your life easyer
when I made the richtextboxes and the checkboxes I set there names to a number
Checkboxes[i].Name = i.ToString();
then I used the event that was provided for me by Mirza Danish Baig
Create and event void checkBox_CheckedChanged(object sender, EventArgs e) { } and then assign this event name Checkboxes[i].CheckedChanged += checkBox_CheckedChanged; –
and after that I started to try things eventualy I came to this :
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox ThisCheckbox = (CheckBox)sender;
if (ThisCheckbox.Checked == true)
{
//finding the richtextbox by id...
RichTextBox ThisRichtextbox = this.Controls.Find("TB" + ThisCheckbox.Name, true).FirstOrDefault() as RichTextBox;
try//try and catch for testing, this can be removed later.
{
MessageBox.Show(ThisRichtextbox.Text);
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
}