moskalak
moskalak

Reputation: 271

Handling information from forms

I'm sure this is a very noob-ish question, but I've been looking for this information for past two hours and haven't found anything, really. (I'm sure I just didn't ask the right question, though.)

The thing is, I'm designing an application which needs a user to provide a lot of information, I guess it's like 14 TextBoxes. Now, my problem is I'm not sure what is the best way to handle (and process) this information.

After getting it all from a user, I want to pass it to a class called SaveIt which uses then passes it to SerializeToXML, which uses XMLSerialize to save the info to an XML file. But that's a hell lot of writing and a hell lot of variables to create and I'm sure it can be done in a more elegant and smart way.

To recap: I have to pass this information 2 times (once to SaveIt, then to SerializeToXML), plus I have to define a class that XMLSerialize will use to, well... serialize it.

What is the smartest way to do that?

Upvotes: 1

Views: 81

Answers (4)

James2707
James2707

Reputation: 122

One approach to this is use databinding. You need to add to the text box databinding, the property of your object SaveIt, and this fills your property automatically when the user writes in the textbox.

You can add this in the constructor of the form for every property.

SaveIt saveIt = new SaveIt();
textBoxFirstName.DataBindings.Add("Text", saveIt.FirstName, "FirstName");
textBoxLastName.DataBindings.Add("Text", saveIt.LastName, "LastName");

and you need to implement the interface INotifyPropertyChanged in your class SaveIt like this:

public class SaveIt : INotifyPropertyChanged {


    public event PropertyChangedEventHandler PropertyChanged;

    private string _firstName = string.Empty;
    private string _lastName = string.Empty;

    public string FirstName {
        get { return _firstName; }
        set {
            _firstName = value;
            OnPropertyChanged("FirstName");
        }
    }

    public string LastName {
        get { return _lastName; }
        set {
            _lastName = value;
            OnPropertyChanged("LastName");
        }
    }

    private void OnPropertyChanged(string propertyName) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Using this you don't need to fill all the Properties manually

Then you can use the approach of Casperah to serialize the SaveIt object

Upvotes: 0

ispiro
ispiro

Reputation: 27703

One way would be to just pass the original Form as a parameter. Then get the information when you really need it. But then those methods will be hard coded for this job, and you won't be able to reuse them.

So how about creating a class with those variables, and passing an instance of that class?

public class TextBoxesInformation
{
    public string box1string;
    public string box2string;
    //...
}

Upvotes: 1

ispiro
ispiro

Reputation: 27703

I already wrote an answer, but perhaps the main point of your question was different so here's an answer for a different aspect of the question.

Perhaps something like:

foreach (Control ctrl in Controls)
    if (ctrl is TextBox)
        dictionary.Add((ctrl as TextBox), (ctrl as TextBox).Text);

Where:

Dictionary<TextBox, string> dictionary = new Dictionary<TextBox, string>();

Upvotes: 1

Casperah
Casperah

Reputation: 4554

You should make your SaveIt class serializable. The XML Serializer only saves properties.

[Serializable]
public class SaveIt
{
    public string Text1 {get; set;}
    public string Text2 {get; set;}
    ...
}

private void Save(string filename)
{
    SaveIt save = new SaveIt();
    save.Text1 = textBox1.Text;
    ...

    using(Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
    {
        XmlSerializer xmlserializer = new XmlSerializer( typeof(SaveIt) );
        xmlserializer.Serialize( stream, save );
    }
}

private void Load(string filename)
{
    SaveIt load;
    using(Stream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        XmlSerializer xmlserializer = new XmlSerializer( typeof(SaveIt) );
        load = xmlserializer.Deserialize(stream) as SaveIt;
    }

    textBox1.Text = load.Text1;
    textBox2.Text = load.Text2;
    ...
}

But I am afraid that there is no easy way to copy data from form to the SaveIt class.

Upvotes: 1

Related Questions