Make a Windows Form save my changes.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 3999 Views - Last Post: 16 December 2014 - 12:23 PM Rate Topic: -----

#1 Smedleyja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-July 11

Make a Windows Form save my changes.

Posted 09 December 2014 - 09:00 AM

I am having trouble figuring out how to get my windows form to save the changes I've made to the form. Here is the code that I keep finding online but it appears I need to add something to it but I have no clue what it is at the moment.

 private void btnsave_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                }
            }
        }



Any help is much appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Make a Windows Form save my changes.

#2 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 09:08 AM

Hi

What errors are you getting? Does the code build?

My guess is it doesn't and you are missing a using statement for the StreamWriter which is part of the System.IO namespace. Add the following to the top of your code file:
using System.IO;


Having said that, if your intention is to save settings, such as fonts, colours, screen sizes etc. then you would be better off using the Settings which is provided to you as part of your application and is essentially an XML file that is used for this very purpose.

Check out this MSDN article for further information: How To: Write User Settings at Run Time a With C#


HTH
Was This Post Helpful? 0
  • +
  • -

#3 Smedleyja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-July 11

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 09:30 AM

Yeah I have that namespace on there. It does build with no errors. It just shows the save button as a grayed out button that you cannot click on.

The application is basically a bill tracker. I want to click a button that says paid and the button turns black to show its paid for example. I want to be able to save that change as well with entering amounts in text boxes etc.

Does that make sense hopefully?
Was This Post Helpful? 0
  • +
  • -

#4 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 09:44 AM

Hi

Why is your button disabled? Is there code somewhere that does this intentionally?

Regarding the saving, yes that makes sense, but what sort of structure are you going to use? Plain text, XML? I'm assuming you are going to want to read this data back into the program?

If you could provide more information it would help.
Was This Post Helpful? 0
  • +
  • -

#5 Smedleyja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-July 11

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 10:02 AM

This is my whole code. I'm new to all this and have watched some videos to try and teach myself more about it. I did go to school for it but its been a year and haven't really messed with coding much. I've been more into networking. So as far as the structure I'm not sure. I just want to be able to save my changes to the program so when I reopen the file I saved I can continue my work and edit/save again over and over like a word file. I'm sorry I'm not much help I'm sure but neither is google at the moment anyways lol

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Bills
{
    public partial class Bills : Form
    {
        public Bills()
        {
            InitializeComponent();
            btnsave.Enabled = false;
        }

        private void btnOutofAccount_Click(object sender, EventArgs e)
        {
            this.btnOutOfAccount.BackColor = Color.Black;
            this.btnOutOfAccount.ForeColor = Color.White;
        }

        private void btnPaid_Click(object sender, EventArgs e)
        {
            this.btnPaid.BackColor = Color.Black;
            this.btnPaid.ForeColor = Color.White;
        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string name = saveFileDialog1.FileName;
            File.WriteAllText(name, "test");
        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            Stream myStream;
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Code to write the stream goes here.
                    myStream.Close();
                }
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 10:19 AM

Hi

In your constructor for your Form (line 18) you are setting the Enabled property of your btnSave button to False and I don't see anywhere where you enable it. Is there a reason for this? If not then remove that line.

In terms of structure you should give some thought to how this might work. For example, if you are only saving a small amount of data then look into the Settings option I mentioned, this will allow you to persist data when your application is closed and it is very easy to read the data back when your application is loaded again.

If however there are many things to save then an XML a file might be more appropriate and there are many ways we can implement the reading and writing of XML files. However, before going into detail, can you provide information on what you are saving and reading back (in detail).

And don't worry about not being much help, just need to know more detail to be able to help you better.

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 10:46 AM

Two comments:

1 - WinForms is 20+ years old and not the way new software is built today. Since its all equally new to you I wouldn't waste my time on WinForms. Just go straight to WPF. Its a better use of your next 10,000 hours to become proficient and hireable.
2 - Check out the FAQ below, question 13 for several techniques for saving data and preferences for your application. My preferred method (if not using a database) is serialize an XML document. That way you can still open the document directly if you *have* to, to make changes and it deserializes back into an object - making you design in a proper OOP way to save you a ton of headache down the line from not going OOP and just whacking together code.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2014
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 Smedleyja   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 10-July 11

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 12:11 PM

View Postdjjeavons, on 09 December 2014 - 10:19 AM, said:

Hi

In your constructor for your Form (line 18) you are setting the Enabled property of your btnSave button to False and I don't see anywhere where you enable it. Is there a reason for this? If not then remove that line.

In terms of structure you should give some thought to how this might work. For example, if you are only saving a small amount of data then look into the Settings option I mentioned, this will allow you to persist data when your application is closed and it is very easy to read the data back when your application is loaded again.

If however there are many things to save then an XML a file might be more appropriate and there are many ways we can implement the reading and writing of XML files. However, before going into detail, can you provide information on what you are saving and reading back (in detail).

And don't worry about not being much help, just need to know more detail to be able to help you better.

Thanks



Okay, yeah I'm not sure how that got there to be honest. Maybe it was something in one of the save code bits I added in. Who knows. Thanks for that.

Deleting that did allow the button to show and save my project but it saved it in the format in which I couldn't reopen it and see the program as how it was. I see it's saving it as a .txt format, I tried .exe but that didn't work either.

Here's is how the program works and I want to save every change to the program as if I was editing and saving an excel sheet etc.

I have a list of bills and beside each bill name I have two text boxes. Each of those text boxes need to accept information that the user inputs and save it. Beside the two text boxes are two buttons that are highlighted when clicked to show if the bill has been paid and or come out of the users account. I want the program to be able to save these button changes as well. So basically it has to save the program in its current state after the user gets done entering in the bill amounts, due dates and or clicking that is has been paid or been withdrawn from their account.


Bill Name txtboxAmount(Info entered by user) txtboxDueDate("Ditto") BtnPaid(Turns black on click) BtnRA(turns black on click)


That is basically how its setup. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 12:52 PM

You need to stop trying to build this. Sorry for how harsh this sounds, but you clearly don't understand enough about programming (yet) to architect this program.
You also are also clearly dragging out controls and banging some code out without any plan of HOW this is going to work, what format the data needs to be in, how you are going to save and so on. Would you start spray painting metal, bolting together some parts you found laying around... and then later ask "How do I build a motorcycle?"

You don't really "save the state" of the program. You save data. You need to create a class for your data: A Bill class. Then you would have a collection of those, such as List<Bill>. A Bill object would have properties such as:
  • Name
  • Amount
  • Start Date
  • End Date
  • Repeat frequency
  • PaidOnDate
  • And so on


Quote

I want the program to be able to save these button changes as well.

The buttons don't have changes. They might change a property on the selected Bill object. For example an unpaid bill would have the IsPaid property changed from false to true, or update the PaidOnDate to today's date. You would then save that updated bill object.

People have numerous bills. Saving this as files is a bad way to go. You want to do this with some flavor of database. A compact database would be fine. Here is another thread where someone is asking questions about just such a database: http://www.dreaminco...0&#entry2083075

There are tutorials linked in my signature block for properties and for classes.
Plus here's one on FIRST PLANNING then building your first application:
http://www.dreaminco...ister-part-one/
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15804
  • View blog
  • Posts: 63,309
  • Joined: 12-June 08

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 12:57 PM

This looks an awful lot like the webform question you posted earlier. Is this a webform or desktop form?

http://www.dreaminco...-button-colors/
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make a Windows Form save my changes.

Posted 09 December 2014 - 01:03 PM

andrewsw moved the other topic to ASP.NET|WebForms because the OP stated "web forms" in his question. I'll bet that was a typo or lack of understanding between WinForms and WebForms.

This post has been edited by tlhIn`toq: 09 December 2014 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make a Windows Form save my changes.

Posted 11 December 2014 - 10:00 AM

Just a thought... You need to make objects for your bills... You need to store all that data in a database (a local compact database is fine)... You probably want to make use of EntityFramework. That way you can make your bill class (and other classes) and make use a database with minimal plumbing: The entity framework handles most of that for you.

http://msdn.microsof...e/hh852588.aspx

http://www.entityfra...code-first.aspx
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6905
  • View blog
  • Posts: 28,567
  • Joined: 12-December 12

Re: Make a Windows Form save my changes.

Posted 11 December 2014 - 10:19 AM

View PosttlhIn`toq, on 09 December 2014 - 08:03 PM, said:

andrewsw moved the other topic to ASP.NET|WebForms because the OP stated "web forms" in his question. I'll bet that was a typo or lack of understanding between WinForms and WebForms.

Yes, "web forms" is stated in both the title and the topic-text. On looking further though, it is about WinForms, not WebForms.

[It could be moved I suppose, but might confuse. Perhaps the link in this current topic is sufficient?]
Was This Post Helpful? 0
  • +
  • -

#14 optix212   User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: Make a Windows Form save my changes.

Posted 11 December 2014 - 12:27 PM

Just a question...

What's the problem with Properties.Settings.Default.Save?

If you're honestly storing a ton of data, what I usually do (kind of similar to the Klingon) is store everything in a string and save that string to Properties.Settings.Default. I use a comma or some other type of delimiter to separate each individual variable, and then when my program loads I just grab the string out of my Properties.Settings.Default, use string.Split() with my delimiter, and parse it back into an object. You can even break your string into "chunks" using a series of different delimiters.. This is usually the way people save video game data with the Unity3D engine as well. Is this okay to do, or have I been doing it wrong the entire time? lol

This post has been edited by optix212: 11 December 2014 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Make a Windows Form save my changes.

Posted 11 December 2014 - 12:34 PM

I wouldn't do it this way.
Its hard to modify a single value.
Its very much locked to a given version. For example, if in version 2 of your application you add several settings but have to maintain backwards compatibility its hard to deal with.
Its very code intensive. When you deserialize a settings object the .NET framework does all the work for you. I'll be you have 100+ lines of your string.split() method just to set and then again to save - and it will get longer with every addition to the settings object.
What you have sounds like it really isn't object oriented, though its hard to tell without seeing the code. But I'd bet you don't really have a 'properties' or 'userPriveleges' object used throughout your program.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3