how to auto focus an .Net winform application running in background - c#

I have a weird problem, my .net win form application triggers a third party CRM application. When the customer is working with CRM application and clicks some button in my application, on first click the button does not trigger the event, only on the second click it responds.
The reason I suspect is that my application is not in focus.
I tried with the following code
private void XXXXX_MouseHover(object sender, EventArgs e)
{
this.BringToFront();
this.Focus();
}
But then its not working. I am new to .Net can any one point me how to fix this issue?
Any help is highly appreciated.
update: my toolstrip button.
btnbutton.Click += delegate(object sender, EventArgs e)
{
//some code to execute on button click.
};
Thanks

The .NET tool strip ignores clicks when the application doesn't have focus. This is just like how Words work. The idea here is that the user can click "anywhere" in the window without having to worry that he actually performs an action. Only when the window has focus will the click "count".
The post at http://blogs.msdn.com/b/rickbrew/archive/2006/01/09/511003.aspx describes how you can work around this. Basically, you inherit from ToolStrip and override the WndProc, and change the WM_MOUSEACTIVATE with the MA_ACTIVATEANDEAT result to the MA_ACTIVATE result.

Related

MaskedTextBox.SelectAll on GotFocus doesn't work with mouse

I want to select all the contents of a MaskedTextBox when the clicks (or tabs onto) the control, so they can easily replace the old content. I tried calling SelectAll() in the Enter event, but that didn't work at all.
I switched to using the GotFocus event, which works great when tabbing through controls, but doesn't work when I click on it with the mouse. I would only want to select all the contents when first entering/focusing on the control (subsequent clicks might be used to position the cursor to edit the existing text).
I added a button and tried calling SelectAll() in the button click event, but that didn't do anything either. What's going on? Is this a bug?
How can I get around this?
Steps to reproduce
Create a new Windows Form Application in .NET 4.0 in Visual
Studio 2010.
Add a TextBox, MaskedTextBox, and Button to the default form
Change the Mask property on the MaskedTextBox to "_____".
Add some event handlers:
private void maskedTextBox1_GotFocus(object sender, EventArgs e)
{
Debug.WriteLine("GotFocus");
maskedTextBox1.SelectAll();
}
private void button1_Click(object sender, EventArgs e)
{
Debug.WriteLine("Click");
maskedTextBox1.SelectAll();
}
Run the program, entered some data into the MaskedTextBox, tab through controls back to it. It selects the contents of the MaskedTextBox.
Select the other TextBox. Try clicking on MaskedTextBox. Output shows that GotFocus event was called, but text doesn't get selected.
Try clicking on button in form. Text doesn't get selected.
Tested in Visual Studio 2010 with .NET 4.0 in a Windows Forms Application project
Why this isn't a duplicate of TextBox.SelectAll() does not work with TAB
If you notice, the title says "SelectAll doesn't work with TAB". In my case, it does work with Tab, it doesn't work with the mouse - completely opposite scenario. The answer for that question is to use the GotFocus event. I'm already using the GotFocus event, but it doesn't work. That answer does not answer this question. It is clearly not a duplicate. If I'm wrong, please explain in the comments.
Your SelectAll() is being overwritten by the default functionality of the masked textbox select. I would use the Enter event, it allows for tabbed entry or mouse click entry to the masked text box. You will most likely need to use the BeginInvoke method. Try the code below. It worked for me when I tried...
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
BeginInvoke((Action) delegate { SetMaskedTextBoxSelectAll((MaskedTextBox) sender); });
}
private void SetMaskedTextBoxSelectAll(MaskedTextBox txtbox)
{
txtbox.SelectAll();
}
Executing Focus before Select All worked for me:
private void Masked_Enter(object sender, EventArgs e) {
((MaskedTextBox)sender).Focus();
((MaskedTextBox)sender).SelectAll();
}

What is the event which fires every time when i click outside for my wpf page?

i am developing windows application using wpf. I want to do some functionality when i click outside of my control created. for example, if i have Message-box open in my window, i want to do some function if i click outside of my Message-box window.
I tried,
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
.....
}
but its not working.. please any one tell me, what is the event fire when i click outside of my control?
you have two supposed solutions:
one of them is to get Mouse.X, Mouse.Y from System not from application, this article will help
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
second which is better is not using Dialog, but use PopUp window and this article will help
how to close a WPF Dialog Window when the user clicks outside it

How can I make MouseWheel work without first having to focus the form?

I have a form with only a ReportViewer control on it. When the form is displayed, if you click on the report you can then use the mouse wheel to scroll vertically.
I'd like to be able to scroll as soon as the form appears.
I've tried the following, but no dice...
private void ReportViewer_Load(object sender, EventArgs e)
{
rptViewer.Focus();
}
private void ReportViewer_Activated(object sender, EventArgs e)
{
rptViewer.Focus();
}
Put your code in the form's constructor, right after InitializeComponent();:
rptViewer.Select();
After set rptViewer.Focus call SendKeys.Send(Chr(Keys.Tab)) to move focus from menu to preview area.
Did you try calling rptView.Activate()?
Also it may be that your form is getting focus after the load event completes (I think I've had problems with that before). One solution is, although it is definitely not elegant, to create a single-use Timer that starts when your Load method runs, and fires after 1 ms, and then stops. When the Timer fires, it will activate/focus your ReportViewer.
You could also try adding a MouseWheel event handler to your form. When the event is fired, send a scroll message to your ReportViewer to scroll it up or down. Then it doesn't matter whether or not your ReportViewer has focus, it (should) always scroll when the form has focus.

C# mouse click event after focus in windows 2012

I find a strange bug in windows 2012. I have a simple window (WinForm) with a text box and a button (textBox1 and button1).And I try to focus on textbox1, after form appear.
private void Find_Paint(object sender, PaintEventArgs e)
{
textBox1.Focus();
}
And if I set it Click and MouseClick events stop working. So I can't click on button.
In windows 2008 it's work. If comment focus line - works too.
Who can suggest a solution or perhaps an alternative? Need to get the cursor in the textbox after the form has appeared
You should use the Shown event instead:
private void Find_Shown(object sender, EventArgs e){
textBox1.Focus();
}
Note: you used Paint event which will be very nasty, everytime your form is repainted, your textBox1 will be focused, the Paint event is fired every time your form resizes, state changes, ... we can't determine exactly the time it fires but it fires fairly frequently when your form is running. That is the reason why you can't click on and select anything on your form. That's because clicking or selecting controls fires the Paint event and makes your textBox1 focused then.

Using widgets in MonoDevelop and GTKSharp

I switched from using VirtualStudio Express 2010 and I am trying to work with MonoDevelop and GTKSharp. Now. I am trying to switch to new editor software but it seems much different than VisualStudio.
What I am trying to do is basically to use widgets in this editor. For example when I am creating a button in VisualStudio and then double click the item then I am automatically getting the piece of code representing the item in the form. And here is the problem, how do I create events for buttons and comboboxes in MonoDevelop? I was looking through Internet examples for two days now and I can not figure out how to do it. The examples are not clear enough.
What I am trying to create? First I am trying to figure out how to use ComboBox and button which will allow me to choose one of 3 options in ComboBox and then under button event I want to fire 1 of 3 separate windows depending on which item has been picked.
Please provide me some easy examples how to work with MonoDevelop or else I will need to switch back to Windows OS :(
Please help!
// edit //
Lets say I got time on my hands and I am really interested in it. So if GTK# allow me so far:
public MainWindow () : base(Gtk.WindowType.Toplevel)
{
Build ();
button1.Clicked += button1_Click;
combobox1.SelectionGet += comboBox1_Selection; << is this correct?
}
private void button1_Click(object s, EventArgs e)
{
}
private void comboBox1_Selection (object s, EventArgs e)
{
switch (combobox1.SelectedIndex)
{
case 0:
window1.Show();
break;
case 1:
window2.Show();
break;
case 2:
window3.Show();
break;
}
}
But I feel like I am more lost than I was earlier.
Create a new C# GTK project.
Open up the "MainWindow.cs" and at the bottom right of the window click the "Designer" button to go into designer mode.
Next open up the hidden toolBox window at the right of the MonoDev window. Drag out a "Fixed" Container on the main windows canvas. This is required to put Buttons and stuff on your window.
Now drag a button on the Fixed container. To move the button around click on the little white box above the button when selected.
Now go to your SourceCode again. In the constructor write::
button1.Clicked += button1_Click;
Then make the new Click method.
private void button1_Click(object s, EventArgs e)
{
}
You don't need to add the handlers manually...
In Gtk the concept names changes a little, when a widget which is the same as a control in windows forms, just different name, does something it emits a "signal" then what you have to do is to "handle" that signal which would be the equivalent to catch the event in windows forms.
You can just select your widget in monodevelop and then go to the properties pane of that widget, there you will see a tab called "signals", this has the list of the signals that the widget specifically emits, thus allowing you to code actions when the widget does something like being clicked. Once there just double click in the signal you want to handle, for example, for a button, double click on the "Released" signal which is the signal emitted by the button when you click on it and release it.
I'm attaching a screenshot so you can get the picture. Hope it helps!!

Categories