Closing multiple forms C# - c#

I'm currently working with C# windows forms.
I got like 40 cs files, and when the applications works like :
Pressing a button -> Opens new form upon the first one and pressing another button opens another form upon the one before.
Now, whenever I click multiple forms none of the others are closing itself when I'm pressing different buttons, they all stay on the background.
Now, if I use this.Close(); its working with 1, but i got like 40 cs files and it's hard to compile them all..
looking for any suggestion ?
Thank you guys for any sort of help !
public partial class Costumers_Orders : Form
{
public Costumers_Orders()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
NewCostumer mm = new NewCostumer();
mm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Remove_Customer mm = new Remove_Customer();
mm.Show();
}
private void Costumers_Orders_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Show_Edit_Customer mm = new Show_Edit_Customer();
mm.Show();
}
private void button4_Click(object sender, EventArgs e)
{
Orders_report mm = new Orders_report();
mm.Show();
}
}

Assuming you only ever want one form open at a time, and that "Costumers" is the main form and can't be closed:
private void button1_Click(object sender, EventArgs e)
{
ShowForm(new NewCostumer());
}
private void ShowForm(Form newForm)
{
List<Form> forms = new List<Form>();
foreach (Form frm in Application.OpenForms)
{
if (!(frm is Costumers))
{
forms.Add(frm);
}
}
foreach (Form frm in forms)
{
frm.Close();
}
newForm.Show();
}
There are other ways you could do this as well.

Related

C# .xaml transfer an Object from one window another

I have a form "SprocketOrderForm.xaml" window. I push a button to open a new window, "SprocketForm.xaml". In that window when I click submit it will create a new "Aluminum" object populated by the textboxes in "SprocketForm". Now I need to get that new object back into a listbox in "SprocketOrderForm". I've been stuck on this for a while now.
"SprocketOrderForm.xaml"
private void btnAdd_Click(object sender, RoutedEventArgs e)
{
if (rbAluminum.IsChecked==true)
{
SprocketForm sf = new SprocketForm("Aluminum");
sf.Show();
lisbOrderList.Items.Add(sf.objAlum);
}
}
"SprocketForm.xaml"
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (sproc=="Aluminum")
{
AluminumSprocket Steel = new AluminumSprocket(int.Parse(txtbItemID.Text), int.Parse(txtbNumberOfItems.Text), int.Parse(txtbNumberOfTeeth.Text));
objAlum = Steel;
this.Close();
}
}

Wpf detect when window is closed from a different window

private void newmail_Click(object sender, RoutedEventArgs e)
{
Nieuweemail _nieuweEmail = new Nieuweemail(_username);
_nieuweEmail.Show();
}
When that window is closed, I want to call a function inside my main window that will execute.
_nieuweEmail.Closed += setContent();
I could do this if I could call it in the window that is going to be closed. But that's not the case. How can I detect this?
Assuming setContent and newmail_Click are both methods in your main window...
private void newmail_Click(object sender, RoutedEventArgs e)
{
Nieuweemail _nieuweEmail = new Nieuweemail(_username);
_nieuweEmail.Closed += SetContentHandler;
_nieuweEmail.Show();
}
private void SetContentHandler(object sender, EventArgs e)
{
setContent();
}

C# - Form2 value to Form1

I'm having trouble passing values ​​entered in form2(citacao) to form1(principal).
Principal.cs (form1)
richEditControl1.Document.AppendText(citacao.valor_edit[0]);
Citacao.cs (form2)
public string[] valor_edit = new string[3];
private void simpleButton2_Click(object sender, EventArgs e)
{
valor_edit[0] = memoEdit1.Text;
valor_edit[1] = comboBox1.SelectedItem.ToString();
valor_edit[2] = textEdit1.Text;
}
But when I click the button nothing happens , the values ​​are not inserted into the richedit I like it.
I already have this on form (Pass DataGrid to ComboBox)
Form1 (principal)
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
citacao cita = new citacao(this);
cita.Show();
}
form2(citação)
public citacao(principal gridForm)
{
InitializeComponent();
frm1 = gridForm;
}
// LOAD ALL FONTS (Referencias);
private void citacao_Load(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (DataGridViewRow row in frm1.DataGridView1.Rows)
{
comboBox1.Items.Add(row.Cells[0].Value.ToString());
}
comboBox1.SelectedIndex = 0;
}
let's see whether I understood your situation :)
declare your variable in Form 1 as a class variable
private citacao cita;
then initialize it in the button press event
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
cita = new citacao(this);
// subscribe to the closing event
cita.FormClosing += form_FormClosing;
cita.Show();
}
// when Form 2 will be closed you can execute your important line in the event
void form_FormClosing(object sender, FormClosingEventArgs e)
{
// BUT! you have to use the variable name!
richEditControl1.Document.AppendText(cita.valor_edit[0]);
}
EDIT:
Ok after looking at the entire code:
please remove the button3! and this entire code:
private void button3_Click(object sender, EventArgs e)
{
cita = new citacao(this);
richEditControl1.Document.AppendText(citacao.valor_edit); // this line is the problem!
}
The function AppendText probably needs a string as parameter and you give the entire array!
If you subscribe to the closing event in Form1 / principal and also implement
the event, your data will be transmitted automatically as soon as the Form 2 disappears from the screen :)

How can I refresh One form when the other form is closed?

I have been developing project in c#.
It has 2 form and these are connected with between each other.
I want to do that when second form is closed, first form refresh.
If I use Thread's Sleep program will be tired. I want to do this with closing events. How can I do ?(Like java's repaint)
Codes are below:
Form1
public static Form1 form;
public Form1()
{
InitializeComponent();
form = this;
}
private void button11_Click(object sender, EventArgs e)
{
Form2 yeniform = new Form2();
yeniform.Show();
}
Form2(Close Button)
private void button1_Click(object sender, EventArgs e)
{
Form1.form.Invalidate();
Form1.form.Refresh();
this.Close();
}
Bind Form_Closing event in your first form.
//Form1
private void button11_Click(object sender, EventArgs e)
{
Form2 yeniform = new Form2();
yeniform.FormClosing += new FormClosingEventHandler(this.Form2_FormClosing);
yeniform.Show();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
//Do your stuff here.
}
' button in the second form which is to be closed
' form1 is the form which is to be re loaded when form 2 is closed
private void btn_close_Click(object sender, EventArgs e)
form1.close() 'unload form 1 before closing form2
form1.show() ' form 1 reloading
unload(me) 'closing form2
end sub
This is a working sample. In parent form
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
VendorsAddForm f = new VendorsAddForm();
f.StartPosition = FormStartPosition.CenterScreen;
f.FormClosed += new FormClosedEventHandler(child_FormClosed);
f.Show();
}
void child_FormClosed(object sender, FormClosedEventArgs e)
{
this.Refresh();
var query = dbContext.AccountObjects.Where(p => p.IsVendor == true).ToList();
accountObjectsBindingSource.DataSource = new BindingList<AccountObject>(query);
}
Note: child form is VendorsAddForm
Thank to https://www.daniweb.com/posts/jump/1302760 , I learned from there.

Creating a new form, switching focus between the new and the old form with a button

I have a form and I want to get an instance of the same form as stated in the code below. And I have a button: every time I press this button, if a new form is created, I want it to focus to that window, if not, I want to create a new form.
I managed to create a new form but if I want to focus on it, the code did not work, any ideas?
private void btn_Click(object sender, EventArgs e)
{
if (opened == false)
{
Text = "form1";
var form = new myformapp();
form.Show();
opened = true;
form.Text = "form2";
}
else
{
if (Application.OpenForms[1].Focused)
{
Application.OpenForms[0].BringToFront();
Application.OpenForms[0].Focus();
}
if (Application.OpenForms[0].Focused)
{
Application.OpenForms[1].BringToFront();
Application.OpenForms[1].Focus();
}
}
}
You can try shortening your code without the need to introduce more variables with this example:
void button1_Click(object sender, EventArgs e) {
bool found = false;
for (int i = 0; i < Application.OpenForms.Count; ++i) {
if (Application.OpenForms[i].GetType() == typeof(myformapp) &&
Application.OpenForms[i] != this) {
Application.OpenForms[i].Select();
found = true;
}
}
if (!found) {
myformapp form = new myformapp();
form.Show();
}
}
Updated code from Francesco Baruchelli's comment.
If I understand correctly what you are trying to do, you can keep a static List with the opened forms. Everytime an instance of your Form is opened you add it to the List, and everytime it is closed you remove it. The when you press the button you can check the size of the List. If it is 1 you create a new Form, open it and set the focus on it. If the size is already 2, you look in the List for the instance which is different from the one executing the click event. The code could be something like this:
private static List<Form1> openForms = new List<Form1>();
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = null;
if (openForms.Count == 2)
{
foreach (Form1 aForm in openForms)
if (aForm != this)
{
frm = aForm;
break;
}
}
else
{
frm = new Form1();
frm.Show();
}
frm.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
openForms.Add(this);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
openForms.Remove(this);
}

Categories

Resources