我想知道WPF是否可以保存单选按钮内容并使用事件处理程序将内容发送到文本文件。我是否需要在事件处理程序中执行所有操作,或者我可以调用某个类?
public void method(){
string s = "Test1&Test2&Test3&Test4";
string[] s = Spliting.Split(new string[] { "&" }, StringSplitOptions.RemoveEmptyEntries);
List<RadioButton> radioButtonList = new List<RadioButton>();
radioButtonList.Add(radioButton);
radioButtonList.Add(radioButton1);
for (int i = 0; i < radioButtonList.Count; i++)
{
//some code that insert string into the radio button (done)
//when i use streamwriter inside here, it give me this in the text file
//System.Windows.Controls.RadioButton Content IsChecked:False <- this can be turn to true if i put variable.Checked = true
}
private void radioButton_Checked(object sender, RoutedEventArgs e)
{
if (radioButton.IsChecked == true)
{
StreamWriter streamwriters = new StreamWriter ("Sent.txt", false);
streamwriters.Close();
}
else if (radioButton1.IsChecked == true)
{
StreamWriter streamwriters = new StreamWriter("Sent.txt", false);
streamwriters.Close();
}
或者WPF有一个方法可以自动将内容保存到变量中。我错过了什么吗?
我希望单选按钮检查单选按钮并将内容发送到文本文件中。
答案 0 :(得分:-1)
如果您只想将单选按钮显示的文本保存到文件中,则:
private void radioButton1_Checked(object sender, EventArgs e)
{
string radioButtonContent = ((RadioButton)sender).Text;
string savePath = @"C:\sent.txt"; // or wherever
File.WriteAllText(savePath, radioButtonContent);
}