Stringbuilder附加保存文件,List <string>

时间:2017-11-24 00:47:05

标签: c# list append stringbuilder

编辑:

程序:我有一个汽车型号名称列表,旁边是1到4的数字,用户可以在他/她想要保存的汽车的文本框中输入该号码。该数字与汽车名称相关,并将其保存在stringbuilder追加中。

问题:如何在Stringbuilder附加中保存放在文本框中的单词?我也可以创建另一个文本框并保存其他单词抛出相同的Stringbuilder吗?

 public partial class MainWindow : Window
{
    ObservableCollection<string> Items = new ObservableCollection<string>();

    StringBuilder sb = new StringBuilder();

    public MainWindow()
    {

        InitializeComponent();

        Items.Add("1.Honda");
        Items.Add("2.Toyota");
        Items.Add("3.Fiat");
        Items.Add("4.Ferrari");
    }



    private void SaveFile_Click(object sender, RoutedEventArgs e)
    {

        if (txtValue.Text == "1"){txtValue.Text = "Honda";}
        if (txtValue.Text == "2") { txtValue.Text = "Toyota"; }
        if (txtValue.Text == "3") { txtValue.Text = "Fiat"; }
        if (txtValue.Text == "4") { txtValue.Text = "Ferrari"; }

        foreach (string txtValue in Items) //Just playing around trying to figure out how to Append
        {
            sb.Append(Items).AppendLine();
        }


        SaveFileDialog saveFileDialog = new SaveFileDialog();

        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            File.WriteAllText(saveFileDialog.FileName, txtValue.Text);

      }

    }
}

XAML

 <Window.Resources>
    <x:Array x:Key="Items" Type="{x:Type sys:String}"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <sys:String>1.Honda</sys:String>
        <sys:String>2.Toyota</sys:String>
        <sys:String>3.Fiat</sys:String>
        <sys:String>4.Ferrari</sys:String>
    </x:Array>
  </Window.Resources>
  <Grid>

    <StackPanel Margin="10">
        <TextBox x:Name="txtValue"   />
        <ListBox  Height=" 100" ItemsSource="{StaticResource Items}" >
                        </ListBox>
        <Button Name="SaveFile" Click="SaveFile_Click" >OK</Button>
     </StackPanel>
   </Grid>
</Window>

0 个答案:

没有答案