Reputation: 55
I have recently created a Database in Visual Studio. I have two forms, the first form displays a table. The second form is connected to the first table and displays the information in a design view.
When the user changes the data in the design view, if they click save, the changed information is updated on the original form when the second form is closed.
However, if they close the form without saving, the information on the original form remains unchanged.
I want to create a Save button to clearly show the user that they must save any changes they make in the design view.
Has anyone created a 'save' button before? For a form connected to another form, not a file?
Upvotes: 0
Views: 25126
Reputation: 1
so if you want to save it you can use this code for the save button
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "html|*.html|lua file|*.lua|text
document|*.txt|video|*.mp4|image|*.jpg";
saveFileDialog1.Title = "save project";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the Image via a FileStream created by the OpenFile method.
System.IO.FileStream fs =
(System.IO.FileStream)saveFileDialog1.OpenFile();
// Saves the Image in the appropriate ImageFormat based upon the
// File type selected in the dialog box.
// NOTE that the FilterIndex property is one-based.
switch (saveFileDialog1.FilterIndex)
{
case 1:
this.button2.Image.Save(fs,
//change button number to what your button number is
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
case 3:
this.button2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Gif);
break;
}
fs.Close();
Upvotes: 0
Reputation: 3391
Not sure if I understood your question correctly but I put together this little example to try and replicate what you were asking.
Here is the main form:
The main form consists of a data grid view (showing data that could have come from a database) and an edit button that is used to open a second form that will be used to edit the DataValue of the selected item in the grid view. Form2 (the edit form) looks like this:
As you can see I put a Save button and a Cancel button on Form2. I am using Form2 as a modal dialog form (so the test form cannot be changed while Form2 is open). This means I can set the DialogResult properties of the Save and Cancel buttons and use these to inform the TestForm what to do. The Save button is set so that DialogResult = OK
and the Cancel button is set so that DialogResult = Cancel
.
I also added a special public property to From2 that can be used to get and set the data. In my case this is just a simple string, but in a more advanced case this would get and set an object of a special type or perhaps a data row from the table. Here is my property:
public string DataValue
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}
Now behind my Edit button on the TestForm I have the following event code:
private void EditButton_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count != 1) { return; }
string data = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
// show the form as a modal dialog box while editing.
Form2 editForm = new Form2();
editForm.DataValue = data;
if (editForm.ShowDialog() == DialogResult.OK)
{
// If the user clicks Save then we want to update the datagrid
// (and eventually the database).
dataGridView1.SelectedRows[0].Cells[1].Value = editForm.DataValue;
}
}
If you need a non-modal version of Form2 then you will not be able to do all this within the edit button event. Instead you will have to spawn a new edit form and handle it's close event. You will then need to move the code that updates the grid view into this close event. Each spawned edit form will need to remember which data row it is editing so that the close event can update the correct row.
I hope this helps.
Upvotes: 0
Reputation: 981
What about using the event FormClosing ?
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Do you want to save changes to your table?", "My Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
e.Cancel = true;
}
}
Upvotes: 0