Accessing Dynamically created control (textbox) in a dynamically created eventhandler - c#

I am trying to access my dynamically created TextBox in C#, inside an event handler of a Button.
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
this.Controls.Add(t);
t.Location = new Point(60,40);
Label Mylable=new Label();
this.Controls.Add(Mylable);
Mylable.Location=new Point(15,43);
Mylable.Text="string : ";
t.Width=200;
t.Name="MyText";
t.Refresh();
Button Myb=new Button();
Myb.Location=new Point(270,40);
this.Controls.Add(Myb);
Myb.Text="Reverse it!";
Myb.Name="Mybo";
Myb.Click += new EventHandler(this.Myb_Clicked);
this.Refresh();
}
void Myb_Clicked(object sender, EventArgs e) {
// HOW SHOULD I GAIN ACCESS to MyText.Text HERE
MessageBox.Show();
}

Give a name to your dynamic TextBox:
TextBox t=new TextBox();
t.Name = "MyTextBox";
this.Controls.Add(t);
And then:
void Myb_Clicked(object sender, EventArgs e) {
string text = this.Controls["MyTextBox"].Text;
}

Wrong answer: object sender is the TextBox. You can cast sender to textbox and use it.
A decent way would be to make your textbox a class level member. And then you have access to it. If not, link TextBox.Text to a string property and use that.

You could keep a reference to your TextBox in your class
publc class MyForm: Form
{
TextBox myBox = null; // class member
void MainFormLoad(object sender, EventArgs e)
{
this.Width=600;
this.Height=400;
this.FormBorderStyle= FormBorderStyle.FixedDialog;
TextBox t=new TextBox();
myBox = t; // keep it for future reference
// rest of your code
}
void Myb_Clicked(object sender, EventArgs e) {
if (myBox !=null)
{
myBox.Text= "Clicked!";
}
MessageBox.Show();
}
}

Related

Place selected item from drop down list in to a text box

I want to get a selected item from a drop down list, on button click need to show that selected item into a text box.
I haven't tried anything cause i don't know what to do.
Just use this code...
Suppose, Button click event:
protected void Button1_Click(object sender, EventArgs e)
{
Textbox1.Text = DropDownList1.SelectedItem.Text.ToString();
}
The following class can give you the general idea of doing what you want.
public class MyClass
{
public MyClass()
{
comboBox = new ComboBox();
button = new Button();
textBox = new TextBox();
button.Click += OnButtonClick;
}
private void OnButtonClick(Object sender, EventArgs e)
{
textBox.Text = comboBox.SelectedItem.ToString();
}
ComboBox comboBox;
Button button;
TextBox textBox;
}

Passing variables in C#

I'm new to C#. I thought I knew a bit of C# but clearly not.
As an example I'm using a very plain form with a button and a custom textbox. Clicking on the button should give me the content of the custom textbox, but I'm getting
Error CS0103 The name 'tb' does not exist in the current context
I have tried all possible options provided but with no luck.
When I use a static textbox (named tb) from the toolbox then it works without any errors. Below is my code:
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Dock = System.Windows.Forms.DockStyle.Fill;
tb.Location = new System.Drawing.Point(600, 430);
tb.Multiline = true;
panel2.Controls.Add(tb);
}
public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
}
I have tried to search Google and Stack Overflow, but I'm not sure what to search for.
This is a problem of scope. You are declaring tb in your method, so outside the method it doesn't exist. You want to declare tb outside the method in the class itself:
TextBox tb;
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
tb = new TextBox();
tb.Dock = System.Windows.Forms.DockStyle.Fill;
tb.Location = new System.Drawing.Point(600, 430);
tb.Multiline = true;
panel2.Controls.Add(tb);
}
public void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(tb.Text);
}
Your tb variable is defined in the context of Form_Load(). Then it is added to the panel, then it goes out of scope. You need to find another way to get access to your text box... for example by making it a member variable of the class.

Get text from dynamicly created RichTextBox in onChange event

How can I get the text from a dynamicly created RichTextBox and a dynamicly created rtb_TextChanged Event?
e.g:
private void button1_Click(object sender, EventArgs e)
{
RichTextBox rtb = new RichTextBox();
rtb.Name = "rtb" + i;
rtb.Dock = DockStyle.Fill;
rtb.TextChanged += rtb_TextChanged;
Controls.Add(rtb);
}
void rtb_TextChanged(object sender, EventArgs e)
{
//string s = rtb.Text; //How can I get the rtb.Text?
}
You need to use the sender argument of your event handler:
void rtb_TextChanged(object sender, EventArgs e)
{
RichTextBox rtb = (RichTextBox)sender;
string s = rtb.Text;
//... etc
}
You just need to use the event parameter : sender
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
RichTextBox rtb = (RichTextBox)sender;
var str = rtb .Text;
}
First rtb is not the name that you called the textbox. Since the textbox sent the message you could cast the sender to a textbox and look at its text property.

Get instance of control whose event I am in

Is there a way to get an instance of the control whose event I am in?
private void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox1.Text = "hi";
ThisControl.Text = "hi";
}
Sort of so that these two lines would do the same thing? Like the "This" keyword but for the event control rather than the class.
The object sender parameter is a reference to the control that fired the event. Therefore, you could do something along the lines of:
private void textBox1_TextChanged(object sender, EventArgs e)
{
((TextBox)sender).Text = "hi";
// Or
TextBox txtBox = sender as TextBox;
txtBox.Text = "hi";
}

Changing the BackColor of a textbox upon entry

I have the following code for my form:
private void txt1_Enter(object sender, EventArgs e)
{
txt1.SelectAll();
txt1.BackColor = Color.LightBlue;
}
private void txt2_Enter(object sender, EventArgs e)
{
txt2.SelectAll();
txt2.BackColor = Color.LightBlue;
}
private void txt1_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
private void txt2_Leave(object sender, EventArgs e)
{
txtThermalConductivity.BackColor = Color.White;
}
There are another 20 textboxes on my form that I would like to do the same for. Is it possible to combine all of the enter events and all of the leave events so I have two events in total rather than 44 individual events?
In your Designer view, select each textbox and set the Enter and Leave events to point to a single implementation of each.
Then you can do this:
private void txt_enter(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.LightBlue;
}
private void txt_leave(object sender, EventArgs e) {
((TextBox)sender).BackColor = Color.White;
}
Also, SelectAll isn't required because you're setting the entire textbox's background color.. not the SelectionColor of a RichTextBox.
You could either add manually or iterate over all textboxes in form (extension method found here GetChildControls.
foreach (TextBox textBox in this.GetChildControls<TextBox>())
{
textBox.Enter += new EventHandler(TextBox_Enter);
textBox.Leave += new EventHandler(TextBox_Leave);
}
The above can be called from the Form's Load event.
The event listener now can look like the following by casting the sender to TextBox.
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox .SelectAll();
txtBox .BackColor = Color.LightBlue;
}
private void TextBox_Leave(object sender, EventArgs e)
{
TextBox txtBox = (TextBox)sender;
txtBox.BackColor = Color.White;
}
It is, just use something like the following:
private void tbLeave(object sender, EventArgs e) {
((TextBox) sender).BackColor = Color.White;
}
The set the controls event declaration to point to this function.
You can also do the same for the Leave() event.
(Just a little note to say, I much prefer to handle this kind of thing client side where possible.)

Categories

Resources