简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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