Called WinForm not getting focus - c#

In the form calling the 2nd form I have this code:
this.WindowState = FormWindowState.Minimized;
frmCalledForm frm = new frmCalledForm();
frm.Show();
this.WindowState = FormWindowState.Normal;
And in the Load() event of frmCalledForm
this.TopMost = true;
this.BringToFront();
this.Activate();
I have the code calling the 2nd form in a button click event and at one point I open it programmatically based on certain conditions.
In both cases, the called form does not get focus, the calling form retains focus, even though it is minimized and then returned to normal.
I found the above code in this SO question, and apparently it's working for him, but for some reason it's not working for me.
Due to Chris's comment about returning the calling form to Normal state, I commented that line out, but left the line minimizing the calling form.
Besides the fact that, to the user, the main form will suddenly disappear to his taskbar and stay there, which obviously doesn't make for a good user experience. But when I ran the program with the modified code, the main form went to the taskbar, and the called form opened, but still did not have focus. I was quite surprised to see that.

Related

Can't Focus To Any Control after Closing Dialog

I am showing some database records in a Dialog. When I click any particular record that record fills to Active Form. But I want to focus to a button when my dialog close. So I have written the following code on form closing evennt.
private void frmDG_RecordSelection_FormClosing(object sender, FormClosingEventArgs e)
{
RecordSelectionStatus.Text = "False";
Form TargetForm = Home.ActiveMdiChild;
Button SelectRefConsultant = (Button)TargetForm.Controls.Find("btnSelectRefConsultant_NI", true).SingleOrDefault();
SelectRefConsultant.Focus();
TargetForm.ActiveControl = SelectRefConsultant;
}
But it's not working. Focus still remain to it's previous place. What am I missing ?
I am assuming that the dialog is modal... Instead of doing this in FormClosing do it, after calling ShowDialog(). If not, try using the FormClosed event instead.
I think your code is not working because, while the Form is closing, it still has modal focus.
If frmDG_RecordSelection is also MDIChild, then Home.ActiveMDIChild is this form. That is being closed.
But if frmDG is just a Dialog, the problem is different.
This dialog is Closing. But it's still visible. You cannot set focus to control that is not visible.
So you will have to set focus after this frmDG is completely closed, and invisible... To be more specific, when your MDI form is visible.
It's far more easier to do this from your MDI Form. I don't know how you have programmmed it, but I suppose it's something like that:
//this is in your MDI form
void OnRecordSelected(...)
{
frmDG yourDialog = new frmDG();
frmDG.ShowModal();
frmDG.Dispose();
}
In this case, you will have to set focus after frmDG is disposed.

Process continue after closing application c#

I'm beginner in C# and I was trying to make WFA application. But there is one problem. When I run it, it shows first(login) form and if I close it without logging in, it will works fine, but when i login, first form hides, secound form shows up and if i close it by clicking X button, form disappeared but I can see process running in task manager. On secound form is also logout button, and if i click button it hides secound form, and shows first(login) form, and if i close it, still process running in background.
How can i stop process running in background when closing App by X button?
I was searching but i couldn't find solution for my problem. (i found something to attach to process and see threads but there is too many of them)
There is code where Forms are created hided etc:
If logged in frmLogin hides -> show frmMail
If logged out frmMail hides -> show frmLogin.
If user successfully logged in:
this.Hide();
frmMail f2 = new frmMail();
f2.Show();
If user click "Logout" button:
this.Hide();
frmLogin f1 = new frmLogin();
f1.Show();
I found solution, if everyone have same problem this may help.
How to switch between forms without creating new instance of forms?
How do I prevent the app from terminating when I close the startup form?
What I will do in your case is pretty simple.
Fist I will override the FormClose_Event, something like this
void Form_FormClosing(object sender, FormClosingEventArgs e)
{
//And here you can call
Application.Exit();
// which this will cause to close everything in your application
//Also if you want to be more aggressive, there is another option, you can
//use, Environment.Exit(1), which will basically kill you process.
}
I hope my answer helps you, have a nice day!

WinForms Form doesn't show after activation

I have a WinForms Form I call from two places, one from a tray-icon's context menu and from a context menu I get from right-clicking on my app's shortcut on the desktop. When I use the tray-icon's menu the form opens just fine, even if it was minimized - it displays again. The problem is when I use the link. If the form is minimized, sometimes it will restore it, but other times it won't. The application's icon on the task-bar will flash but the window itself won't show.
I checked through debugging and both calls to display the form are from the same thread. Are there other contexts I'm missing that might interfere?
Code I use:
public void Display()
{
this.WindowState = FormWindowState.Normal;
this.Show();
this.Activate();
this.Focus();
}

Application not visible in taskbar when using Application.Run()

I am creating a windows application using C#.Net. I am showing a form when starting an application using below code :
Form myForm = new MyForm();
Application.Run(myForm);
Application is not appearing in taskbar, but I know its running since I can navigate to the application window using Alt-TAB.
If I use myForm.ShowDialog(), application is visible in taskbar.
What am I missing here?
UPDATE: ShowInTaskbar property is set to true for the form.
UPDATE2: FormBorderStyle is set to None
Toggle ShowInTaskbar in your form load event with these two lines:
This.ShowInTaskbar = False
This.ShowInTaskbar = True
It worked for me.
Add Activate() in Form_Shown event.
With .NET Framework 4.6.1, toggling:
this.ShowInTaskBar = false;
this.ShowInTaskBar = true;
causes FormClosing event being fired.
Calling this.Activate() in Shown event works properly.
In my case it was nonmodal form, called to be shown with ShowDialog(), but the request to create and show the form was sent from pipe. When executed by user's click on main form, everything was ok, form was shown and appeared on the taskbar.
You may not set an empty string as windows title during Form load.
This is what happened to me today (my window title is retrieved from a file that was missing, and so the window disappeared from the taskbar).

WinForms programming - Modal and Non-Modal forms problem

I have a problem with modality of the forms under C#.NET. Let's say I have main form #0 (see the image below). This form represents main application form, where user can perform various operations. However, from time to time, there is a need to open additional non-modal form to perform additional main application functionality supporting tasks. Let's say this is form #1 in the image. On this #1 form there might be opened few additional modal forms on top of each other (#2 form in the image), and at the end, there is a progress dialog showing a long operation progress and status, which might take from few minutes up to few hours. The problem is that the main form #0 is not responsive until you close all modal forms (#2 in the image). I need that the main form #0 would be operational in this situation. However, if you open a non-modal form in form #2, you can operate with both modal #2 form and newly created non modal form. I need the same behavior between the main form #0 and form #1 with all its child forms. Is it possible? Or am I doing something wrong? Maybe there is some kind of workaround, I really would not like to change all ShowDialog calls to Show...
Image http://img225.imageshack.us/img225/1075/modalnonmodalproblem.png
Modal forms do exactly what "modal" means, they disable all other windows in the app. That's rather important, your program is in a somewhat perilous state. You've got a chunk of code that is waiting for the dialog to close. Really Bad Things could happen if those other windows were not disabled. Like the user could start the modal dialog again, now your code is nested twice. Or she could close the owner window of the dialog, now it suddenly disappears.
These are the exact kind of problems you'd run into if you call Application.DoEvents() inside a loop. Which is one way to get a form to behave modal without disabling other windows. For example:
Form2 mDialog;
private void button1_Click(object sender, EventArgs e) {
mDialog = new Form2();
mDialog.FormClosed += (o, ea) => mDialog = null;
mDialog.Show(this);
while (mDialog != null) Application.DoEvents();
}
This is dangerous.
It is certainly best to use modal forms the way they were designed to stay out of trouble. If you don't want a modal form then simply don't make it modal, use the Show() method. Subscribe to its FormClosing event to know that it is about to close:
private void button1_Click(object sender, EventArgs e) {
var frm = new Form2();
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.Show();
}
void frm_FormClosing(object sender, FormClosingEventArgs e) {
var frm = sender as Form2;
// Do something with <frm>
//...
}
The first thing that comes to mind would be something like this. You could disable form 1 when you launch form 2 and then have form 1 handle the closed event of the second form to re-enable itself. You would NOT open modal 2 using show dialog.
Now keep in mind, from a user perspective this is going to be quite cumbersome, you might look at doing a MDI application to get all windows inside of a single container.
Your main form will not be responsive until any modal dialogs that are in the same process space are closed. There is not work around for that.
It looks to me like you could use an MDI application setting the Form #0 IsMdiContainer property to true.
Then, you could do something alike:
public partial class Form0 {
public Form0 {
InitializeComponent();
this.IsMdiContainer = true; // This will allow the Form #0 to be responsive while other forms are opened.
}
private void button1_Click(object sender, EventArgs e) {
Form1 newForm1 = new Form1();
newForm1.Parent = this;
newForm1.Show();
}
}
Using the ShowDialog() as you stated in your question will make all of the forms Modal = true.
By definition, a modal form is:
When a form is displayed modally, no input (keyboard or mouse click) can occur except to objects on the modal form. The program must hide or close a modal form (usually in response to some user action) before input to another form can occur. Forms that are displayed modally are typically used as dialog boxes in an application.
You can use this property [(Modal)] to determine whether a form that you have obtained from a method or property has been displayed modally.
So, a modal form shall be used only when you require immediate assistance/interaction from the user. Using modal forms otherwise makes believe that you're perhaps running into a wrong direction.
If you do not want your main form to be an MDI container, then perhaps using multithreading is one solution through a simple BackgroundWorker class is the key to what you want to achieve. Thus, it looks to me like a design smell...
What is it you want to do, apart of making your main form responsive, etc.
What is it you have to do?
Explaining what you have to do, we might be able to guide you altogether into the right, or at least perhaps better, direction.
Actually the answer is very simple. Try
newForm.showDialog();
This will open a new form, while the parent one is inaccessible.

Categories

Resources