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();
}
Related
private void btnCodeAkas_Click(object sender, EventArgs e)
{
tmrTimerAkas.Start();
tmrTimerTwoAkas.Start();
}
But I need to target only one button if it was clicked how do I do that?
well you can cast your sender to Control object so you can get the name of the button then you write your conditional logic.
private void button1_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control.Name == "button1")
{
Console.WriteLine($"Clicked {control.Name} button");
}
else if (((Control)sender).Name == "button2")
{
Console.WriteLine($"Clicked {control.Name} button");
}
}
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.
I am trying to empty the textbox when selected. I have multiple textboxes. How do i select the specific textbox dynamically? Below is my code:
private void newToDoTextBox_GotFocus(object sender, RoutedEventArgs e)
{
// Clear the text box when it gets focus.
newToDoTextBox.Text = String.Empty;
newToDoTextBox2.Text = String.Empty;
}
try this method please:
// use this method to your 2 textbox
private void yourTextBox_GotFocus(object sender, RoutedEventArgs e)
{
(sender as TextBox).Text = string.Empty;
}
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.)
I want to use month calendar date in text box after click on button for window form.
Calendar should be hide when form open and after click on button that calendar be visible.
I am using c# with visual studio 2008.So please give me response according this tool.
Use the DateTimePicker class for this. It has exactly the behavior you described.
Try to use Control.Show() method and Control.Visible property:
private MonthCalendar _monthCalendar;
public Form1()
{
InitializeComponent();
// invisible on startup
_monthCalendar.Visible = false;
_monthCalendar.MaxSelectionCount = 1;
}
private void button1_Click(object sender, EventArgs e)
{
//show when needed
_monthCalendar.Show();
}
private void textBox1_Enter(object sender, EventArgs e)
{
_monthCalendar.Show();
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (!_monthCalendar.Focused)
_monthCalendar.Visible = false;
}
private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e)
{
var monthCalendar = sender as MonthCalendar;
textBox1.Text = monthCalendar.SelectionStart.ToString();
}
private void monthCalendar_Leave(object sender, EventArgs e)
{
var monthCalendar = sender as MonthCalendar;
monthCalendar.Visible = false;
}