0

ここは初心者なので、おかしなことを言って申し訳ありません

ユーザーが値を選択して送信ボタンをクリックするコンボボックスがあります。選択した値を取得してテキストファイルに書き込む方法がわかりません。助けてくれる人はいますか?

前もって感謝します...ジンボブ

4

3 に答える 3

1

試してみてください:

    string PathToFile = "c:\\File.txt";
    System.IO.File.WriteAllText(PathToFile,Combobox.SelectValue.ToString());
于 2012-11-22T13:14:22.153 に答える
0

あなたのコードを見ずに、MVVMを使用していないと仮定すると、これを実現するにはいくつかのものが必要になると思います:

  1. Name="myComboBox" のように、コンボ ボックスに XAML で名前を付けます。
  2. また、XAML で、クリック イベントをボタンに追加します。
  3. ボタンのクリック ハンドラーで、次のように記述します。 System.IO.File.WriteAllText("selected.txt", myComboBox.SelectedValue);
于 2012-11-22T13:21:04.453 に答える
0

オブジェクトを返すComboBoxusingの選択された値を検出できます。comboBox1.SelectedValue必要に応じて、次を使用して文字列に変換できますToString()

string _string = comboBox1.SelectedValue.ToString();

これにより、 name の新しい変数が で_string選択されたComboBox値として初期化されますStringcomboBox1.SelectedIndexまた、 which を返す を使用して、それがintであることを考慮して、アイテムの選択されたインデックスを取得することもできます。comboBox1ComboBox

さらに、特定のファイルに値を書き込みたい場合は、StreamWriterorを使用できますがFile.WriteAllLines、 a のStreamWriter方が管理しやすいと思います

string DestinationFile = @"D:\Resources\International\MyNewFile.txt"; //Initializes a new string of name DestinationFile as D:\Resources\International\MyNewFile.txt
StreamWriter _StreamWriter = new StreamWriter(DestinationFile); //Initializes a new StreamWriter class of name _StreamWriter
_StreamWriter.WriteLine(comboBox1.SelectedValue.ToString()); //Attempts to write the selected combo box value in string as a new line
_StreamWriter.Close(); //Closes the file and saves settings

注意: これにより、D:\Resources\International\MyNewFile.txt に新しいファイルが作成されます。次に、ファイルを選択した項目で改行して上書きComboBoxします。テキストを特定のファイルに追加したい場合は、後に追加することをお勧め,trueます_StreamWriter = new StreamWriter(DestinationFile

string DestinationFile = @"D:\Resources\International\MyNewFile.txt"; //Initializes a new string of name DestinationFile as D:\Resources\International\MyNewFile.txt
StreamWriter _StreamWriter = new StreamWriter(DestinationFile, true); //Initializes a new StreamWriter class of name _StreamWriter
_StreamWriter.WriteLine(comboBox1.SelectedValue.ToString()); //Attempts to write the selected combo box value in string as a new line
_StreamWriter.Close(); //Closes the file and saves settings

ありがとう、
これがお役に立てば幸いです:)

于 2012-11-22T13:26:56.600 に答える