Make a check before closing form? - c#

The code below is in MainFrame.cs and it opens and checks the MovieForm.cs. I want to check if a entered movie title already exist, before a new movie is added to a list. But the problem is, if the title already exist and the messagebox appears, then the MovieForm.cs is already closed and all other data gone and there is no possibilities for the user to change the title to another one! Could this be done in some other way that isn't to complicated? Is there a way to stop the closing of the form? Thanks!
private void btnNewMovie_Click(object sender, EventArgs e)
{
movieForm = new MovieForm();
if (movieForm.ShowDialog() == DialogResult.OK)
{
if (!movieManager.GetMovieFromList(index).Split(',') [0].Equals(movieForm.GetTitle))
{
movieManager.AddNewMovieToMediaLibrary(movieForm.GetNewMovie); // Anropar properties i objektet movieManager
UppdateListboxOfMovies();
}
else
{
MessageBox.Show("Det finns redan en film med titeln " + movieManager.GetMovieFromList(index).Split(',')[0], "Ooops!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}

You have an opportunity to cancel the form closing:
private void btnNewMovie_Click(object sender, EventArgs e)
{
using (var movieForm = new MovieForm())
{
movieForm.Closing += (s, a) =>
{
if (movieForm.DialogResult == DialogResult.OK)
{
if (!movieManager.GetMovieFromList(index).Split(',') [0].Equals(movieForm.GetTitle))
{
movieManager.AddNewMovieToMediaLibrary(movieForm.GetNewMovie); // Anropar properties i objektet movieManager
UppdateListboxOfMovies();
}
else
{
MessageBox.Show("Det finns redan en film med titeln " + movieManager.GetMovieFromList(index).Split(',')[0], "Ooops!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// Prevent the form from closing and let the user try again
a.Cancel = true;
}
}
};
movieForm.ShowDialog();
}
}

The movieForm object is still in scope, so you can still access any public data from it. I assume that movieForm.GetTitle is returning correctly. All you need to do now is apply the following correction because at the moment you are just comparing your title with the first title in the list:
if (!movieManager.GetMovieFromList(index).Split(',').Contains(movieForm.GetTitle))
...
That should solve your problem.
Edit: Ok, I misunderstood your problem. You want the form to stay open so that the user can make corrections. Possible solutions:
Solution 1: Pass in the movieManager object into MovieForm through a parameterized constructor. This way you can check the list before closing the form (on the button's click event).
Solution 2: Create a static MovieManager.GetMovieFromList method so that you aren't required to instantiate it.
I hope this makes sense.

You still have the movieForm object. You could do movieForm.ShowDialog() again.
Don't forget to fill in the edit fields again with the values in onShow or similar method.

Move the check/add code inside your MovieForm and then you can simply just call movieForm.ShowDialog(). You could also raise an event to the main form that the movie was added.

Related

Close current form if dialog results for dialogbox is cancel

I have a Form (frmcustlist).
At one time on this list i scan the list using a dataset and check if it now 0 (no customers left).
At this stage i have an input box pop up (dialog) to ask a new customer name.
If they press OK everything is fine. I also have validation on the box for the input.
However if they press CANCEL, i can get it to escape out of the dialog, but not close frmcustlist that the dialog was called from.
using (inputbox ipfirst = new inputbox("Enter Customer First Name:", "", ""))
{
if (ipfirst.ShowDialog() == DialogResult.OK)
{
newfirstname = ipfirst.answer;
}
else
{
this.Close();
}
}
Now, this.close() doesn't work at all.. so i used return; which stops it going on to ask for the last name and date of birth.. but i want it to stop asking questions (like return) AS WELL as close frmcustlist.
...
Thanks for any advice you can give.
ps. this appears in a few places, but is called in frmcustlist_load as well.. i dont know if that is going to make a difference or not!
Answer was made by STEVE in comments.
As frmcustlist was called itself as a Dialog, i just ended up having to give that dialog a Cancel result.
Final Code that works:
using (inputbox ipfirst = new inputbox("Enter Customer First Name:", "", ""))
{
if (ipfirst.ShowDialog() == DialogResult.OK)
{
newfirstname = ipfirst.answer;
}
else
{
DialogResult = DialogResult.Cancel;
return;
}
}

How to add/remove items from a list from a different form?

Say I have a list called listOfFruits in my main form. In a second form I've made I want the user to be able to remove items from that list to a second list called removedFruits. Currently I know I can access these lists in my second form simply passing them as parameters in the form constructor. However c# can't do pointers (correct?) so how can I effect the main form's copy of these lists from my second form? Because currently any changes to those lists in my second form don't effect the main form's original copy of the lists. If I were to remove 5 fruits from the listOfFruits passed to my second form then after finishing my work the main form would still still have a full listOfFruits and an empty removedFruits. Is there a simple fix to this? Maybe a get/set or a way to add/remove items from the original lists from the second form? Maybe the answer is in some sort of accessor stuff?
EDIT: To clarify; I want to add to one list, and remove from another. Not add/remove to the same list. Not sure if this matters entirely but I figured I'd be specific here in case it does.
EDIT2: I think the issue is I'm copying the original list from the first form and not editing it directly. Can someone fix my code so I can access the original list from my second form instead of making a copy of the list?
public partial class ListSelector : Form
{
private string windowName = Form1.typeOfModuleAdded;
public List<IOModule> innerIOList;
IOModule cardAdded = null;
public ListSelector(List<IOModule> cardList)
{
this.Text = windowName;
innerIOList = cardList;
InitializeComponent();
InitializeList();
}
private void InitializeList()
{
if (windowName == "Drive")
{
string[] listDrives = { "ACS880", "test" };
listBox1.Items.AddRange(listDrives);
}
else if (windowName == "IOBlock")
{
if (!innerIOList.Any())
{
MessageBox.Show("No cards loaded! Please import cards from IO List.", "Error Empty Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
foreach (IOModule card in innerIOList)
{
cardAdded = card;
listBox1.Items.Add(card.name);
}
}
else if (windowName == "Local Card")
{
string[] listLocals = { "1756-EN2T", "test" };
listBox1.Items.AddRange(listLocals);
}
else if (windowName == "Processor")
{
string[] listProcessors = { "1756-L71S", "test" };
listBox1.Items.AddRange(listProcessors);
}
}
private void addBtn_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
Form1.SetModule(listBox1.SelectedItem.ToString());
Form1.confirmedAdd = true;
this.Close();
}
else if (cardAdded != null)
{
innerIOList.Remove(cardAdded);
}
else
{
MessageBox.Show("No module selected!");
}
}
and here's how I pass the list to that form from my first form:
ListSelector test = new ListSelector(ioList);
test.ShowDialog();
where ListSelector is the name of my second form, and ioList is the list im passing to it.
EDIT3: added more code
"However c# can't do pointers (correct?) so how can I effect the main form's copy of these lists from my second form?"
No, not correct. Any object reference (for instance, of a List<Fruit>) is still very much a pointer to a place in memory, and if you pass the same List<Fruit> object to both Forms, they share the same List.
I don't know why your changes to your listOfFruits don't chow up in your first Form. I would check the following things:
Are you 100% sure you use the same List<Fruit> object in both Forms. (If you create a new List like this: new List<Fruit>(listOfFruits) it is NOT the same List)
Does the first Form have any way of finding out, that the List has changed? Possible using a Timer with recurring checks, or (my favorite) triggering an event when you change something, and subscribe an EventHandler in the first Form to the event.
I assume that you have created a second list in your second form that is filled with the items of the first form's list. Then changes on the second list aren't reflected in the first list. You have to use the same reference of the list.
public Form2(List<Fruit> listOfFruits)
{
this._listOfFruits = listOfFruits;
}
private List<Fruit> _listOfFruits;
Instead using a public field, try to use property and on creating your new ListSelector pass the list to the property.
public partial class ListSelector : Form
{
private string windowName = Form1.typeOfModuleAdded;
private List<IOModule> innerIOList;
IOModule cardAdded = null;
public List<IOModule> CardList
{
get
{
return innerIOList;
}
set
{
innerIOList = value;
InitializeList();
}
}
public ListSelector()
{
this.Text = windowName;
InitializeComponent();
}
When creating your new ListSelector object
ListSelector ls = new ListSelector();
ls.CardList = your mainform list of IOModule here
ls.ShowDialog();

C# Stop the flow of the program and reset it without restarting

I am writing a simple WindowsForm program with radiobuttons, buttons, and a customary implemented InputBox.
Relevant program logic: I click one of the radiobutons from the groupbox -> enable button -> clicking button initiates custom inputbox asking for 1 value and Yes/Cancel buttons.
|
V
If Yes button clicked -> proceed with logic flow
If Cancel button clicked -> popup window appears ("Your input was cancelled, do you want to exit application?") with yes/no buttons
|
V
If Yes button clicked -> Exit the program
If No button clicked -> apply the same logic as if Reset button is clicked, which would reset the whole program without restarting it <---------This is what I am trying to achieve (all relevant methods are provided below)
What is the problem?
When I simply click Reset button, it applies all needed actions and stops, waiting for my further input. This is the exact result I am trying to achieve when I click No button in the popup window I mentioned above.
However, this is not the case. During the debug mode, after I clicked No button, it goes through the entire logic of the Reset button like I wanted, BUT right after that, it goes into the IF statement (marked in my buttonGetWorkHours_Click method). I don't want that, I want it to stop the flow after applying the logic of Reset button and waiting for my input (radiobutton/button click).
What I tried
I have searched through several threads here in SO and tried implementing several suggestions. The results of these suggestions are commented out inside inputBoxProcedure method. Also, I was looking for similar posts, which would give me the right idea. But they state that it is impossible without reloading. Based on that thread, I thought about implementing extra variable to use for checking if reset logic was running, but seems unnecessarily complicated.
ULTIMATE QUESTION:
I saw a test executable, so I know it is POSSIBLE, unlike what posts and threads were saying. Can you please point me in a right direction of how to proceed with it?
Relevant code snippet methods
For the sake of saving everyone's time I will include only methods relevant to my question.
private void buttonGetWorkHours_Click(object sender, EventArgs e)
{
if (radioButtonJobStatusFull.Checked)
{
inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40");
}
else if (radioButtonJobStatusPart.Checked)
{
inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30");
}
//if, else if, else, unrelated to the lines above
if() //<----------------the logic goes here after going through the whole Reset button logic
{}
else if()
{}
else()
{}
}
private void buttonReset_Click(object sender, EventArgs e)
{
//clear all radiobutton selections
//set all strings to empty
//disable several buttons and groupboxes
//hide several labels
}
private void inputBoxProcedure(string text, string defaulttext)
{
InputBoxResult result = InputBox.Show(text, "Hours Entry", defaulttext, new InputBoxValidatingHandler(inputBox_Validating));
if (result.OK)
{
labelHoursWorked.Text = result.Text.Trim();
}
else
{
if (MessageBox.Show("Input was cancelled. Do you wish to quit the application?", "Input Cancelled", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
Close();
}
else
{
buttonReset_Click(this, new EventArgs());
//Form fr = new Form();
//fr.Show();
//this.Close();
//Application.Restart();
}
}
}
Try changing the inputBoxProcedure to this:
private bool inputBoxProcedure(string text, string defaulttext)
{
InputBoxResult result = InputBox.Show(text, "Hours Entry", defaulttext, new InputBoxValidatingHandler(inputBox_Validating));
if (result.OK)
{
labelHoursWorked.Text = result.Text.Trim();
}
else
{
if (MessageBox.Show("Input was cancelled. Do you wish to quit the application?", "Input Cancelled", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
{
Close();
}
else
{
buttonReset_Click(this, new EventArgs());
//Form fr = new Form();
//fr.Show();
//this.Close();
//Application.Restart();
return false; // Added
}
}
return true; // Added
}
Notice that the return type void becomes bool and that two lines of return have been added.
In buttonGetWorkHours_Click change:
if (radioButtonJobStatusFull.Checked)
{
inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40");
}
else if (radioButtonJobStatusPart.Checked)
{
inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30");
}
Into:
if (radioButtonJobStatusFull.Checked)
{
if (!inputBoxProcedure("Enter the total hours worked by the full time employee for the week", "40"))
return;
}
else if (radioButtonJobStatusPart.Checked)
{
if (!inputBoxProcedure("Enter the total hours worked by the part time employee for the week", "30"))
return;
}
In this way, after a reset, the funtion inputBoxProcedure will return false. When the function returns false the function buttonGetWorkHours_Click will return and thus prevent further excecution.
I hope this helps.

How to reload or refresh Windows Form into original state?

How to reload or refresh the Windows Form into original state? i have used this.Refresh();,this.Invalidate();,form.Refresh(),form.Invalidate()
private void AdduserBtn_Click_1(object sender, EventArgs e)
{
UserManagement obj = new UserManagement ();
obj.CourseCategoryId = (int) CourseRegCbox.SelectedValue;
obj.IDNumber = IDNumberTbox.Text;
obj.Password = PasswordRegTbox.Text;
obj.FName = FnameRegTbox.Text;
obj.LName = LnameRegTbox.Text;
obj.Gender = GenderTbox.Text;
obj.Email = EmailRegTbox.Text;
obj.PhoneNumber = PhonenumberRegTbox.Text;
obj.Address = AddressRegTbox.Text;
if ( UserManagement != null && UserManagement.Id > 0 )
{
obj.Id = UserManagement.Id;
if ( UserManagement.UserInfo_Update (obj) > 0 )
{
MessageBox.Show ("Record Succesfully Updated!");
UserInfoForm form = new UserInfoForm ();
form.Refresh ();
}
else
{
MessageBox.Show ("An error occured!");
}
}
else
{
if ( UserManagement.UserInfo_Insert (obj) > 0 )
{
MessageBox.Show ("Record Succesfully Added!");
UserInfoForm form = new UserInfoForm ();
form.Refresh ();
}
else
{
MessageBox.Show ("An error occured!");
}
}
}
I just want to reload the form into original state once the data properly save or updated.
"this.Refresh();,this.Invalidate();,form.Refresh(),form.Invalidate()"
These functions just tell the window manager to redraw the form graphic; they have nothing to do with the state of the form's data.
Seems that all you need to do is set your control values back to their original values So, make a function on the form:
private void ResetForm()
{
//write code here to setup your dropdowns, put empty strings into textboxes, etc.
//pretty much the reverse of the process by which you copy the values into your user object.
}
and then in the sucess part of the code call the function:
if ( UserManagement.UserInfo_Update (obj) > 0 )
{
MessageBox.Show ("Record Succesfully Updated!");
//reset this form, no need to make another one...
ResetForm();
}
and you can also include a call to ResetForm() somewhere in your Form_Load, etc.
However
I'd recommend that once you are comfortable with doing this, you then stop doing it and use the data-binding facility that's built into Winforms; what it allows you to do is use the designer to bind user interface elements on the form (textboxes, etc) to various Class properties (e.g. UserManagement class).
This way you can simply "reset" your form by creating a new instance of UserManagement without having to deal with all the cruddy details of clearing out textboxes, etc. Otherwise you will find as your objects grow more complex, writing the code to manually reset form UI elments becomes more and more tedious and error-prone.
Hope that helps.
This is simple. You must create a new form object and close current form.
Form fr = new Form();
fr.Show();
this.Close();
I accomplished this by closing and opening the form. So replace the ***** with your form name
***your form name** ss = new **your form name***();
ss.Show();
this.Hide();
I would also recommend putting a form close to fully close the form:
private void ***Your form name***Closed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
Try this :
this.Controls.Clear();
this.InitializeComponent();

Check if form is opened and update if opened

I want to check if an instance of a form is opened and open the existing instance to update a textbox else create a new instance.
After searching I found : How to check if a windows form is already open, and close it if it is?
From the accepted answer I tried
try
{
foreach (Form fm in Application.OpenForms)
{
if (fm is Form2)
{
Form2 n1 = (Form2)Application.OpenForms["Form2"];
n1.textBox1.Text = textBox1.Text;
break;
}
else
{
Form2 n1 = new Form2();
n1.textBox1.Text = textBox1.Text;
n1.Show();
}
}
}
catch (InvalidOperationException)
{
}
Apart from that this code throws an InvalidOperationException (which i am already catching), The code doesn't work because if an instance already exists it still creates a new instance.
What am i doing wrong?
A better approach would be to filter the OpenForms based on the form type:
var form2collection = Application.OpenForms.OfType<Form2>();
You can then loop over those, or if the collection is empty, open a new form. The advantage is that you aren't relying on the form name, but the actual class definition of the form, which is more reliable.
Additionally, I tend to avoid direct manipulation of controls from other code. I find it more reliable if others call a method, such as
public void setSomeControl(string value)
{
this.controlName.Text = value;
}
then call
form2collection[0].setSomeControl("new value");
which allows your form to do all the housekeeping and the calling code can ignore those details.

Categories

Resources