Closing A Windows Form - c#

I received help yesterday from a few wonderful users and thought I would reach out again with another issue I am experiencing. I have always used this.Close(); to close my winform but in this instance I am returning the value from a variable and utilizing this to do such so when my code hits thett this.Close(); the winform is not closed as the this refers to something else
(or at least I think that is what the issue is) - what I am after is a way to close my winform TestData when the code hits the this.Close();
What is currently happening is an endless loop of the code that starts with
if (spreadsheetimported == true)
{
--if I need to show additional code I can--
public partial class TestData : Form
{
public CancelFileImport p1;
public TestData(CancelFileImport _p1)
{
p1 = _p1;
InitializeComponent();
}
private void Testing_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
if (ssi == true)
{
CancelFileImport CFI = new CancelFileImport(this);
CFI.StartPosition = FormStartPosition.CenterParent;
CFI.ShowDialog();
if (cnclsve)
{
this.Close();
}
else
{
//user did not mean to cancel the save
//go back to save method
}
}
}
}
}
public partial class CancelFileImport : Form
{
public static TestData _p1;
public CancelFileImport(TestData p1)
{
_p1 = p1;
InitializeComponent();
}
public CancelFileImport()
{
InitializeComponent();
}
private void btnYes_Click(object sender, EventArgs e)
{
_p1.ssi = true;
this.Close();
}
private void btnNo_Click(object sender, EventArgs e)
{
_p1.ssi = false;
this.Close();
}
}

Try to do it like this
Replace this.Close(); with TestData.ActiveForm.Dispose();

Related

Manipulating similar objects in a form using another form

I am new to C# and I want to utilize the forms with one another.
I have 2 forms. (1)MMCMLibrary_home and (2)MMCMLibrary_reserve.
In this project, I'm in the stage of changing the label background colors in Form 1 but can't seem to utilize Form 2 to process it.
These are my necessary codes so far:
FORM 1
namespace MMCM_Library
{
public partial class MMCMLibrary_home : Form
{
public static MMCMLibrary_home instance;
//DCR1 Labels
public Label lbl1_1;
public Label lbl1_2;
public Label lbl1_3;
public Label lbl1_4;
public MMCMLibrary_home()
{
InitializeComponent();
instance = this;
lbl1_1 = lblDCR1_9;
lbl1_2 = lblDCR2_11;
lbl1_3 = lblDCR1_1;
lbl1_4 = lblDCR1_3;
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve();
reserveDCR1.Show();
}
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve();
reserveDCR2.Show();
}
public void btnDCR3_Click(object sender, EventArgs e)
{
var reserveDCR3 = new MMCMLibrary_reserve();
reserveDCR3.Show();
}
public void btnDCR4_Click(object sender, EventArgs e)
{
var reserveDCR4 = new MMCMLibrary_reserve();
reserveDCR4.Show();
}
}
}
FORM 2
when I click any reserve now button in form 1 it will open form 2. However, if I pick a radio button, the background change will always be applied to Discussion Room 1 even I reserved for discussion room 2
namespace MMCM_Library
{
public partial class MMCMLibrary_reserve : Form
{
public static MMCMLibrary_reserve instance;
public MMCMLibrary_reserve()
{
InitializeComponent();
instance = this;
}
private void MMCMLibrary_reserve_Load(object sender, EventArgs e)
{
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
{
}
private void btnDCR1_Click(object sender, EventArgs e)
{
}
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
}
}
}
Can you help me to device an efficient way to solve this. Can you also suggest a database method suitable for my beginner project.
You've said that:
the background change will always be applied to Discussion Room 1
Well, yes. It seems you don't pass specific object into Form2. There's strightforward:
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
So you'll be always changing backcolor of lbl1_1 of your Form1. What needs to be done is to indicate which room has been selected. You can assign room after you click the button by passing the int parameter:
public void btnDCR1_Click(object sender, EventArgs e)
{
var reserveDCR1 = new MMCMLibrary_reserve(1);
reserveDCR1.Show();
}
or:
public void btnDCR2_Click(object sender, EventArgs e)
{
var reserveDCR2 = new MMCMLibrary_reserve(2);
reserveDCR2.Show();
}
Then, in Form2, at the very top, add something like:
int room; to be able to assign the room number in Form2:
public MMCMLibrary_reserve(int roomNumber)
{
InitializeComponent();
room = roomNumber;
instance = this;
}
And then you could just select room you clicked, by:
public void btnDCRoomsReserve_Click(object sender, EventArgs e)
{
if (rbtn9.Checked)
{
if(room == 1)
{
MMCMLibrary_home.instance.lbl1_1.BackColor = System.Drawing.Color.Red;
}
else if(room == 2)
{
MMCMLibrary_home.instance.lbl1_2.BackColor = System.Drawing.Color.Red;
}
else if(room == 3)
{
MMCMLibrary_home.instance.lbl1_3.BackColor = System.Drawing.Color.Red;
}
else if(room == 4)
{
MMCMLibrary_home.instance.lbl1_4.BackColor = System.Drawing.Color.Red;
}
}
else if(radioButton2.Checked)
{
//etc.
}
else if(radioButton3.Checked)
{
//etc.
}
else if(radioButton4.Checked)
{
//etc.
}
}
I think that was the problem. Try it and let us know.

How in main form Cancel backgroundWorker other form

I need to cancel the BakgroundWorker from the main form that was launched in another form. I am trying to solve this problem with the help of delegates. However, I cannot undo the BakgroundWorker. Help solve this problem. If there are other solutions, please write.
I give for example the code of the main form
public partial class MainForm : Form
{
public MainForm(string FIO)
{
//some code
}
public event EventHandler<EventArgs> Canceled;
private void Button5_Click(object sender, EventArgs e)
{
if (Canceled != null)
Canceled(sender, e);
}
}
Code of the form where it was launched backgroundWorker
public partial class CarriageForm : Form
{
public CarriageForm(ToolStripProgressBar toolStripProgressBar1, ToolStripLabel toolStripLabel1)
{
//some code
}
private void CarriageForm_Load(object sender, EventArgs e)
{
progBar.Visible = false;
if (!backgroundWorker1.IsBusy)
{
progBar.Visible = true;
progBar.Maximum = GetTotalRecords();
string GetCarriage = "Select dc.ID, dc.CarNumber [Номер вагона],dc.AXIS [Осность],do.ID [OwnerID], do.Name [Собственник],do.FullName [Собственник полное наименование] From d__Carriage dc Left Join d__Owner do on do.ID = dc.Owner_ID";
MainForm mainForm = new MainForm(null);
mainForm.Canceled += new EventHandler<EventArgs>(Button2_Click);
backgroundWorker1.RunWorkerAsync(GetCarriage);
}
//BackgroundWorker1_DoWork...
//BackgroundWorker1_ProgressChanged...
//BackgroundWorker1_RunWorkerCompleted..
public void Button2_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Stop the Background Thread execution
Application.UseWaitCursor = false;
System.Windows.Forms.Cursor.Current = Cursors.Default;
backgroundWorker1.CancelAsync();
progBar.Value = 0;
progBar.Visible = false;
TlStpLabel.Text = "Пользователь умышленно отменил";
}
}
}
For clarity

How can I find what Form opened the form currently open? (C#, WinForms)

So I basically have a form that is opened and I need to check what form called that one to open
"Parent form"
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm();
nf.Show()
}
"Opened form"
private void newForm_Load(object sender, EventArgs e)
{
if parent is ("oldForm") // Need to know how to code for this line.
{
//do some stuff here
}
else
{
//Do something different
}
}
So for example if oldForm was the form that called this one, something specific would happen that wouldn't if "anotherForm" called it for example
You could just add a 'Parent' property to your forms, and set it before calling Show.
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm();
nf.Parent = this;
nf.Show()
}
Your form would look like this:
public class MyForm
{
public Form Parent {get;set;}
private void newForm_Load(object sender, EventArgs e)
{
if (this.Parent is oldForm)
{
//do some stuff here
}
else
{
//Do something different
}
}
}
Note that if (this.Parent is oldForm) is equivalent to if (this.Parent.GetType() == typeof(oldForm))
As one of the comments said, if you're only using the Parent property to make this one decision, you'd be better to define it as a Boolean property, called DoSomething that indicates what it does. Combining this with the other suggestion gives:
public class MyForm
{
private bool specialMode;
public MyForm(bool mode)
{
this.specialMode = mode;
}
private void newForm_Load(object sender, EventArgs e)
{
if (this.specialMode)
{
//do some stuff here
}
else
{
//Do something different
}
}
}
which you'd call like this:
private void btnEnter_Click(object sender, EventArgs e)
{
this.Close();
newForm nf = new newForm(true); // SpecialMode = ON
nf.Show()
}

Second form is not showing properly

My main form is a mdi container with a menu strip. When I choose Options-Maintenance I want another mdi to appear. This kind of works. Instead of another mdi container along with the design, a regular smaller form appears and not sure why.
public partial class mdiMain : Form
{
static string sTo = ConfigurationManager.ConnectionStrings["connectionTo"].ToString();
public myDataAccess3 data;
public mdiMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
data = new myDataAccess3(sTo);
frmLogOn frmLogOn = new frmLogOn(data);
if (frmLogOn.ShowDialog().Equals(DialogResult.Cancel))
{
frmLogOn.Close();
frmLogOn = null;
Application.Exit();
return;
}
frmLogOn.Close();
frmLogOn = null;
this.Focus();
}
catch (Exception e1)
{
MessageBox.Show("There was an error " + e1);
}
}
private void maintenanceToolStripMenuItem_Click(object sender, EventArgs e)
{
mdiMaintenance maintenance = new mdiMaintenance(this,data);
maintenance.Enabled = true;
maintenance.Show();
}
}
public partial class mdiMaintenance : Form
{
private myDataAccess3 data;
private mdiMain mdiMain;
public mdiMaintenance()
{
InitializeComponent();
}
public mdiMaintenance(mdiMain mdiMain, myDataAccess3 data)
{
// TODO: Complete member initialization
this.mdiMain = mdiMain;
this.data = data;
}
private void mdiMaintenance_Load(object sender, EventArgs e)
{
}
Thanks for the help
If the form is intended to be an MDI Child then you need to set the MdiParent property:
private void maintenanceToolStripMenuItem_Click(object sender, EventArgs e)
{
mdiMaintenance maintenance = new mdiMaintenance(this,data);
maintenance.Enabled = true;
maintenance.MdiParent = this;
maintenance.Show();
}

Closing event handlers C#

I have two forms, Form 2 is inheriting from Form 1.
What I need to do is that when I close both Form 1 and Form 2 another form which asks if user is sure to quit appears. Then if user clicks Yes, another form which asks if the user wants to save the game appears if and only if the form which the user closes is Form 2 and not Form 1 since for Form 1 there is no saving necessary.
This is what I managed to do:
// These are the Form 1 closing and closed event handlers:
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
SureClose sc = new SureClose();
sc.StartPosition = FormStartPosition.CenterScreen;
sc.Show();
}
private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
}
Then in Sure Close: // Please note that Tournament is Form 2 inheriting from GameForm (Form 1)
private void yesButton_Click(object sender, EventArgs e)
{
this.Hide();
if (GameForm.ActiveForm is Tournament)
{
SaveGame sg = new SaveGame();
sg.StartPosition = FormStartPosition.CenterScreen;
sg.Show();
}
else
{
GameForm.ActiveForm.Close();
}
}
private void noButton_Click(object sender, EventArgs e)
{
this.Hide();
}
// This is the SaveGame Form:
private void saveButton_Click(object sender, EventArgs e)
{
// Still to do saving!
}
private void dontSaveButton_Click(object sender, EventArgs e)
{
this.Hide();
GameForm.ActiveForm.Close();
}
The problem is that when in the yesButton event handler in SureClose Form I have GameForm.ActiveForm.Close(), this is going back to the GameForm Closing event handler therefore the SureClose dialog is appearing again.
I tried doing: if (e.CloseReason() == CloseReason.UserClosing)
but obviously it doesn't work either because the reason of closing will always be the user :/
How can I solve this?
Thanks a lot for any help !
Form1 :
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(SureClose())
{
SaveChanges();
}
else
{
e.Cancel = true;
}
}
private bool SureClose()
{
using(SureClose sc = new SureClose())
{
sc.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sc.ShowDialog();
if(result == DialogResult.OK)
{
return true;
}
else
{
return false;
}
}
}
protected virtual void SaveChanges()
{
}
Form2:
protected override void SaveChanges()
{
using(SaveGame sg = new SaveGame())
{
sg.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sg.ShowDialog();
if(result == DialogResult.OK)
{
//saving code here
}
}
}
SureClose form and SaveGame form:
private void yesButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void noButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}

Categories