2012-04-28 2 views
0

나는 C#에서 WPF 응용 프로그램을 작성하여 파일에서 내용을 가져와야하므로 런타임 중에 확인란을 만들 수 있습니다. 사용자는 확인란에서 항목을 선택합니다. 버튼을 클릭하면 모든 체크 된 항목을 텍스트 파일에 작성해야합니다. 어떻게 할 수 있습니까? 다음 코드는 확인란을 동적으로 만들 수 있습니까?동적으로 생성 된 체크 박스의 체크 된 아이템을 얻고 그 값을 WPF의 파일에 쓰는 방법?

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 */ 
} 

답변

2

private void button5_Click(object sender, RoutedEventArgs e) 
{ 
string str=""; 
foreach (UIElement child in canvas.Children) 
{ 
    if(child is CheckBox) 
    if(((CheckBox)child).IsChecked) 
     str+=((CheckBox)child).Content; 

} 
string fp6 = @"D:\List2.txt"; 
File.WriteAllText(fp6,str); 

} 
+0

감사를 할 수있다! 도움이되었다. – Ashwini

관련 문제