C# dynamic box data read - c#

I'm having problem understanding in how to read data from textbox, that was dynamically created at the run time. I get an error,and here is the code.I'm sure there is just a minor change needs to be added, but I cant find what exactly. Thanks
private void button2_Click(object sender, EventArgs e)
{
//*************************************TEXTBOX***********************************************//
TextBox tbox1 = new TextBox();
tbox1.Name = "textBox8";
tbox1.Location = new System.Drawing.Point(54 + (0), 55);
tbox1.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox1);
tbox1.BackColor = System.Drawing.SystemColors.InactiveCaption;
tbox1.TextChanged += new EventHandler(tbox1_TextChanged);
//*************************************BUTTON***********************************************//
Button button3 = new Button();
button3.BackColor = System.Drawing.SystemColors.Highlight;
button3.Location = new System.Drawing.Point(470, 55);
button3.Name = "button3";
button3.Size = new System.Drawing.Size(139, 23);
button3.TabIndex = 0;
button3.Text = "Calculate";
this.Controls.Add(button3);
button3.UseVisualStyleBackColor = false;
button3.Click += new System.EventHandler(button3_Click);
}//button2_click
//here i want to store into variable data that I enter into textbox
double var8;
private void tbox1_TextChanged(object sender, EventArgs e)
{
TextBox tbox = sender as TextBox;
var8 = Convert.ToDouble(tbox.Text);
}
//once the button3 is clicked, i want to display calculated data into textbox
double result2;
private void button3_Click(object sender, EventArgs e)
{
result2 = var8 * 2;
//get an error saying tbox does not exist in current context(what needs to be changed?)
tbox.Text = result2.ToString();
}

within your private void button3_Click(object sender, EventArgs e) there is no definition of tbox var! You may choose to define a global var and assigning the dynamically created textbox to it, or iterate through your Controls collection and find the according textbox!
So the first possible solution is
TextBox tbox = null;
private void tbox1_TextChanged(object sender, EventArgs e)
{
tbox = sender as TextBox;
var8 = Convert.ToDouble(tbox.Text);
}
private void button3_Click(object sender, EventArgs e)
{
result2 = var8 * 2;
if (tbox!=null)
tbox.Text = result2.ToString();
}

private void button3_Click(object sender, EventArgs e)
{
result2 = var8 * 2;
//get an error saying tbox does not exist in current context(what needs to be changed?)
TextBox tbox = this.Controls.Find("textboxid") as TextBox;
//tbox.Text = result2.ToString(); tbox is out of context
}

Related

Dynamically Create,Move And Resize The Controls

I have a Code Which Create a TextBox In Run Time and Also Resize and Move the pre-created Controls.
The Problem i am facing is i cannot resize or Move the Control which I create During the Run time.
Here is the Code.
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = cLeft * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
return txt;
}
private void button3_Click_1(object sender, EventArgs e)
{
AddNewTextBox();
}
private void button3_Click(object sender, EventArgs e)
{
ControlMoverOrResizer.Init(textBox1);
cboWorkType.SelectedIndex = 0;
}
The problem I am facing is I don't know how to refer the Newly created Textbox in
ControlMoverOrResizer.Init(textBox1);
I tried to Call
txt.Text
But it is throwing the error of
cannot convert String to Windows.form.controls.
Please Guide me where I am making mistake.
Thanks
You have to get a reference to the control. Either store it as a field in your form, Or give it a Name and find it later in the controls collection.
If you only add one text box then it's easy:
Method 1:
Add a field to your form, and assign it when you create the TextBox
private void button3_Click_1(object sender, EventArgs e)
{
this.myTextBox = AddNewTextBox();
}
Later:
ControlMoverOrResizer.Init(this.myTextBox);
Method 2: Give it name and find it dynamically later:
private void button3_Click_1(object sender, EventArgs e)
{
var txt = AddNewTextBox();
txt.Name = "MyTextBox";
}
Later:
ControlMoverOrResizer.Init(this.Controls["MyTextBox"]);
Note that if you add more than one TextBox you will have to adapt this code a bit
You need somewhere to reference from. Maybe add your textboxes to a dictionary.
private Dictionary<string, TextBox> dynamicTextBoxes = new Dictionary<string, TextBox>();
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
dynamicTextBoxes.Add($"tb{cLeft}", txt);
txt.Top = cLeft * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
return txt;
}
private void button3_Click(object sender, EventArgs e)
{
ControlMoverOrResizer.Init(dynamicTextBoxes[$"tb{cLeft - 1}"]);
cboWorkType.SelectedIndex = 0;
}
Note: this will only ever move the last added textbox!

How to execute event on dynamic generated text box in c# imminently when text box created

I created Dynamic Text Box using List in C# and i want to execute event immediately after text box is created
Following is my code.
private List<TextBox> txtTotalCost = new List<TextBox>();
private void btnMaterialAdd_Click(object sender, EventArgs e)
{
TextBox tbTotalCost = new TextBox();
tbTotalCost.Location = new Point(652, RowCount * 22);
tbTotalCost.Width = 60;
txtTotalCost.Add(tbTotalCost);
tbTotalCost.MouseClick += tbTotalCost_TextChanged;
panel1.Controls.Add(tbTotalCost);
}
void tbTotalCost_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
TotalCost = CanadianCost * Qut;
tb.Text = TotalCost.ToString();
return;
}
I want to generate or execute event as text box created i do now want to mouse click or text_change.
And want to display multiplication of two integer inside
how can i do that??
Just do this:
private void btnMaterialAdd_Click(object sender, EventArgs e)
{
TextBox tbTotalCost = new TextBox();
tbTotalCost.Location = new Point(652, RowCount * 22);
tbTotalCost.Width = 60;
txtTotalCost.Add(tbTotalCost);
tbTotalCost.MouseClick += tbTotalCost_TextChanged;
panel1.Controls.Add(tbTotalCost);
tbTotalCost_TextChanged(tbTotalCost, null); // <-- this line.
}

I want to get name of focused textbox controls that I created at run-time in WP 8.1

I am adding got focus event mytextboxes when I create them
`TextBox Xi = new TextBox();
Xi.Name = "X" + i.ToString();
Xi.Width = 10;
Xi.Height = 10;
Xi.GotFocus += Xi_GotFocus;`
But I can't get focused control's name
void Xi_GotFocus(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
is there any way get the name of these controls ? Thank you
you can use below mentioned code
void Xi_GotFocus(object sender, RoutedEventArgs e)
{
TextBox t = sender as TextBox;
string name = t.Name;
}

Find Control() not working

I have created 5 text boxes in a button click event and i have to get the values in the text boxes when the dynamically generated button is clicked.
protected void Button1_Click(object sender, EventArgs e)
{
for(int i=0;i<5;i++)
{
HtmlGenericControl tr = new HtmlGenericControl("tr");
HtmlGenericControl td = new HtmlGenericControl("td");
HtmlGenericControl tdbtn = new HtmlGenericControl("td");
TextBox txt=new TextBox();
txt.ID="txt_"+i.ToString();
td.Controls.Add(txt);
Button btn=new Button();
btn.ID="btn_"+i.ToString();
btn.Click+=new EventHandler(btnpay_Click);
btn.Text="Pay";
tdbtn.Controls.Add(btn);
tr.Controls.Add(td);
tr.Controls.Add(tdbtn);
PlaceHolder1.Controls.Add(tr);
}
}
But i couldn't get the Values in the text boxes at btnpay_Click
protected void btnpay_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn = sender as Button;
string[] splitvaues = btn.ID.Split('_');
string identity = splitvaues[1];
TextBox txt = new TextBox();
txt =PlaceHolder1.FindControl("txt_" + identity) as TextBox;
}
Can Anybody tell me a way to solve this problem?
Your problem is that FindControl doesn't recurse down the control tree. It only searches the controls directly in the ControlCollection of the container.
This method will find a control only if the control is directly
contained by the specified container; that is, the method does not
search throughout a hierarchy of controls within controls.
You need to write a recursive FindControl. Something like:
public static Control FindControlRecursive(this Control control, string id)
{
if (control == null || control.ID == id) return control;
foreach (var c in control.Controls)
{
var found = c.FindControlRecursive(id);
if (found != null) return found;
}
return null;
}
try this code.....
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
createcontrol();
}
}
private void createcontrol()
{
for (int i = 0; i < 5; i++)
{
HtmlGenericControl tr = new HtmlGenericControl("tr");
HtmlGenericControl td = new HtmlGenericControl("td");
HtmlGenericControl tdbtn = new HtmlGenericControl("td");
TextBox txt = new TextBox();
txt.ID = "txt_" + i.ToString();
td.Controls.Add(txt);
Button btn = new Button();
btn.ID = "btn_" + i.ToString();
btn.Click += new EventHandler(btnpay_Click);
btn.Text = "Pay";
tdbtn.Controls.Add(btn);
tr.Controls.Add(td);
tr.Controls.Add(tdbtn);
plh1.Controls.Add(tr);
}
}
protected void btnpay_Click(object sender, EventArgs e)
{
Button btn = new Button();
btn = sender as Button;
string[] splitvaues = btn.ID.Split('_');
string identity = splitvaues[1].ToString();
TextBox txt = new TextBox();
txt = plh1.FindControl("txt_" + identity) as TextBox;
string q = txt.Text;
}
protected void Button1_Click(object sender, EventArgs e)
{
createcontrol();
}

How to host datetimepicker in DataGridView control in C# winforms

private void showdate(DataGridViewCellEventArgs e)
{
dateTimePicker1.Size = vacation_transDataGridView.CurrentCell.Size;
dateTimePicker1.Top = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Top + vacation_transDataGridView.Top;
dateTimePicker1.Left = vacation_transDataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Left + vacation_transDataGridView.Left;
if (!(object.Equals(Convert.ToString(vacation_transDataGridView.CurrentCell.Value),"")))
{
dateTimePicker1.Value = Convert.ToDateTime(vacation_transDataGridView.CurrentCell.Value);
//dateTimePicker1.Visible = false;
}
dateTimePicker1.Visible = true;
}
This code in dgv cell_click event
There is an example of exactly this thing on MSDN.
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx
Unfortunately there isn't such a Cell or Column Type you can use (as far as i know). But Microsoft provides an example on how to get a NumericUpDown (that also doesn't exist) into a DataGridView.
Maybe you can adopt this example to get your DateTimePicker into a DataGridView.
private void dgtest_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
dtp = new DateTimePicker();
dgtest.Controls.Add(dtp);
dtp.Format = DateTimePickerFormat.Short;
Rectangle Rectangle = dgtest.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
dtp.Size = new Size(Rectangle.Width, Rectangle.Height);
dtp.Location = new Point(Rectangle.X, Rectangle.Y);
dtp.CloseUp += new EventHandler(dtp_CloseUp);
dtp.TextChanged += new EventHandler(dtp_OnTextChange);
dtp.Visible = true;
}
}
private void dtp_OnTextChange(object sender, EventArgs e)
{
dgtest.CurrentCell.Value = dtp.Text.ToString();
}
void dtp_CloseUp(object sender, EventArgs e)
{
dtp.Visible = false;
}

Categories

Resources