Dynamically Create,Move And Resize The Controls - c#

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!

Related

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

How to make a MessageBoxButton move?

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You will need to create your own form and make it act like a messagebox. Instead of creating a MessageBox, you will instantiate your own form and so that you can handle the buttons on it.

C# dynamic box data read

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
}

Display a message in the font type and size selected by the user from two listboxes

I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}

Categories

Resources