ここは初心者なので、おかしなことを言って申し訳ありません
ユーザーが値を選択して送信ボタンをクリックするコンボボックスがあります。選択した値を取得してテキストファイルに書き込む方法がわかりません。助けてくれる人はいますか?
前もって感謝します...ジンボブ
ここは初心者なので、おかしなことを言って申し訳ありません
ユーザーが値を選択して送信ボタンをクリックするコンボボックスがあります。選択した値を取得してテキストファイルに書き込む方法がわかりません。助けてくれる人はいますか?
前もって感謝します...ジンボブ
試してみてください:
string PathToFile = "c:\\File.txt";
System.IO.File.WriteAllText(PathToFile,Combobox.SelectValue.ToString());
あなたのコードを見ずに、MVVMを使用していないと仮定すると、これを実現するにはいくつかのものが必要になると思います:
オブジェクトを返すComboBox
usingの選択された値を検出できます。comboBox1.SelectedValue
必要に応じて、次を使用して文字列に変換できますToString()
例
string _string = comboBox1.SelectedValue.ToString();
これにより、 name の新しい変数が で_string
選択されたComboBox
値として初期化されますString
。comboBox1.SelectedIndex
また、 which を返す を使用して、それがint
であることを考慮して、アイテムの選択されたインデックスを取得することもできます。comboBox1
ComboBox
さらに、特定のファイルに値を書き込みたい場合は、StreamWriter
orを使用できますが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
ありがとう、
これがお役に立てば幸いです:)