How to hold previous opened form active in C#? - c#

I have two forms ,for example form1 and form2
the form1 is parent form ,from form1 im calling form2 and show it ,but the problem is when second form "form2" has been opened the previous form will be inactive ?
Please tell me how to have multiple form active at same time.

You probably want to use Form.Show() instead of Form.ShowDialog(). The first one will show a form along side anther one while the other will "pause" the first form until you close the 2nd one.

Use
Form.Show()
Instead of
Form.ShowDialog()

set TopMost property of form2 to true
and then use form2.Show() instead of ShowDialog()
OR
you can open form 2 in another thread like what i done
private void ShowForm2()
{
new Form2().ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(ShowForm2));
th.Start();
}

How are you showing your 2nd form? Sounds like you're showing it in a modal way - you don't want to!

Copy and paste this code into your second form class:
protected override bool ShowWithoutActivation {
get {
return true;
}
}

Related

How to disable a form when a second form is active

I have two Windows Forms: FormMain and FormRecordTranslation. I have a menu item on FormMain. When I click the item, I should see FormRecordTranslation form and FormMain must be deactivated i.e. unclickable as long as I am working on second form. when I close the second form, the first form must be available. To this purpose I used the following code inside FormMain class.
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void recordTranslationToolStripMenuItem_Click(object sender, EventArgs e)
{
FormRecordTranslation frmRecordTranslation = new FormRecordTranslation();
frmRecordTranslation.Activate();
frmRecordTranslation.Show();
frmRecordTranslation.TopMost = true;
}
}
Although this holds the second form on top. it still allows the first form to be clicked and worked on.
Use frmRecordTranslation.ShowDialog() instead of frmRecordTranslation.Show(). This will show your second forma as a modal dialog.
Instead of calling Form.Show(), like so:
frmRecordTranslation.Show();
Call the ShowDialog() method. ShowDialog specifies that the form to be shown is a modal dialog - ie. the form is not able to be interacted with while the dialog is shown, much like message boxes (when displayed using MessageBox.Show).
There is also ShowDialog(Form) (MSDN) for this purpose.
Also, the Show() method has an overload (MSDN) which takes another form as the parent.
Then, there should be no need for
frmRecordTranslation.Activate();
frmRecordTranslation.TopMost = true;
any more. But the parent form will still be enabled in this case (just eliminates the need of TopMost).

Order Break Between Model And Main Forms c#

I have two forms form1 is main form and form two is model form I want to set the forms as below:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show(this);
}
The above would set the form1 owner of form2 and form2 would be shown but the problem is that this will break the order of forms on press of Alt+Tab keys hence I have tried it with another way as below.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
This would be works but the problem is that the dialogue forms will not allow me to maximise/minimise and close
My form2 is borderless form and it is set to show on specific location as to fit with main form1. My aim to do not shows the form2 in Alt+Tab list and as I close the form2 then form1 will show immediately without break order of form.
When I press Alt+Tab keys on first condition and try to close form2 then the other application shown instead of form1 which is I do not want.
Is there any solution of this problem?.
It really sounds like you could do the second form as a custom control.
See Microsoft's documentation and this set of examples.
Think of it as a standard control, like a Button, DataGridView, TextBox, or the like, except that you have total control over it. You can show or hide it, you don't have to worry about where it is positioned, it won't take focus away from the parent form, and so on. And you can put whatever other controls you want in it, encapsulate all their logic, etc.
A possible hack is to keep your parent form active after opening child form as a modal form, so that you could do maximize/minimize your parent as well. An extension method:
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool enable);
public static DialogResult ShowDialogSpecial(this Form formToBeShown, Form parent)
{
parent.BeginInvoke(new Action(() => EnableWindow(parent.Handle, true)));
formToBeShown.ShowDialog(parent);
return formToBeShown.DialogResult;
}
You can call:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
//additionally do f2.ShowInTaskbar = false to make sense.
f2.ShowDialogSpecial(this);
}
This wont let child form truly act as non-modal form, since child form can cover over parent form.

How to hide a stack of modal forms in .NET Windows Forms

I have the following problem:
I open multiple modal forms in a stack (for example, form1 opens modal form form2 which in turn opens modal form form3, etc.). I would like to hide the entire stack.
I tried calling the Hide method or setting the Visible property on the parent, but this only hides the parent. I also tried hiding every form individually, but then I have to call ShowDialog on each of the forms which locks the thread in which I call the aforementioned method.
Is there be a way to set the modal dialogs so that they inherit the status of the parent and get hidden in a cascade just by setting the property on the first form?
I'm also open to other suggestions.
To re-show a form you hid by setting obj.Visible = false just set obj.Visible = true, not ShowDialog.
ShowDialog initiates a message loop, which will cause confusion since the dialog is already running a message loop.
Since you're talking about modal dialogs, it would be the last one opened that would commence this action. Open every form as in the following example, and then Hide() that last one.
public partial class Form1 : Form
{
Form2 frm2 = new Form2();
public Form1()
{
InitializeComponent();
frm2.VisibleChanged += frm2_VisibleChanged;
Shown += Form1_Shown;
}
void Form1_Shown(object sender, EventArgs e)
{
frm2.ShowDialog();
}
void frm2_VisibleChanged(object sender, EventArgs e)
{
if (frm2.Visible == false) Hide();
}
}

f2.show() method does not show the progress bar in form2

I have an application that has 2 forms. First one is where I do all the job and second one is just for displaying a progressbar.
I want to open the second one from the main form. if I use
Form2 newForm = new Form2();
newForm.Show();
Form2 opens and closes when it needs to open and close, but I cannot see the progress bar. I just can see a blank instead of it.
When I use
Form2 newForm = new Form2();
newForm.ShowDialog();
I can see the progressbar but Form2 doesn't close when it needs. It runs forever, what should I do?
I use a static public variable closeForm to close the second form. When I need to close the form I set
closeForm = true;
and in the second form, I have a timer
private void timer1_Tick(object sender, EventArgs e)
{
if (Form1.closeForm)
{
this.Dispose();
this.Close();
return;
}
else
{
progVal++;
progressBar1.Value = (progVal % 100);
}
}
this is where I put the ProgressBar value and close the form.
When I use show method, I only see blanks instead of the controls in form2. not just the progressbar, and I want form1 to close form2
first of all you need to report progress to progressbar
int iProgressPercentage = (int)(dProgressPercentage * 100);
// update the progress bar
progressBar1.ReportProgress(iProgressPercentage);
try doing that first then call this.close();
As I said above in the comment, you need to check Modal dialog from here Form.ShowDialog Method, and I just quote the following form there:
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
As why you can't see your ProgressBar on Form2 with Show(); you need to provide more information of how you handles it, as if I separate your program into two parts and use two button click to run them (Click button1 to show Form2; and click button2 to close it) I can see your expected result: the progressbar.
Without your further information, my best guess is something running prevents the Form2 to update its GUI.

opening forms in c#

I had a button in first form .if i click the button the second form is opening ,if i click again same button in first form another second form is opening.i need to open only one second form only in c# .
Well you could do a :
Form.ShowDialog()
This will prevent the user clicking the button on the first form, as the second form will keep focus until it is closed.
Or you could do
Form2 form2 = null;
void button_click(object sender, EventArgs e)
{
if(form2 == null)
{
form2 = new Form2();
form2.Disposed += new EventHandler(f_Disposed);
form2.Show();
}
}
void f_Disposed(object sender, EventArgs e)
{
form2 = null;
}
Check to see if the form is already shown by storing a reference to it and using something like:
if(form2Instance.Visible==true)
....
If you provide some sample code, we'll have a more specific answer, but it sounds like you are instantiating a new form in your button click event. The first time you click the button, your second form (will/may) not exist, so create it and keep a reference to it local to your first form. Then the next time the button is clicked, show the form instead of recreating it.
I'd declare a private variable in the class with the button that contains a reference to the opened form.
If the button is clicked:
Check whether it is null,
If yes, create and show a new form, if no, don't do anything.
If it's about having exactly one form open, you might also check out form.ShowDialog(), which blocks the caller form until the new form is closed.

Categories