Get text from dynamicly created RichTextBox in onChange event - c#

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.

Related

C# make Event to RichTextBox in TabControl

I make RichTextBox in TabControl:
private void newModuleToolStripMenuItem_Click(object sender, EventArgs e) {
TabPage tab = new TabPage();
RichTextBox richText = new RichTextBox();
string promptValue = ShowDialog("Input File Name", "File name");
tab.Text = promptValue;
tabControl1.Controls.Add(tab);
tabControl1.SelectTab(tabControl1.TabCount - 1);
richText.Parent = tabControl1.SelectedTab;
richText.Dock = DockStyle.Fill;
}
and I would to make event TextChange to this RichTextBox.
You can just add the following to newModuleToolStripMenuItem_Click code:
richText.TextChanged += RichText_TextChanged;
Then define the event handler:
private void RichText_TextChanged(object sender, EventArgs e)
{
// add your handling code here ...
}
Or you can make your event handler in lambda expression:
richText.TextChanged += (sender, e) =>
{
// add your handling code here ...
};

How to select all text in textbox when it gets focus

In Windows phone, how can I select all text in Textbox when the TextBox has focus?
I try setting the get focus property of Textbox:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox .SelectAll();
}
What I see is I see all the text is being selected for 1-2 sec and then it goes back to cursor mode (i.e. 1 blink line).
I had this same problem on WPF and managed to solve it. Not sure if you can use what I used but essentially your code would look like:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox .CaptureMouse()
}
private void TextBox_GotMouseCapture(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.SelectAll();
}
private void TextBox_IsMouseCaptureWithinChanged(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.SelectAll();
}
All events hooked up to the original textbox. If this doesn't work for you, maybe you can replace CaptureMouse with CaptureTouch (and use the appropriate events). Good luck!
You can try this code,
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
String sSelectedText = mytextbox.SelectedText;
}
If user clicks on copy icon that comes after selection it will get copied, if you want to do it programmatically you can try this
DataPackage d = new DataPackage();
d.SetText(selectedText);
Clipboard.SetContent(d);
I would suggest doing the copying in some other event rather than gotfocus, as this will be triggered immediately after user taps on text field so this method will be called when there is no text actually entered.
protected override void OnStartup(StartupEventArgs e)
{
//works for tab into textbox
EventManager.RegisterClassHandler(typeof(TextBox),
TextBox.GotFocusEvent,
new RoutedEventHandler(TextBox_GotFocus));
//works for click textbox
EventManager.RegisterClassHandler(typeof(Window),
Window.GotMouseCaptureEvent,
new RoutedEventHandler(Window_MouseCapture));
base.OnStartup(e);
}
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).SelectAll();
}
private void Window_MouseCapture(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
textBox.SelectAll();
}

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

Accessing Dynamically created control (textbox) in a dynamically created eventhandler

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

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