开发者

fill up form with previous details and finding a way to clear form?

I am using wpf in c#.

Question 1: I created a wpf window. Is there a way to load the previous users entered fields of the form via a button click event?

Question 2: So after I load the field of the form, I would like to create a clear button to clear all the fields of the form. Is there a way to achieve this?

Thanks.

EDIT 1

I am currently struggling with XML serialization, trying to read up but i am still pretty lost. From what i know i am going to create a new class and i would have a "Save" button and "Load" button in my application. I have a few combo boxes, each one populated by some XML file already. I also have textboxes, checkboxes for them to fill up. So once users enters the form, i would like to have them to "save" the form entries and be able to "load" them whenever they wish. I have also studied the filebrowserdialog which will open an explorer for user to explore and save a copy of the old form in some directory but right now i only have this in my code:

private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        var dialog = new System.Windows.Forms.FolderBrowserDialog();
        System.Windows.Forms.DialogResult result = dialog.ShowDialog();

    }

this is far from what i require at the moment.

i think i'm pretty lost especially the part on creating new class for the combo box values. please guide me if possible. would also appreciate if you can guide me on text box and combo box values too. thanks.

this is how i populate the combo boxes:

public void PopulateDDLFromXMLFile()
        {
            DataSet ds = new DataSet();
            ds.ReadXml("C:\\GUI\\buildermanageremail.xml");


            DataView dv = ds.Tables["builder"].DefaultView;
            DataView dw = ds.Tables["manager"].DefaultView;

            dv.Sort = "value";

            comboBox1.ItemsSource = dv; //Sets the collection of items from which to populate
            comboBox2.ItemsSource = dw;

            comboBox1.DisplayMemberPath = "value"; //Sets the path within an item to use for display
            comboB开发者_运维问答ox2.DisplayMemberPath = "value";
        }


Yes sure its possible.. You may store per user entries in a database or temp file (Serialize) and show them in your text boxes when you load the form or as you mentioned you have a button to do that. Its similar to what you usually do when you show data from database on your form in Labels/TextBoxes or Gridview.

But to make it work you will need to save the user entries in the first place. You can either you can have a Button that allow user to save data or you can do so in Texthange/ LostFocus events of the controls.

If you don't need data to be persistent you can just store them in the variables and keep them in memory till the lifetime of the app.

For clearing the form a sample code will be like;

foreach(Control ctl in this.Controls)
{
    TextBox tbx = ctl as TextBox;
    if(tbx != null)
    {
         tbx.Text = String.Empty;
    }
}


If you were to move all data out to a Model object (use MVVM to seperate these layers) bind all the controls to properties on the model, then you can simply serialize the model to a file to save and deserialize on load.

A clear button could simply create a new instance of the model, minimal code to write to achieve this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜