简体   繁体   English

WPF 是否有保存单选按钮内容的方法,然后通过事件处理程序发送保存内容

[英]Does WPF have a way of saving radio button content and then sent the save content through a event handler

I was wondering if it possible for WPF to save the radio button content and use the event handler to sent the content to text file.我想知道 WPF 是否可以保存单选按钮内容并使用事件处理程序将内容发送到文本文件。 Do I need to do all that in the event handler or i can invoke some class?我需要在事件处理程序中完成所有这些还是我可以调用某个类?

    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();
        }

Or WPF have a method that can automatically save the content into variable.或者WPF有一个方法可以自动将内容保存到变量中。 Am I missing something?我错过了什么吗?

I wanted the radio button to check the radio button and sent the content into the text file.我希望单选按钮检查单选按钮并将内容发送到文本文件中。

If you just want to save the text displayed with the radio button to a file, then:如果您只想将使用单选按钮显示的文本保存到文件中,则:

private void radioButton1_Checked(object sender, EventArgs e)
{
    string radioButtonContent = ((RadioButton)sender).Text;
    string savePath = @"C:\sent.txt"; // or wherever

    File.WriteAllText(savePath, radioButtonContent);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM