【问题标题】:Save Windows Form App Combobox and Textbox Selections of Individual Users and Load Them With a Button保存单个用户的 Windows 窗体应用程序组合框和文本框选择并使用按钮加载它们
【发布时间】:2018-05-03 11:45:41
【问题描述】:

我有一个用 C# 编写的 Windows 窗体应用程序。它有一个文本框、一个组合框和一个按钮。该按钮是一个保存按钮,旨在保存用户在文本框中输入的值并在单击时将组合框设置为,并且应该能够保存多个不同的选择,例如一个用户将“test1”添加到文本框和选择组合框的 0 索引选项并单击保存按钮以保存这些选项,然后另一个用户可以将“test2”添加到文本框并选择组合框的 2 索引选项并单击保存按钮以保存这些选项,结果在可以加载回应用程序的两个单独保存的选择中,从而使表单具有选择的保存文件数据。

问题是我不知道如何实现这样的功能,或者从哪里开始。我研究过将用户的选项保存为 xml 文件,但显然这不能用组合框数据来完成。

这是我正在尝试做的图表。

关于如何在 c# 中的 Visual Studio Windows 窗体应用程序中实现这一点有什么建议,或者从哪里开始?

【问题讨论】:

标签: c# visual-studio


【解决方案1】:

这个描述的功能可以使用txt文件来实现。我使用它们是因为它们相当简单。

保存:

private void SaveButton_Click(object sender, EventArgs e)
{
   SaveFileDialog sfd = new SaveFileDialog();
   sfd.Filter = "Text files (*.txt)|*.txt";
   sfd.ShowDialog();
   string name = sfd.Title; 
   string filePath = sfd.FileName; 

   StreamWriter sw = new StreamWriter(filePath);
   // Write the text you want to the file.
   sw.WriteLine(MyTextBox.Text);
   sw.WriteLine(MyComboBox.SelectedIndex); 
   // You could do MyComboBox.SelectedItem if you evaluate what index the item 
   // belongs at when reading back the file for the load function.

   sw.Close();

}

加载:

private void LoadButton_Click(object sender, EventArgs e)
{
   OpenFileDialog ofd = new OpenFileDialog();
   ofd.Filter = "Text files (*.txt)|*.txt";
   ofd.ShowDialog();

   // If the user doesn't select a file, cancels, or exists the dialogue box 
   nothing is done.
   if (ofd.FileName == null || ofd.FileName.Equals("")) { }

   // Else if the user has selected a file, the file's text is converted when necessary, and used to change the value of the applications controls.
   else
   {
       // Get the contense of the txt file as a string array.
       string[] list = File.ReadAllLines(ofd.FileName);

       // Reads the text stored in the txt file and puts it as the value for the boxes.
       MyTextBox.Text = list[0];
       MyComboBox.SelectedIndex = list[1];

   }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2020-08-12
    相关资源
    最近更新 更多