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;
Related
This question already has answers here:
C# - Add button click events using code
(3 answers)
Closed 3 years ago.
I very new to C# and I would like to add a Button to a form programmatically. When this button is pressed I want it to run some code (like a normal button woukd work). But, everywhere I have searched only shows how to add the button and not how to add code to the button.
One way to do it is to give the button a name property, then in your code you "refer" to the button by it's name like so:
void btnButton_Click(object sender, EventArgs e)
{
// your code goes here
}
where btnButton_Click is the name of the button.
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();
}));
}
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
}
}
This question already has answers here:
How can I unmask password text box and mask it back to password?
(9 answers)
Closed 9 years ago.
I am having a textbox for password field, I want to add a Checkbox that toggles the password into plain text.
I did the following coding for it.
txtpwd.PasswordChar = (char)(byte)32;
but this simply hides not showing the plain text of what is typed in textbox field.
any body can help?
Here is the solution, I added CheckBox with name chkpwd and on its CheckedChange event i added this code
private void chkpwd_CheckedChanged(object sender, EventArgs e)
{
if (chkpwd.Checked)
txtpwd.PasswordChar = '\0';
else
txtpwd.PasswordChar = '*';
}
This will show plain text when checkbox's checked property true
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