my simple WinForms app consists of one Form which has 2 important methods:
private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = true;
}
keyTimer.Start();
}
private void MainWindow_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
{
qKeyPressed = false;
}
keyTimer.Stop();
}
In form's constructor I use button1.TabStop = false; to set button1 focus to false, so my form can handle methods KeyUp and KeyDown. I want my form to be focused even after button1 click event. I tried:
button1.TabStop = false;
this.Focus();
in private void button1_Click(object sender, EventArgs e) but this doesn't seem to work and button after click looks like focused and methods KeyUp and KeyDown are disabled. How can my problem be solved? Thanks in advance
Run this in the button click function:
this.ActiveControl = null;
When you click the button, you set it as the active control. Removing this property will let you put focus on your form.
Related
This seems very basic but have not been able to find any information on it.
I have a button in a Windows form that is the AcceptButton and should always have the AcceptButton behavior i.e. pressing the enter key when focus is on the form should always click the button and it should always be highlighted to show that action is available.
However, when a different button is clicked, that button is now highlighted and when enter is pressed, that button is clicked instead of the AcceptButton.
Here is my test application that shows the behavior:
public Form1()
{
InitializeComponent();
AcceptButton = btnAccept;
}
private void btnAccept_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked the accept button");
Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
}
private void btnNothing_Click(object sender, EventArgs e)
{
Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
}
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
Debug.WriteLine(AcceptButton == btnAccept ? "true" : "false");
}
When the application launches initially - it works as I expect, btnAccept is highlighted and enter clicks that button.
However, when I click btnNothing or btnBrowse, that button is now highlighted and pressing enter clicks that button again. Additionally, the debug lines always show that btnAccept is still the AcceptButton even when the enter button is clicking a different button.
The CanFocus property that Button inherits from Control looks promising, but this property is read only.
What's going on here? Is there some property I can use to change this behavior?
Edit
Ended up going with this solution based on #AWinkle 's answer:
public Form1()
{
InitializeComponent();
AcceptButton = btnAccept;
}
private void btnAccept_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked the accept button");
}
private void btnNothing_Click(object sender, EventArgs e)
{
Button acceptButton = (Button)this.AcceptButton;
acceptButton.Select();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
DialogResult result = folderBrowserDialog1.ShowDialog();
Button acceptButton = (Button)this.AcceptButton;
acceptButton.Select();
}
I know you will be thinking "Not again this question", as I found like a hundred results when I searched for it. But when I put in the code as described on the pages here, it just minimizes to right above the start menu.
This is the code I use (I added a message box to see if the code gets triggered, but the message box never pops up):
private void Form1_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Because I don't know if it links to Form1 or Form, I have tried both, to no avail.
private void Form_Resize(object sender, EventArgs e)
{
MessageBox.Show("Works");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
Now, when you double click on the Form, it puts this line in the Form1.Designer.cs:
this.Load += new System.EventHandler(this.Form1_Load);
Do I need a similar line to trigger the minimize event?
As you can see, I am completely lost :)
Oh, and it doesn't minimize to the taskbar, as I am using the following code to hide the form on run:
protected override void OnLoad(EventArgs e)
{
Visible = false; // Hide form window.
ShowInTaskbar = false; // Remove from taskbar.
base.OnLoad(e);
}
You need the event
private void Form1_Resize(object sender, EventArgs e)
{
}
Creating Event Handlers on the Windows Forms Designer
Add a NotifyIcon component to your Form. Make sure you set an icon via the properties pane otherwise it will be invisible.
Create an event handler for the form's Control.SizeChanged event. In that event handler place the following code:
sample code:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
ShowInTaskbar = false;
}
And then to make the form visible again the NotifyIcon.MouseDoubleClick event handler you can place the following code:
private void trayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
WindowState = FormWindowState.Normal;
ShowInTaskbar = true;
}
The basic thing you need to know is events. Events are triggered when certain things happen to your form (or any control). For example, when the form is resized, or loaded, or clicked, an event is raised. You can hook into this event to execute your own code when the event happens.
In your case you want to execute code to minimize the form, on the event that the form is resized. So you need to hook your method to the resize event. The name of your method is not relevant, so let's use a better name:
private void HideWhenMinimized(object sender, EventArgs e)
{
MessageBox.Show("Works1");
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
To hook your HideWhenMinimized method into the Resize event of the form, you have to do it like this:
this.Resize += new System.EventHandler(this.HideWhenMinimized);
If you add that line of code in the form's constructor or Load event, then your code gets called as soon as the form is resized.
Code in User Control (Windows User Control).
I have a button and when click on the button some logic will fire.
below is button event.
private void btnCancel_Click(object sender, EventArgs e)
{
// Some Logic
}
I have Main form (Windows form)
I am adding the above user control in a Panel of my Main Winform.
From here I want to invoke the user control btnCancel_Click event using some function Keys Like (F3,F4).
This is a very new thing for me. Please help me in this regard.
Thanks .
Krishna
Set your parent form KeyPreview attribute to true and then in the KeyDown event of your parent form, look for if(e.KeyCode == Keys.F3), then write a public method for your usercontrol which fires the btnCancel_Click. You can access it from your parent form.
Parent Form:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
MyUserControl.FireButtonEvent();
}
}
Usercontrol:
public void FireButtonEvent()
{
this.btnCancel_Click(null, EventArgs.Empty);
}
Create a KeyEvent method. for example:
private void KeyDown(Object sender, KeyEventArgs e)
{
string sKey = e.KeyValue.ToString();
if (sKey==112) {//SomeCode;} // F1
}
In the designer code link the method to a key press event on the main form:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyDown);
Look on the net for the key values of the button you want to activate it,
F1 to F24 are values 112 to 135.
I have a button on my WinForms app that I want to be invisible until the user moves his mouse over the button. Then they could click it. If the mouse leaves the button, it needs to be hidden again. The button.Visible parameter makes the button completely inaccessible and disables the mouse over. Any ideas or other button parameters I could use?
This currently does not work:
private void settingButton_MouseEnter(object sender, EventArgs e)
{
settingButton.Visible = true;
}
private void settingButton_MouseLeave(object sender, EventArgs e)
{
settingButton.Visible = false;
}
This issue was brought up and answered here:
C# WinForms MouseHover and MouseLeave problem
private void Form_MouseMove(object sender, MouseEventArgs e) {
if(settingButton.Bounds.Contains(e.Location) && !settingButton.Visible) {
settingButton.Show();
}
}
I have a winform form which has typical OK and Cancel buttons.
The OK button is set as the default button. When the user presses the ENTER key, the form is closed.
The form also has a text box which has a Search button beside it. The text box allows the user to enter search text and the Search button initiates the search function.
I would like the user to be able to press the ENTER key when the text box has the input focus and have the search function activate.
The problem is that the form is grabbing the ENTER key event before the text box's handler gets it.
EDIT: I've tried using the following event handlers, but they never get hit, and the form closes when the Enter key is hit:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
private void txtFilter_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
e.Handled = true;
}
}
I've also tried enabling the KeyPreview property on the form and implementing a KeyDown event handler for the form, but the Enter key never seems to cause any of these to be hit.
What is the best way to accomplish this?
Try handling the Enter and Leave events of the TextBox to clear out your form's AcceptButton property:
private void txtFilter_Enter(object sender, EventArgs e) {
this.AcceptButton = null;
}
private void txtFilter_Leave(object sender, EventArgs e) {
this.AcceptButton = closeButton;
}
Then you can just process your KeyUp event as your want:
private void txtFilter_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Search();
}
}
Add a KeyDown event handler to the textBox and then add this to it
if (e.KeyCode == Keys.Enter)
{
btnSearch.PerformClick();
}