C# WPF textbox GotFocus SelectAll not working [duplicate] - c#

This question already has answers here:
How to select all text in TextBox WPF when focused?
(2 answers)
Closed 4 years ago.
I have a textbox with a GotFocus() event. The event is supposed select all text in the textbox, but it only works when I set a breakpoint on the textbox.SelectAll() command or when I step through the method. I've noticed that when I click on the textboxs bottom border it sometimes selects all.
Can anybody tell me what's going on?
The TextBox_GotFocus() method is as simple as it gets, but here it is:
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
textBox.SelectAll();
}
Edit:
My problem isn't that I don't know how to select all and the way I'm using it should work in my mind (I've used it the same way in other apps without problems). The problem is that it only works when I set a breakpoint in the method or step into the method in debugging but not when selecting the textbox in real time.

Following should help you to select all text.
private void TextBox_GotFocus(objetc sender, RoutedEventArgs e)
{
var txtControl = sender as TextBox;
txtControl.Dispatcher.BeginInvoke(new Action(() =>
{
txtControl.SelectAll();
}));
}

Related

Creating a Watermark in winforms [duplicate]

This question already has answers here:
Watermark TextBox in WinForms
(11 answers)
Adding placeholder text to textbox
(26 answers)
Closed 3 years ago.
I am working on a Winforms project and I would like to create a Watermark in a TextBox. I tried a code that seems to be the right solution but is incorrect.
There is my code :
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Veuillez entrez votre nom ici";
if (textBox1.GotFocus)
{
textBox1.Text = "";
}
}
And I get this error at "GotFocus" :
CS0079 C# The event can only appear on the left hand side of += or -=
I searched on google but I didn't find any solutions to help my case.
What should I use instead of "GotFocus" to create a Watermark ?
Best regards, Zancrew.
A lazy solution
create a label and locate it where you want your watermark to be shown and disable it
then paste code below on textbox's textchanged event
label_watermark.Visible = textBox1.Text.Length<1;

How to make event handler for key down even if form is not visible/closed? [duplicate]

This question already has answers here:
Set global hotkeys using C#
(3 answers)
Closed 3 years ago.
I'm making an app that will control my rgb lights and I want it to handle event for clicking specific key, but i want to run it as a background process.
I have already tried using this:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F9)
{
Debug.WriteLine("F9 down");
}
}
It works, but only if form window is active and visible, so I'm not satisfied. I'm looking for a solution that will work even if the form is invisible.
When a keydown event happens and a form is not focused / active it will not detect the event. However, you can use Win API to create a key listener of sorts. See this link for more details of "Global Hotkeys".

How to add a right click event to an object like button,label,listbox etc [duplicate]

This question already has answers here:
How to create a right click event handler in c#
(4 answers)
Closed 7 years ago.
I'm a beginner, so this may be easy. But i could not find it. I want to add a right click event to a windows form. but when i look properties/events menu of the form, I could not find any right click event. Is there a menu or code example for that? i prefer to use it auto constructed like double clicking for click event, so it is better to me if you can show me how can i find that property if it exists.
Thanks for helping.
its a mousedown event,
private void btn_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// do your stuff
}
}

How to make combo box auto expand on mouse hover and close when the mouse leaves combo box in c#? [duplicate]

This question already has answers here:
Winforms: how to open combobox properly?
(5 answers)
Closed 8 years ago.
I have a windows form. In that windows form i have a combo box. I have predefined items in the combo box as Add, Remove and Delete. I want to make the combo box auto expand on mouse hover. How can I do that? I noticed that auto expand code should be given in the mouse hover event of combo box. like this
private void comboBox1_MouseHover(object sender, EventArgs e)
{
}
but I don't know how to expand the combo box. Can anyone tell me how to do that?
Ok I got it done the expanding part
private void comboBox1_MouseHover(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
but I want to close the combo box when I leave the mouse pointer from the combo box.. How to do that?
Use the DroppedDown Property and make it true,
private void comboBox1_MouseHover(object sender, EventArgs e)
{
var box = sender as ComboBox ;
box.DroppedDown = true;
}
comboBox1.DroppedDown = true;
This has already been answered here Open ComboBox DropDown programmatically [duplicate]
which was actually a duplicate of Winforms: how to open combobox properly?

Show tooltip text by clicking a control [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Programatically show tooltip in winforms application
When set a TooltipText on a control, and the tooltip text will be shown when user move mouse on the control.
Now I have a picturebox on a windows form, what I want is to display the tooltip text by clicking the control instead of hover it.
How to do it?
Thanks.
This code will do the trick:
private void PictureBox1_Click(object sender, EventArgs e)
{
int durationMilliseconds = 10000;
ToolTip1.Show(ToolTip1.GetToolTip(PictureBox1), PictureBox1, durationMilliseconds);
}
Tooltip has a Show function. Just use that as necessary

Categories

Resources