Uma V

Uma V

  • NA
  • 1
  • 2.4k

How to get the checked items of checkboxes created dynamically and write that value to a file in WPF?

Apr 28 2012 10:52 AM
I have written a WPF Application in C# to create checkboxes during runtime as the contents must be obtained from a file. The user selects the items from the checkbox. On the click of a button, all the checked items must be written to a text file. How can it be done? Is the following code correct way to create checkboxes dynamically?

CheckBox chb;
private void radioButton2_Checked(object sender, RoutedEventArgs e)
{
   
//Create file
   
string fp5 = @"D:\List.txt";      

   
FileStream fs = new FileStream(fp5, FileMode.Open, FileAccess.Read);
   
StreamReader reader = new StreamReader(fs);

   
float cby = 135.0F;
   
int ControlIndex=1;

   
while (!reader.EndOfStream)
   
{
       
string line = reader.ReadLine();
        chb
= new CheckBox();
        chb
.Name = "Chk" + ControlIndex;

       
Canvas.SetLeft(chb, 28);
       
Canvas.SetTop(chb, cby);
        chb
.Content = line;
        chb
.IsChecked = false;
        chb
.Foreground = new SolidColorBrush(Colors.Blue);
        myCanvas
.Children.Add(chb);
        cby
= cby + 25.0F;
       
ControlIndex++;
   
}
    fs
.Close();
}

private void button5_Click(object sender, RoutedEventArgs e)
{
   
//Create files
   
string fp6 = @"D:\List2.txt";

   
if (!File.Exists(fp6))
   
File.Create(fp6).Close();

   
/*I want to write the checked items of the checkbox chb to the text file List2.txt.
    I wanted to know how to do this */

}