This question already has answers here:
Panel not getting focus
(6 answers)
Closed 7 years ago.
I have got some controls on the Panel and I am trying to delete them using "Delete" button. I handled KeyPress Event as mentioned in How to get Keypress event in Windows Panel control in C#
and I am getting Event for buttons (A-Z and 1-9) pressed, but not for the Delete, Control/Alt/ Shift or F1, F2.... buttons.
Do we need to do something special for handling these buttons?
Try like this:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
e.Handled = true;
}
}
Also you need to set KeyPreview on.
You can also refer Keyboard.Modifiers Property
This question already has answers here:
How to change menu hover color
(4 answers)
Closed 7 years ago.
So I have a MenuStrip in C# which I am trying to do a darkish theme for, but when I press the button for the dropdown menu well....
Is there a way to make it go from white to another color? I can't seem to figure out a way to do it. This is probably my first time even customizing context menus.
You could use MouseHover and MouseLeave event. It's easy. Just do the following steps:
We have a form with these items: http://s3.picofile.com/file/8188577184/Capture.JPG
Choose that dark backcolor for ToolStripMenuItem. I choosed black color for fileToolStripMenuItem in my example.
Use this for MouseHover event:
private void fileToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
fileToolStripMenuItem.BackColor = Color.White;
fileToolStripMenuItem.ForeColor =Color.Black;
}
Use this for MouseLeave event:
private void fileToolStripMenuItem_MouseLeave(object sender, EventArgs e)
{
fileToolStripMenuItem.BackColor = Color.Black;
fileToolStripMenuItem.ForeColor = Color.White;
}
I am coding a C# Forms application and I have a question about validating a form.
I have a textbox called textBoxCodeToSearchFor that I want to validate when I press a button. If the textbox does not have any text in it, I need a tooltip to be shown.
Here is my code:
private void textBoxCodeToSearchFor_Validating(object sender, CancelEventArgs e)
{
if (String.IsNullOrEmpty(textBoxCodeToSearchFor.Text))
{
e.Cancel = true;
toolTip.Show("Please enter the code to search for", textBoxCodeToSearchFor);
}
}
On the button click I have the following code:
bool validated = this.Validate();
The tooltip is then shown, however, I cannot close the form when pressing a cancel button.
How can I shown a tooltip for a textbox if the textbox does not validate, but still close out of the form is wanted?
Thanks in advance.
I need to set focus to a textedit on Button Click event ? in my Form
i tried this one but not working TXE_FlatDiscountP.Focus(); How to set focus on event ?
Your way is correct. Maybe you are missing something.To be more detailed here is an example:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Focus();
}
This is an example of setting focus to a textbox.
This question already has answers here:
Watermark TextBox in WinForms
(11 answers)
Closed 9 years ago.
I have a windows form with textfields. I would like that the textbox shows sample text and when i click on the textbox, the text is cleared. What is the best way to do this?
e.g. the form shows text: 192.18.130.44, as the user clicks on that textbox, the text is cleared.
I think you want to show the default Text of a textbox, if it's just focused without any editing, the default Text will be restored when it loses focus like this:
string initText = "Love .NET";
bool edited;
//This code line is just for demonstrative purpose, it should be placed such as in the Form constructor
textBox1.Text = initText;
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
edited = !char.IsControl(e.KeyChar);
}
private void textBox1_Enter(object sender, EventArgs e)
{
if(!edited) textBox1.Clear();
}
private void textBox1_Leave(object sender, EventArgs e)
{
if (!edited) textBox1.Text = initText;
}
If you want to make the text look like watermark, you may want to apply more Font and ForeColor accordingly or some custom Paint if needed. The last is using a third-party textbox, it's up to you.
Use the Enter event:
private void textBox_Enter(Object sender, EventArgs e)
{
textBox.Text = null;
}
Although, unless you want it to always clear, I'd put some validation in there as well!
BTW...that link for CueProvider looks pretty slick, too, if you don't mind 3rd party stuff.
To make it show the sample text, set the text property in the properties menu to what you want, e.g. 192.18.120.44
To make it clear upon click, create a method for the click event and do txtbox1.Text = "";
You can initiate this method by double clicking the text box.
When you load your winforms, check if the textbox is null or empty.
Then, if it is, show your sample text, and set a boolean to true(false if text is not empty).
Then, write a click event on you textBox, that cleared content if boolean is true and nothing if boolean is false, to avoid clear the textBox, if he contains other things that your sample..