Enable/Disable another form WITHOUT INSTANTIATING NEW ONE - c#

Here's my question.. From form 1, i want when the user clicked on update items, Form 2 will open but form 1 will still remain open. At this point, I want the Form 1 to be enabled false and I've done it. But I want when the form 2 closes, the form 1 enable should be true again. I cannot do this... Here's my code:
In Form 1:
private void btnEditItem_Click(object sender, EventArgs e){
Form3 form3 = new Form3();
form3.Show();
this.Enabled = false;
}
In Form 2(which is for update items, after updating):
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 form1 = new Form1();
form1Here.Enabled = true;
}
In this situation, it will open another Form1. The question is, how am i suppose to "ENABLE BACK AGAIN THE FORM 1 WITHOUT OPENING IT?" HELP ME PLEASE PROVIDE SOME SAMPLE IF POSSIBLE. THANKS

Attach a handler to your form.
Form3 form3 = new Form3();
form3.FormClosed += new FormClosedEventHandler(frm3_FormClosed);
form3.Show();
private void frm3_FormClosed(object sender, FormClosedEventArgs e)
{
this.Enabled = true;
}
Or you can use ShowDialog
Opens a window and returns only when the newly opened window is closed.

From your description it seems you want a modal Dialog Box.
From MSDN Modal and Modeless Dialog Boxes:
A modal dialog box must be closed (hidden or unloaded) before you can
continue working with the rest of the application
This means that if Form 1 opens Form 2 as a modal dialog box, form 1 will remain visible, but it is disabled, you can't do anything with it until you've closed form 2. In your words: "form 1 is disabled and when form 2 closes form 1 is enabled again".
The code:
private void btnEditItem_Click(object sender, EventArgs e)
{
using (Form3 form3 = new Form3()}
{
var dlgResult = form3.ShowDialog(); // show form 3 as a modal dialog box
// halt this procedure until form3 is closed
// handle the result of form3:
if (dlgResult == DialogResult.OK)
{
var x = form3.SomePropery;
ProcessDialogOutput(x);
}
}
}
The using statement is not necessary, but it is neat programming, because Form3 implements IDisposable

Related

How to let the program detect the previous form where a new form was accessed so that the input from the new form is transferred to that previous form

I have multiple forms (e.g. Form1, Form2) that both contains a button that opens another form (Form3). In Form3 (pop-up form), the user is prompted to pick among the options, and once these were submitted through a button in Form3, the selected options will be transferred to the previous form where it was opened (either form1 or form2). Both forms1 and 2 are linked to one input form3, so im thinking of using a conditional statement upon clicking the "Submit" button in Form 3 that will determine whether the active form/currently maximized form is Form1 or Form2, and will let the program redirect and transfer the data accordingly to the specific form.
In maximized Form1 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form1
In maximized Form2 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form2
private void button1_Click(object sender, EventArgs e)
{
if (Form1.ActiveForm != null)
{
Form1.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form1.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
else if (Form2.ActiveForm != null)
{
Form2.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form2.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
}
It shows the output for Form1, but for Form2 there's no output. I tried placing Form2 in the first condition (if) and that works but not for Form1 this time. Apparently, what comes first is the only condition performed by the program, and the else if is not executed.
I tested if (Form1.Visible = true) works, but I've already tried and there was an error in the program. Should there be additional declarations or such or perhaps a new public class?
You're thinking about it backwards. Instead of "pushing" from Form3 back to Form1 or Form2, you should "pull" from Form3 directly from within the code in either Form1 or Form2. You can do this by showing Form3 with ShowDialog(), which will cause execution in the current form (Form1 or Form2j) to STOP until Form3 is dismissed. In Form3, you can make public properties that can be accessed to retrieve the values from it.
For example, here's a boiled down Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public String LabQuantity
{
get
{
return label8.Text;
}
}
public double AmountwFee
{
get
{
return Convert.ToDouble(label12.Text) + 100;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Then in either Form1 or Form2, you'd do something like:
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
if (f3.ShowDialog() == DialogResult.OK) // code STOPS here until "f3" is dismissed!
{
// ... do something with the data from "f3" ...
Console.WriteLine(f3.LabQuantity);
Console.WriteLine(f3.AmountwFee);
}
}
Thank you for all the suggestions! I'm learning a lot from the feedback because I'm only new to this as a student. Since coding is still very complicated for me, I decided to learn how to duplicate forms and proceeded to make a separate form3 for both form1 and form2. They were the common "copy, paste" techniques plus the renaming of forms, but it worked well in the end.
I will try to implement the suggestions the next time we have a coding assignment. Thank you!

Controlling an objects property from a form to another using a button C# [duplicate]

This question already has an answer here:
Interaction between forms — How to change a control of a form from another form?
(1 answer)
Closed 4 years ago.
Sorry, I'm a C# beginner
I am trying to make a button on Form 4 that will make change a property of an object in Form 3.
This case, every time I press button 1 on Form 4, the label on Form 3 will say that "You pressed button 1", Same thing on the button 2.
I added this on Form 4.
public partial class Form4 : Form
{
public bool buttonchecked;
private void button1_Click_1(object sender, EventArgs e)
{
buttonchecked = true;
}
private void button2_Click_1(object sender, EventArgs e)
{
buttonchecked = false;
}
And this is what i put on Form 3:
public void label2_Click(object sender, EventArgs e)
{
Form4 form4 = new Form4(); //add
if (form4.buttonchecked == true)
{
label2.Text = "You pressed button 1";
}
else
{
label2.Text = "You pressed button2";
My label2 text is always set to "You pressed button2" but I didn't
I added a code that closes the current form and Opens the other form, maybe this is causing the problem?
this is from the Form 3
this.Hide();
Form4 f4 = new Form4();
f4.ShowDialog();
and this is from the Form 4
this.Hide();
Form3 frm3 = new Form3();
frm3.ShowDialog();
Is there anything something I'm doing wrong?
There are a few issues with your code:
On Form3, why are you handling the label2_click button? This event is fired when you click on a label. If the Text property of your label is an empty string, you won't even see the label in order to be able to click it.
This code:
Form4 form4 = new Form4();
if (form4.buttonchecked == true)
// etc
is not logically correct, because you are creating a Form4 instance and then you're checking the value of it's public field (buttonchecked) without displaying the form. The default value of a boolean variable is false, so the control will always hit the else branch. That's the reason you're always getting the "You pressed button2" message.
One correct way to do this using your code is the following:
On Form3:
var form4 = new Form4();
var result = form4.ShowDialog();
if (result == DialogResult.OK)
{
label2.Text = "You pressed button 1";
}
else
{
label2.Text = "You pressed button 2";
}
On Form4:
public partial class Form4 : Form
{
public bool buttonchecked;
private void button1_Click_1(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
}
private void button2_Click_1(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
}
}
The ShowDialog() method will display the Form4 and will block the Form3 execution. On Form4 you set a DialogResult based on the button you pressed and you return that result to the calling form (Form3). Based on that result, you can take a decision.
That solution will do the job, but it has one issue: you can't play with both forms in parallel because of the Dialog constraint (when you open the Form4 from Form3, you have to close it in order to reach Form3 again, you can't play with both of them in the same time).
So here's a new (clean) solution that solves this problem:
On Form3 in Designer Mode, click on the label2 -> Properties -> Modifiers -> Public. In that way you can access the label2 from other forms.
On Form4, place the follwing code:
public partial class Form4 : Form
{
private void button1_Click_1(object sender, EventArgs e)
{
var form3 = Application.OpenForms["Form3"];
form3.label2.Text = "You pressed button 1";
}
private void button2_Click_1(object sender, EventArgs e)
{
var form3 = Application.OpenForms["Form3"];
form3.label2.Text = "You pressed button 2";
}
}
Note: on that solution, Form3 needs to be open before Form4, otherwise Application.OpenForms["Form3"] will return null or it will throw an exception.
If you have any further issues, don't hesitate to leave a comment.

Form1 closes after ShowDialog for Form2

I have 2 forms. I'm opening form2 with ShowDialog(), but then when I close it (by hiding it) form 1 disappears for few seconds, but if I use show to open form 2 then this does not happen.
I need to use ShowDialog(), how could I fix disappearance of form 1 after form 2 closes ?
I tried to use Form1.Show() right after I close form 2 with Hide() but does not work.
Form1
private void p0_igra2_Click(object sender, EventArgs e)
{
this.CenterToScreen();
imevislice.ShowDialog();
}
Form2
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
errorProvider1.SetError(textBox1, "Polje more biti izpolnjeno");
else
{
errorProvider1.Clear();
ime = textBox1.Text;
if (radioButton1.Checked)
izbrane_besede = "SLO";
else
izbrane_besede = "ENG";
this.Hide();
form1.Show();
form1.namehangman();
}
}
If you open a form with ShowDialog you need to close (Form.Close) it when you are done. ShowDialog starts a modal dialog; which means the calling function will not continue execution until the form is closed.
Using Show can fix this, but you didn't say what wasn't working with that approach.
In general, ShowDialog is pretty cheap; you should be able to just close the form correctly and you won't run into any issues.

How to prevent a Windows Form from being sent to the back or how to keep it on a specific z-order

I'm encountering a strange problem with Form layout ordering (z-order). Lets say I've three Windows Forms; Form1, Form2 and Form3.
Form1 is launched as application's main form, and it has a button that opens Form2 as a Dialog (using ShowDialog()). Form2 also has button that opens Form3 (but not as a Dialog, using Show()).
Once Form3 is opened, it jumps to the background (behind Form1) as soon as Form2 is clicked. I want to keep Form3 at-least above Form1 when Form2 is clicked, since it contains some options that assist working on Form2.
I Don't want to use Form3.TopMost = True; since it then keeps the Form3 on top of nearly everything even if you open or switch to any other application (e.g. Windows Explorer, Internet Browsers etc).
.BringToFront() also doesn't help. I've tried several combinations of some relevant Form events with Form.BringToFront(), Form.Activate() etc but all in vain. Any help would be appreciated.
Here is the code for reference:
Form1:
Form2 obj2 = new Form2();
private void button1_Click(object sender, EventArgs e)
{
if (obj2 != null && obj2.Visible) { obj2.Focus(); return; }
obj2 = new Form2();
obj2.ShowDialog();
}
Form2:
Form3 obj3 = new Form3();
private void button1_Click(object sender, EventArgs e)
{
if (obj3 != null && obj3.Visible) { obj3.Focus(); return; }
obj3 = new Form3();
obj3.Show();
}
Try something like this, could help setting the parent.
Form3 obj3 = new Form3();
private void button1_Click(object sender, EventArgs e)
{
if (obj3 != null && obj3.Visible) { obj3.Focus(); return; }
obj3 = new Form3();
obj3.Show(this);
}

Keep form on top and open and close other forms

I'm new to C# and could use some help.
What I have so far is a set of 8 windows forms I have created in C#, these forms have basic things like text boxes, labels, radio buttons, etc. Now that I have completed making all of these forms I want to have one additional form (called the Selector form) I can use to select one of the other 8 forms. At any given time, I want the Selector form to be on top of other windows and it will have 8 radio buttons (or regular buttons, doesn't matter). When one of the buttons is clicked, the current form (not the Selector form) should disappear and a new form should appear. The name of the button will be the name of the new form that appears.
I have seen a few examples and here is the code I have so far:
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
form1.Closed += (sender1, args) => this.Close();
form1.Show();
}
void Button2Click(object sender, EventArgs e)
{
// this.Hide();
var form2 = new CCARAdmin();
form2.Closed += (sender1, args) => this.Close();
form2.Show();
//Application.Run(new CCARAdmin());
}
Problem I am having is I don't want to hide the Selector form, which this does, and I don't know how to identify the other form that is open to close it and then open a different form.
From starting the program the logic would be like this:
Show Selector form
When a button is clicked on the Selector form, keep the Selector form on top and show the other form with the name of the button.
When a different button is clicked on the Selector form, close the previous form that was open (not the Selector form) and open the new form corresponding to the name of the button. Keep the Selector form on top.
When the Selector form is close, application stops.
Problem I am having is I don't want to hide the Selector form, which
this does, and I don't know how to identify the other form that is
open to close it and then open a different form.
Set Selector form TopMost to True to make it always on top. Or
you can use BringToFront after opening a new form
to know other forms that are open check this answer. Or you can define each From as a field in the Selector form, and check that.
selectorForm.TopMost = true ( this will help to keep the selector form always on top).
Create a form variable in your selector form to keep the reference of your currently opened form.
Sample code for 1 button click :
Form frm = null;
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
if (frm == null)
{
frm = form1;
}
else
{
frm.Close();
}
form1.Show();
this.TopMost = true;
frm = form1;
}
I resolved this by setting TopMost to true and then using the following code under each one of the buttons:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "FormSelector")
Application.OpenForms[i].Close();
}
var form = new TRAC();
if (radioButton9.Checked == true)
{
form.Show();
}

Categories

Resources