I have 2 forms, one of them is being showed through clicking a button in the first one. I want it such that when the button for showing 2nd form is clicked, first form gets minimized, and the second form opens normally. And when the 2nd form is being closed, the first window should become un-minimized and be placed where it was before.
User can override this and just click on the first form in quick luanch area and bring up the first form if he/she wants, so I dont want the 2nd from to me of Dialog kind.
I am trying the code below, but when I click on the button, the 1st form gets minimized, and also the second form is being minimized too! so I have to manually un-minimize 2nd form!
private void button1_Click(object sender, EventArgs e)
{
if (DebugForm != null)
return;
DebugForm = new DebugForm();
DebugForm.Closed += delegate
{
WindowState = FormWindowState.Normal;
DebugForm = null;
};
WindowState = FormWindowState.Minimized;
DebugForm.Show();
DebugForm.WindowState = FormWindowState.Normal;
DebugForm.BringToFront();
}
It seems to minimize both windows when there is another window on your screen. Minimize all windows except your application. You will see that it works. Keep it working along with other windows like this:
Change this:
WindowState = FormWindowState.Minimized;
DebugForm.Show();
DebugForm.WindowState = FormWindowState.Normal;
DebugForm.BringToFront();
to this:
DebugForm.Show();
WindowState = FormWindowState.Minimized;
The lines I removed weren't really necessary. The important thing is the order of statements to coordinate with the Z order of the parent form before minimizing it.
Ran into similar issue. The problem was that the form I was trying to call back was not an active form. Here's how I solved the issue. On the button click of the current from2 I added:
if (form1.ActiveForm == null)// Check to see if form is active
{
form1 frm = (form1)Application.OpenForms["form1"];// find open form
frm.WindowState = FormWindowState.Maximized;// Set state
frm.ShowInTaskbar = true; //set to show in taskbar
}
Related
I have developed a C# Windows Form.
At first, the Windows Form works fine.
However, one day the Windows Form starts up always minimized and I have no idea.
I checked the WindowState is Normal not Minimized.
How Can I fix it, Thanks!
Edit:
I comment each code block to narrow the scope to locate the problem point.
And I found that I used a Drive Detector in my MainForm.
When that instance was created, the call Window form must be passed as a parameter to the constructor.
Otherwise, the Drive Detector will create a hidden form. However, the MainForm will be minimized.
The below code will NOT create a hidden form.
driveDetector = new DriveDetector(this);
The below code will create a hidden form, it will interfere the call Windows Form.
driveDetector = new DriveDetector();
try to add this code in form load event and test
this.WindowState = FormWindowState.Normal;
you should use WindowState = FormWindowState.Maximized if you wish to open your windows in full screen by default. You can do this programmatically in Form load event.
There are other various options available too from which you can control on How to open your windows form.
1.Check whether you have set the size of form to smaller one.
2.Try re-build your solution.
3.Add Form Load Event from your Events Properties of Form and add following code to it
this.WindowState = FormWindowState.Normal;
Try to do it in form activated event
bool bIsLoaded = false;
private void Form1_Activated(object sender, EventArgs e)
{
if (!bIsLoaded)
{
this.WindowState = FormWindowState.Maximized;
bIsLoaded = true;
}
}
Just try to add it from the code level to say the windows state as follows.
private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
I comment each code block to narrow the scope to locate the problem point.
And I found that I used a Drive Detector in my MainForm.
When that instance was created, the call Window form must be passed as a parameter to the constructor.
Otherwise, the Drive Detector will create a hidden form. However, the MainForm will be minimized.
The below code will NOT create a hidden form.
driveDetector = new DriveDetector(this);
The below code will create a hidden form, it will interfere the call Windows Form.
driveDetector = new DriveDetector();
Try this:
Topmost = true;
In your Form_Load event
I'm currently trying to create a Properties Window which is opened after a Button on the Outlook Toolbar is pressed, i now have:
1) the Button on the Toolbar (currently if pressed nothing occurs)
2) i know how to create the method which would hold the action after the Button is Pressed
-but, I am a beginner and i don't know how to create a window which would open after the button is pressed, the Window should be fairly big, and for now have nothing but a checkbox(which i later would like to apply some method to.
if you ever created a window which opens after a button is pressed, i would be really pleased to get your help.
All help is appreciated, thank you
Here's the recommended way of opening a dialog window when the user clicks a button:
Add a new form to your project (e.g. MyForm) and then you can use the following code in your button's click event handler:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
if (myForm.ShowDialog() == DialogResult.OK)
{
// The code that should be executed when the dialog was closed
// with an OK dialog result
}
}
In case you do not want the new window to be modal (i.e. you want to allow the user use other parts of the application while the window is opened), the code gets even more simple:
private void OnMyButtonClicked(object sender, EventArgs e)
{
MyForm myForm = new MyForm();
myForm.Show();
}
You can also create your form on the fly without adding one to your project, which is a bit more complicated, but advanced developers prefer this approach instead of messing with the designer ;)
private void OnMyButtonClicked(object sender, EventArgs e)
{
Form myForm = new Form();
myForm.Text = "My Form Title";
// Add a checkbox
CheckBox checkBox = new CheckBox();
checkBox.Text = "Check me";
checkBox.Location = new Point(10, 10);
myForm.Controls.Add(checkBox);
// Show the form
myForm.Show();
}
Here is a small tutorial for you to follow..
http://msdn.microsoft.com/en-us/library/ws1btzy8%28v=vs.90%29.aspx
EDIT: I would also recommend you remember the msdn website because it will prove invaluable for other programming issues you come across..
you have to add a new form to your project. Then you call the constructor where you want to pop up the window.
like this
Form2 form2 = new Form2();
form2.showDialog();
Edit:
where form2 is not the "main" Form of you program.
This'll set your main window to the background as long as the newly popped up window is closed.
I have a system in which the main form is the menu, and it pops up a login form on loading.
Originally it loaded the login form below the menu, so I used this.topmost = true to make it come to the front. (as bring to front and send to back did not work)
However, if the user then clicks on something else, say chrome, then it still stays at the top of the z-order, by definition of topmost.
I tried to use the deactivate event, but this meant that on load it once again comes up behind the menu form.
How could I stop it from loading behind my menu form, and yet when it loses focus stop it from being topmost?
private void login_Deactivate(object sender, EventArgs e)
{
// do not want it to remain top most when the application is not in focus.
this.TopMost = false;
}
In the Menu form:
private void Menu_Load(object sender, EventArgs e)
{
openLogin()
}
private void openLogin()
{
Cursor.Current = Cursors.WaitCursor;
login theForm = new login(this);
this.Enabled = false;
theForm.Show();
Cursor.Current = Cursors.Default;
theForm.Activate();
theForm.TopMost = true; // Make the login form display over the Menu
}
Try setting Login's Form Owner property to the menu form.
From above MSDN link:
When a form is owned by another form, it is closed or hidden with the
owner form. ... Owned forms are also never displayed behind their
owner form. You can use owned forms for windows such as find and
replace windows, which should not disappear when the owner form is
selected. To determine the forms that are owned by a parent form, use
the OwnedForms property
.
Assuming this is a Win Forms application, then try changing theForm.Show() to theForm.ShowModal()
I have C# windows form application, with all default settings. I am using VS 2008, OS - Windows VIsta.
When my form loses focus, (as in when a user clicks on something behind the form), i need the same form that lost focus to regain it.
I made use of this Event to handle this;
private void Form1_Deactivate_1(object sender, EventArgs e)
{
Console.WriteLine("DEACTIVATE EVENT _______+++++++++_________");
Form1 f = new Form1();
f.show();
}
Here, what you will see is that is when the form loses its focus, the Console.writeline command will execute and a new form will appear on the screen. I do not want this. i want the exact form that lost focus to regain focus and appear back on the screen. How do i do this.
Form.Activate methods activates the form and gives it focus:
form.Activate();
Form.TopMost Property indicates whether the form should be displayed as a topmost form.
A topmost form is a form that overlaps all the other (non-topmost) forms even if it is not the active or foreground form. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.
form.TopMost = true;
I have been dealing with strange problem. I am using KryptonForm in a project. I have a form (say form1) and I need to open another form on a button click from this form. Here is the code:
void btn_click(object sender, EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
var f = new Form2();
f.ShowDialog();
Visible = true;
ShowInTaskbar = true;
}
The problem is that when the Form2 closes it closes the Form1 also. I have tried setting DialogResult = DialogResult.None from Form2 but of no avail. Please help me.
I am always using this technique and this thing has never happened.
Yes, this code is troublesome. It goes wrong when the user closes the dialog. Windows must then find another window to give the focus to. There isn't any left in your app, your main window is invisible. It then picks a window of another app. Odds are good, for example, that this will be a window inside Visual Studio. A big one. Your main form now disappears behind it.
You need to make sure that your main window is visible again before the dialog closes. You can do so by subscribing to the dialog's FormClosing event handler. For example:
private void button1_Click(object sender, EventArgs e) {
using (var dlg = new Form2()) {
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.Location;
dlg.FormClosing += (s, ea) => this.Show(); // <=== Here
this.Hide();
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
Bugged me for days!! Found this: https://bytes.com/topic/net/answers/769433-c-showdialog-inside-showdialog-closing-both-return
The result was being passed down and I dont know why. But if after the .ShowDialog() I just put this.DialogResult = DialogResult.None, it will fix it. This shouldnt happen in the first place, but this fixes it, so I am not too bothered.
You can also try changing the dialogResult on the button itself to "None" or deleting the this.Btn1.DialogResult... from the designer which worked for some people.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/543093ad-1860-4428-bae1-b0d4f112e04b/showdialog-closes-parent?forum=csharpgeneral
I know this is an old post, but I ran into this, and in my case the accepted answer (at the time I am writing this) is not helpful at all. The answer by #blind Skwirl led me to the culprit.
After 20 years of .Net programming (since it was introduced), I never noticed that BUTTONS have a "dialogresult" property. I always just set the forms "cancelbutton" and "acceptbutton" properties. What I found in my case was that (because I was doing a lot of copy-pasting of buttons), I had a bunch of buttons (not forms) that themselves had their "dialogresult" property set to "cancel", which meant that I would click a button on a dialog that would open another dialog, the "ok" button on the dialog had its result set to "cancel", and the button on the parent form ALSO had its result set to "cancel", so the dialog would close (with a result of cancel) and then the PARENT form would close with a result of cancel, confusing the heck out of me... so...
Just make sure all of your buttons have their dialogresult property set to NONE (or whatever the actual proper setting is that you want).
Bottom line, if a BUTTON (not the form) has its dialogresult property set to anything other than NONE, the form will close with that result when it is clicked (after any click event code has completed).
I hope that helps someone out there.