I need to detect whenever the MenuStrip (NOT a contextmenu) is opened and closed. The Enter and Leave events do not seem to work.
Why I need it:
I use a MenuStrip with options like "File", "Edit", "About", etc. But when the menu strip is active, and the user navigates through it using either the mouse or keyboard the other controls on the Windows Form also respond to it.
For example: I click the "Edit > Paste Special.. > Unformatted text" in my application using the mouse. But below the expanded menu item is a XNA 2d-rendering control that also detects the mouse input and does something that I don't want it to do. Now if I could just detect whenever the menu is opened I could disable/enable the appropriate controls.
Without knowing a little more about what you are doing, you can try the code below:
private void Form1_Load(object sender, EventArgs e)
{
menuStrip1.GotFocus += new EventHandler(MenuStrip1_GotFocus);
menuStrip1.LostFocus += new EventHandler(MenuStrip1_LostFocus);
}
private void MenuStrip1_GotFocus(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void MenuStrip1_LostFocus(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
textBox1.Text = "Has Focus";
}
private void menuStrip1_MenuDeactivate(object sender, EventArgs e)
{
textBox1.Text = "Lost Focus";
}
This seems to be working for what you have described above. If you have the menu strip with tab stop on (true) then the got focus events will handle this case. If you only need to make changes if the mouse is used then Menu Active events should work.
Related
I want to know if there is any event when you click on the rest of the screen and not the Windows Form the Form closes. Is it because of the ShowDialog()?
My Main Form is just with a notifyIcon and when I click it I call Form1.ShowDialog();
Main.cs:
private void ShowForm1(object sender, EventArgs e)
{
form1.Left = Cursor.Position.X;
form1.Top = Screen.PrimaryScreen.WorkingArea.Bottom - form1.Height;
form1.ShowDialog();
}
Form1.cs:
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "Test";
}
You need to run the dialog box non-modally, not modally. Think about it, when you run it modally, the dialog box takes over the UI and plays games with mouse-clicks elsewhere, preventing you from running. You don't want it to be modal anyway.
I created a simple Windows form with a button that includes this handler to open a small AutoCloseDialog form I created (and populated with a few controls):
private void button1_Click(object sender, EventArgs e)
{
var dlg = new AutoCloseDialog();
dlg.Show(this); //NOTE: Show(), not ShowDialog()
}
Then, in the AutoCloseDialog form, I wired up the Deactivate event. I did it in the designer (where this code is generated):
this.Deactivate += new System.EventHandler(this.AutoCloseDialog_Deactivate);
Finally, here is the handler for the Deactivate event.
private void AutoCloseDialog_Deactivate(object sender, EventArgs e)
{
this.Close();
}
I think this does what you are asking.
Yes, you can create a similar function like this, which closes the Form if the Form lost focus (in Form1.cs)
public void Form_LostFocus(object sender, EventArgs e)
{
Close();
}
and then you add the LoseFocus EventHandler (in Form1.Designer.cs):
this.LostFocus += new EventHandler(Form_LostFocus);
I have a windows forms application containing a button and a textbox. At "button_click_event" this code is executed:
System.Diagnostics.Process.Start("osk.exe");
The keyboard shows, yet no text appears on the textbox as I type on said keyboard.
What is happening is that the on screen keyboard is sending information to the Focused Control which in your case is the Button that initiated the Keyboard. Try setting focus to to your textbox after creating the keyboard.
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("osk.exe");
//SetFocus to your TextBox here
textBox1.Focus();
}
To close it do something like this
private void button2_Click(object sender, EventArgs e)
{
var procs = Process.GetProcessesByName("osk");
if (procs.Length != 0)
procs[0].Kill();
}
I am hoping someone here can help me, i have a Tabless Control on my windows forms application and basically because the tabs are purposely hidden i have added 2 buttons to each tab "Next" and "Back".
This is the code snippet i have for my "Next" button:
private void nextbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
this.toolStripStatusLabel8.Text = System.DateTime.Now.ToString();
}
Which works fine, however when i use the exact same theory on the "Back" button it does not work:
private void backbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabmain;
this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
}
So my question is how does one go to a previous tabpage from a button? I have looked through here and tried all of the links that came up but nothing has worked any ideas?
You should use the SelectedIndex property instead of using concrete TabPage instances. This way it will still work when you decide to change the order of the pab pages or add new pages:
private void previousButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex > 0)
{
tabControl1.SelectedIndex--;
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1)
{
tabControl1.SelectedIndex++;
}
}
Since there is no "Tabless" tab control in .NET Framework I can only assume that it works similar to the standard TabControl. If the solution doesn't work you should give us some information about the actual class you use.
BTW: There is no need to repeat the buttons on each page. Why don't you just put the buttons outside the TabControl?
Also: I see that you use a ToolStripStatusLabel to show the current time. Instead of updating it each time the user clicks somewhere add a Timer to your form. Set its Interval to 1000 and handle its Tick event. Update the label there:
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
}
This way it updates constantly and again there is no need to repeat anything. You need to call timer1.Start() in the form's constructor.
guys how can I prevent the opening already open WPF window (without using the user control and not adding in to windows form) when button press event ?
I have following codes, but each time I press the button it will open WPF window according to button pressed amount.
Dose anybody know how to prevent that error
Thank you,
private void button1_Click(object sender, RoutedEventArgs e)
{
win2 v2 = new win2();
v2.Show();
}
here , when each time i click the above button it will open a window, but when its already open; if I clicked the same button it will open another window instead of focusing to the already open window.
how can I prevent that?
(I'm using C# )
private void button1_Click(object sender, RoutedEventArgs e)
{
if(!Application.Current.Windows.OfType<win2>().Any())
{
win2 v2 = new win2();
v2.Show();
}
}
Application.Current.Windows lists all the currently open windows owned by your program. We can check if any are already open that are of the type win2, and if not, then we create a new one.
win2 v2 = null;
private void button1_Click(object sender, RoutedEventArgs e)
{
if (v2 == null)
{
v2 = new win2();
v2.Show();
}
else
v2.Activate();
}
You can use Application.Current.Windows to find all the open windows in your application.
If it contains your Window type Activate it to bing it into focus, if the collection does not contain your Window create a new one
private void button1_Click(object sender, RoutedEventArgs e)
{
if (Application.Current.Windows.OfType<win2>().Any())
{
Application.Current.Windows.OfType<win2>().First().Activate();
}
else
{
new win2().Show();
}
}
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();
}
}