This question already has answers here:
Show a Form without stealing focus?
(19 answers)
Closed 6 years ago.
I have a sort of a pop-up windows form. It pops up to show gathered data. Problem is it gains focus when it loads and interrupts work. I need it not to get focus when loaded. How to do that?
the Simplest thing you can do is, first load all the data which you gathered and when u feel its ready and you want to load the popup window, call the
window.show()
this will help you to use the data once its loaded. Also it would be better if you can share more details on the same.
In Your form class, override OnLostFocus(EventArgs e) and place a focusing call inside it. So it looks like this:
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
this.Focus();
}
Sometimes if that doesn't work try:
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.Focus();
}
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 an answer here:
Application 'Deactivate' event
(1 answer)
Closed 8 years ago.
I want to create an application that closes one of it's window when the user clicks on another application (or in other words the application loses focus or inactive). All the windows in my application to have the property TopMost = true.
How I can do that?
your help is greatly appreciated.
Edit (Update):
I need when I click on another window in my application, the window is not closed. Only when my application loses focus (the user clicks on another application), the window will be closed.
Check out the Deactivate event, or override OnDeactivate:
protected override void OnDeactivate(EventArgs e)
{
Close();
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
(C#) How to detect when a windows form is being minimized
First of all my code:
private void Form1_Load(object sender, EventArgs e)
{
this.MinimumSizeChanged += new EventHandler(Form1_MinimumSizeChanged);
}
void Form1_MinimumSizeChanged(object sender, EventArgs e)
{
MessageBox.Show("1");
}
so, my program idea is, when the program get minized i will do a system tray, but this event never happnd. how can i know when the user do a minize screen(minize the program).
i have tryed everything. any ideas?
MinimumSizeChanged has nothing to do with the form getting minimized. MinimumSizeChanged has to deal with when the MinimumSize properties of the form are changed.
You want to check the Resize event of the form.
In order to minimize to the system tray, add a NotifyIcon control to your form. Now, override the OnResize method and check if your WindowState property is set to FormWindowState.Minimized. If it is, Hide() your forms and show your notify icon. Make sure to set the Icon property of the NotifyIcon as well. Now just reverse the process for the case when the form is restored.