In the Form constructor I generate an array of panels (pnl_ in the code below), that include radiobuttons.
When I visualize the Form, only panels are visible, NOT radiobuttons inside.
Curiously, everything is OK, when I generate only one panel (pnl in the code below).
public partial class Radiobuttons_on_Panel : Form
{
private Panel pnl = new Panel();
private RadioButton rbtn1 = new RadioButton();
private RadioButton rbtn2 = new RadioButton();
private Panel[] pnl_ = new Panel[7];
private RadioButton[] rbtn1_ = new RadioButton[7];
private RadioButton[] rbtn2_ = new RadioButton[7];
public Radiobuttons_on_Panel()
{
InitializeComponent();
pnl.Location = new Point(10, 10);
pnl.Size = new Size(100, 100);
pnl.BorderStyle = BorderStyle.FixedSingle;
rbtn1.Location = pnl.Location;
rbtn1.Text = "AAA";
rbtn2.Location = new Point(pnl.Location.X, pnl.Location.Y + rbtn1.Height);
rbtn2.Text = "BBB";
pnl.Controls.Add(rbtn1);
pnl.Controls.Add(rbtn2);
this.Controls.Add(pnl);
for (int i = 0; i < pnl_.Length; i++)
{
pnl_[i] = new Panel();
pnl_[i].BorderStyle = BorderStyle.FixedSingle;
pnl_[i].Size = new Size(100, 100);
pnl_[i].Location = new Point(10 + i * 110, 200);
rbtn1_[i] = new RadioButton();
rbtn1_[i].Location = pnl_[i].Location;
rbtn1_[i].Text = "AAA";
rbtn2_[i] = new RadioButton();
rbtn2_[i].Location = new Point(pnl_[i].Location.X, pnl_[i].Location.Y + rbtn1_[i].Height);
rbtn2_[i].Text = "BBB";
pnl_[i].Controls.Add(rbtn1_[i]);
pnl_[i].Controls.Add(rbtn2_[i]);
this.Controls.Add(pnl_[i]);
}
}
}
Where is the problem?
Your problem with this is that your calculation of location is wrong. The location of the radio buttons are relative to their container - in this case, the panels. If you just set your locations to
rbtn1_[i] = new RadioButton();
rbtn1_[i].Location = new Point(10, 10);
rbtn1_[i].Text = "AAA";
rbtn2_[i] = new RadioButton();
rbtn2_[i].Location = new Point(rbtn1_[i].Location.X, rbtn1_[i].Location.Y + rbtn1_[i].Height);
rbtn2_[i].Text = "BBB";
the buttons will lay out like you want them to.
Thing work out in your first one because your radio button locations values are small enough to be visible in the panel. For those others, well, they are outside the bounds of their parent panels.
The long and short of it is that the Location property is always relative to the container, whether that is a Panel or a Form - the Location values for your panels are relative to the Form but the the Location values for your radio buttons are relative to whatever panel they are in.
MSDN Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.location.aspx
Related
When I resize the following form with the right resize handle, the contained TableLayoutPanel gets decorated with scroll bars (as intended, panel1.AutoScroll = true) for smaller form sizes, but the TableLayoutPanel also gets displaced from its original position. See images below: after resizing the form with right resize handle only, the second one has its scroll bars not leftmost and the left border of the content is cut off.
It seems somehow that this behavior is tied to the existence of the nested RadioButtons because if I remove them (or replace them by another TextBox for example), the "normal" behavior is restored (TableLayoutPanel stays in place during resize).
What properties do I have to set in order to keep the content always stationary relative to the (top)left borders?
BTW: When I replace the panel1 by a TabControl + one TabPage, the "normal" behavior is also restored.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
/// <summary>
/// Description of Form3.
/// </summary>
public partial class Form3 : Form
{
const int textBoxNameWidth = 500;
TableLayoutPanel testControl1;
Panel panel1;
TextBox textBoxName;
RadioButton radioButtonNo;
RadioButton radioButtonYes;
TableLayoutPanel tableLayoutPanelDecision;
public Form3()
{
testControl1 = new TableLayoutPanel();
panel1 = new Panel();
textBoxName = new TextBox();
radioButtonNo = new RadioButton();
radioButtonYes = new RadioButton();
tableLayoutPanelDecision = new TableLayoutPanel();
testControl1.AutoSize = true;
testControl1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
testControl1.Location = new Point(0, 0);
testControl1.Dock = DockStyle.None;
testControl1.ColumnCount = 2;
testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
testControl1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
testControl1.RowCount = 2;
testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
testControl1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
testControl1.Controls.Add(textBoxName, 1, 0);
testControl1.Controls.Add(tableLayoutPanelDecision, 1, 1);
textBoxName.Text = "New Boolean";
textBoxName.TextAlign = HorizontalAlignment.Center;
textBoxName.Anchor = (AnchorStyles.Left | AnchorStyles.Right);
textBoxName.TabStop = false;
textBoxName.Width = textBoxNameWidth;
tableLayoutPanelDecision.AutoSize = true;
tableLayoutPanelDecision.ColumnCount = 2;
tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
tableLayoutPanelDecision.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
tableLayoutPanelDecision.RowCount = 1;
tableLayoutPanelDecision.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanelDecision.Controls.Add(radioButtonYes, 0, 0);
tableLayoutPanelDecision.Controls.Add(radioButtonNo, 1, 0);
tableLayoutPanelDecision.Dock = DockStyle.Fill;
radioButtonNo.Checked = true;
radioButtonNo.AutoSize = true;
radioButtonNo.TabIndex = 1;
radioButtonNo.TabStop = true;
radioButtonNo.Text = "False";
radioButtonNo.UseVisualStyleBackColor = true;
radioButtonNo.Anchor = AnchorStyles.None;
radioButtonYes.AutoSize = true;
radioButtonYes.TabIndex = 0;
radioButtonYes.Text = "True";
radioButtonYes.UseVisualStyleBackColor = true;
radioButtonYes.Anchor = AnchorStyles.None;
panel1.AutoScroll = true;
panel1.Controls.Add(testControl1);
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel1.Location = new System.Drawing.Point(0, 0);
panel1.Name = "panel1";
panel1.Size = new System.Drawing.Size(560, 219);
panel1.TabIndex = 1;
AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
ClientSize = new System.Drawing.Size(560, 219);
Controls.Add(panel1);
Name = "Form3";
Text = "Form3";
}
}
}
The panel is trying to keep focusable controls within view for the user. To change that, you would have to use your own panel:
public class PanelEx : Panel {
protected override Point ScrollToControl(Control activeControl) {
return this.DisplayRectangle.Location;
}
}
Using Windows Forms, two link labels are created dynamically. When the user clicks on anyone of links labels, one dynamic form is created. In that form I created one data grid, a text box and a button placed dynamically (in that dynamic form). Now I want to access the dynamic data grid in the dynamic button click event. How can I do that?
private void Users_Load(object sender, EventArgs e)
{
da = new SqlDataAdapter("Usp_Get_Employees", con);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string somecode = i.ToString() + ds.Tables[0].Rows[i]["eid"].ToString();
LinkLabel lbluser = new LinkLabel();
lbluser.Name = ds.Tables[0].Rows[i]["eid"].ToString();
lbluser.Text = ds.Tables[0].Rows[i]["ename"].ToString();
lbluser.Location = new System.Drawing.Point(40, i * 40);
lbluser.Size = new System.Drawing.Size(50, 30);
Controls.Add(lbluser);
lbluser.Click += new EventHandler(lbluser_Click);
}
}
}
void lbluser_Click(object sender, EventArgs e)
{
LinkLabel lnkClis = (LinkLabel)sender;
Form frm = new Form();
frm.Name = lnkClis.Name;
frm.Text = lnkClis.Text;
frm.Show();
DataGrid dtgrd = new DataGrid();
dtgrd.Location = new System.Drawing.Point(10, 1 * 40);
dtgrd.Name = lnkClis.Name;
names = lnkClis.Name;
TextBox tx = new TextBox();
tx.Location = new System.Drawing.Point(10, 5 * 40);
tx.Size = new Size(80, 30);
tx.Multiline = true;
tx.LostFocus += new EventHandler(tx_LostFocus);
Button btn = new Button();
btn.Location = new System.Drawing.Point(10, 7 * 40);
btn.Size = new System.Drawing.Size(50, 30);
btn.Name = lnkClis.Name;
btn.Click += new EventHandler(btn_Click);
frm.Controls.Add(dtgrd);
frm.Controls.Add(tx);
frm.Controls.Add(btn);
}
// Now I am trying to access the data grid in the btn_click event
void btn_Click(object sender, EventArgs e)
{
Button btsave = (Button)sender;
string eid = btsave.Name;
object grd = btsave.Parent.Controls.Find("dtgrd", true).FirstOrDefault();
((DataGrid)grd).DataSource = ds.Tables[0];
}
Now I am getting an error object set of instances of an object at:
((DataGrid)grd).DataSource = ds.Tables[0];
The exception message you have written:
Now I am getting an error object set of instances of an object at
makes no sense, but it looks like
Object reference not set to an instance of an object
If this is the case, I think the error lays in Find method call. According to documentation:
Searches for controls by their Name property and builds an array of all the controls that match.
In your button click handler you assume that grid is called dtgrd, but while you create a grid you name it like this:
dtgrd.Name = lnkClis.Name;
it will suffice if you change this line to:
dtgrd.Name = "dtgrd";
Having said that, you should consider using an anonymous method for the button click handler. It will eliminate need for calling the Find method in the first place.
void lbluser_Click(object sender, EventArgs e)
{
//...
DataGrid dtgrd = new DataGrid();
//...
Button btn = new Button();
//...
btn.Click += (sender,args)=> dtgrd.DataSource = ds.Tables[0];
Try the following code
public Form1()
{
Form f1 = new Form();
f1.Text = "New Form";
TextBox t1 = new TextBox();
t1.Top = 0;
t1.Name = "t1";
t1.Visible = true;
f1.Controls.Add(t1);
Button b1 = new Button();
b1.Top = 30;
b1.Name = "b1";
b1.Text = "Click";
b1.Click += b1_Click;
f1.Controls.Add(b1);
f1.Show();
}
public void b1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
object txt = btn.Parent.Controls.Find("t1", false).First();
((TextBox)txt).Text = "Hi, you have clicked me.";
}
I modified Nitesh's code a bit. Just capture the textbox in the click handler using a lambda:
public Form1()
{
Form f1 = new Form();
f1.Text = "New Form";
TextBox t1 = new TextBox();
t1.Top = 0;
t1.Name = "t1";
t1.Visible = true;
f1.Controls.Add(t1);
Button b1 = new Button();
b1.Top = 30;
b1.Name = "b1";
b1.Text = "Click";
b1.Click += (sender, args) => MessageBox.Show("The text is: " + t1.Text);
f1.Controls.Add(b1);
f1.Show();
}
The error you are getting is from the statement (as the grd object is null):
((DataGrid)grd).DataSource = ds.Tables[0];
Since you are trying to catch hold of a dynamic control, it's good have a proper null checks, type checks, and error handling. Something like this:
if(grd != null && grd is DataGrid)
((DataGrid)grd).DataSource = ds.Tables[0];
I added a Line with C# code to my canvas, along with a context menu and attached event. I would like to rotate the Line using a context menu choice, not the menu text in the context menu:
newMenuItem1.PreviewMouseDown += new MouseButtonEventHandler((sx, ex) => {
MenuItem menuItem = (MenuItem)sx;
string theHeader = menuItem.Header.ToString();
if (theHeader.Contains("90")) {
Line ow = ex.Source as Line;
rt = new RotateTransform(90, 25, 50);
ow.RenderTransform = rt;
}
});
This code produces a null reference exception. If I substitute:
UIElement ow = ex.Source as UIElement;
The actual menu text will rotate!
Edit:
Here is more code, I am now trying originalsource too:
private void button1_Click(object sender, RoutedEventArgs e)
{
Line g = new Line();
g.Stroke = System.Windows.Media.Brushes.LawnGreen;
g.X1 = 0; g.X2 = 100;g.Y1 = 0;g.Y2 = 0;
g.HorizontalAlignment = HorizontalAlignment.Left;
g.VerticalAlignment = VerticalAlignment.Center;
g.StrokeThickness = 6;
ContextMenu k = new ContextMenu();
g.ContextMenu = k;
MenuItem newMenuItem1 = new MenuItem();
MenuItem newMenuItem2 = new MenuItem();
MenuItem newMenuItem3 = new MenuItem();
newMenuItem1.Header = "Rotate 90";
newMenuItem2.Header = "Rotate 180";
newMenuItem3.Header = "Rotate 270";
newMenuItem1.PreviewMouseDown += new MouseButtonEventHandler((sx, ex) => {
MenuItem menuItem = (MenuItem)sx;
string theHeader = menuItem.Header.ToString();
if (theHeader.Contains("90")) {
Line ow = (Line)ex.OriginalSource;
rt = new RotateTransform(90, 25, 50);
ow.RenderTransform = rt;
}
});
g.ContextMenu.Items.Add(newMenuItem1);
g.ContextMenu.Items.Add(newMenuItem2);
g.ContextMenu.Items.Add(newMenuItem3);
Canvas.SetTop(g, 18);
Canvas.SetLeft(g, 18);
MyCanvas.Children.Add(g);
///////
I also tried:
private static T FindAncestor<T>(DependencyObject current)
where T : DependencyObject
{
do
{
if (current is T)
{
return (T)current;
}
current = VisualTreeHelper.GetParent(current);
}
while (current != null);
return null;
}
but it does not work. My next plan is to get coordinates off the canvas, and try to determine what control sits there. This will become tricky though if an object is transformed, because I believe the UI sees it at the original position. I've experimented with other controls as well, like the TextBox and get similar issues.
A really quick and dirty way to do this would be to add your line to the menu item's tag property and retrieve it in the PreviewMouseDown handler
When creating your context menu:
newMenuItem1.Tag = g;
In you handler:
Line ow = ((FrameworkElement)ex.Source).Tag as Line;
The less quick and dirty way to do this would be to use the ContextMenuOpening event on your line as that should be sent with the sender equal to the control itself. You could then store a reference to the line somewhere and retrieve it again in the menu item click event. This works better when you have multiple lines (which I'm guess is what you intend) and just one context menu (instead of producing a bunch of copies of the same menu as you are doing now).
i want to print a grid which is generated dynamically.
Means, in the click event of the Print Button, i m generating a grid and then i want to print that grid.
here is my code,
private void btnPrint_Click(object sender, RoutedEventArgs e)
{
PrintDialog Objprint = new System.Windows.Controls.PrintDialog();
if (Objprint.ShowDialog() == true)
{
System.Printing.PrintCapabilities capabilities = Objprint.PrintQueue.GetPrintCapabilities(Objprint.PrintTicket);
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);
#region "Make a grid For Printing"
Grid objgrid = new Grid();
objgrid.Name = "GridForPrinting";
objgrid.Width = 1000;
objgrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
objgrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
objgrid.RowDefinitions.Add(new RowDefinition());
TextBlock objtext = new TextBlock();
objtext.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
objtext.VerticalAlignment = System.Windows.VerticalAlignment.Center;
objtext.Text = "SUPERIOR COURT OF CALIF COUNTY OF SAN BERNARDINO";
Grid.SetRow(objtext, 0);
objgrid.Children.Add(objtext);
#endregion
Objprint.PrintVisual(objgrid, "Case Summary");
}
}
this code give me blank page to print.
how can i do that?
here i get answer,
from the below code i get what i want to do...
void PrintOnClick(object sender, RoutedEventArgs args)
{
PrintDialog dlg = new PrintDialog();
if ((bool)dlg.ShowDialog().GetValueOrDefault())
{
// Create Grid panel.
Grid grid = new Grid();
// Define 5 auto-sized rows and columns.
for (int i = 0; i < 5; i++)
{
ColumnDefinition coldef = new ColumnDefinition();
coldef.Width = GridLength.Auto;
grid.ColumnDefinitions.Add(coldef);
RowDefinition rowdef = new RowDefinition();
rowdef.Height = GridLength.Auto;
grid.RowDefinitions.Add(rowdef);
}
// Give the Grid a gradient brush.
grid.Background =
new LinearGradientBrush(Colors.Black, Colors.White,
new Point(0, 0), new Point(1, 1));
// Every program needs some randomness.
Random rand = new Random();
// Fill the Grid with 25 buttons.
for (int i = 0; i < 25; i++)
{
Button btn = new Button();
btn.FontSize = 12 + rand.Next(8);
btn.Content = "Button No. " + (i + 1);
btn.HorizontalAlignment = HorizontalAlignment.Center;
btn.VerticalAlignment = VerticalAlignment.Center;
btn.Margin = new Thickness(6);
grid.Children.Add(btn);
Grid.SetRow(btn, i % 5);
Grid.SetColumn(btn, i / 5);
}
// Size the Grid.
grid.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
Size sizeGrid = grid.DesiredSize;
// Determine point for centering Grid on page.
Point ptGrid =
new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
(dlg.PrintableAreaHeight - sizeGrid.Height) / 2);
// Layout pass.
grid.Arrange(new Rect(ptGrid, sizeGrid));
// Now print it.
dlg.PrintVisual(grid, Title);
}
}
The PrintVisual print a Visual object. That means, by using the PrintVisual method, we can print any control, container, Window or user control that is in the visualtree.You cannot print a control that is not in the visualtree
i create a dynamic button(xmlparsingbuildbutton) during a buttonclick(xmlparsingbutton) event.
I did a xml parsing...once each xml target is parsed, they will create an individual checkbox. What i need is this: Create a dynamic button event(xmlparsingbuildbutton). During this dynamic button event, for every checkbox that is checked, they will append a text file by writing new lines into the text file. This gets confusing to me because right now i have a foreachloop embedded in the xmlparsingbutton event that creates all these checkboxes.
Right now, I've coded it in such a way that whenever i check the checkboxes, my append of file event will not fire therefore I created this "xmlparsingbuildbutton" to help fire the event.From my understanding, usually i would hard code buttonclickevent, however in this case the target value always change in the foreach loop and therefore it is not right to just merely hardcode the buttonclickevent.
So my question is how do i have this button event to be inside the foreach loop of the xmlparsing event button? Please clarify my doubts. Many thanks!
Here is my code:
private void xmlparsingButton_Click(object sender, RoutedEventArgs e)
{
XDocument xmlDoc = XDocument.Load(#"C:\Build.xml");
var abc = from target in xmlDoc.Descendants("target")
select (string)target.Attribute("if");
ColumnDefinition gridCol1 = new ColumnDefinition();
gridCol1.Width = new GridLength(300);
ColumnDefinition gridCol2 = new ColumnDefinition();
gridCol2.Width = new GridLength(300);
ColumnDefinition gridCol3 = new ColumnDefinition();
gridCol3.Width = new GridLength(300);
ColumnDefinition gridCol4 = new ColumnDefinition();
gridCol4.Width = new GridLength(300);
tab4grid.ColumnDefinitions.Add(gridCol1);
tab4grid.ColumnDefinitions.Add(gridCol2);
tab4grid.ColumnDefinitions.Add(gridCol3);
tab4grid.ColumnDefinitions.Add(gridCol4);
RowDefinition gridRow1 = new RowDefinition();
gridRow1.Height = new GridLength(50);
RowDefinition gridRow2 = new RowDefinition();
gridRow2.Height = new GridLength(50);
RowDefinition gridRow3 = new RowDefinition();
gridRow3.Height = new GridLength(50);
RowDefinition gridRow4 = new RowDefinition();
gridRow4.Height = new GridLength(50);
RowDefinition gridRow5 = new RowDefinition();
gridRow5.Height = new GridLength(50);
RowDefinition gridRow6 = new RowDefinition();
gridRow6.Height = new GridLength(50);
RowDefinition gridRow7 = new RowDefinition();
gridRow7.Height = new GridLength(50);
RowDefinition gridRow8 = new RowDefinition();
gridRow8.Height = new GridLength(50);
RowDefinition gridRow9 = new RowDefinition();
gridRow9.Height = new GridLength(50);
RowDefinition gridRow10 = new RowDefinition();
gridRow10.Height = new GridLength(50);
RowDefinition gridRow11 = new RowDefinition();
gridRow11.Height = new GridLength(50);
RowDefinition gridRow12 = new RowDefinition();
gridRow12.Height = new GridLength(50);
tab4grid.RowDefinitions.Add(gridRow1);
tab4grid.RowDefinitions.Add(gridRow2);
tab4grid.RowDefinitions.Add(gridRow3);
tab4grid.RowDefinitions.Add(gridRow4);
tab4grid.RowDefinitions.Add(gridRow5);
tab4grid.RowDefinitions.Add(gridRow6);
tab4grid.RowDefinitions.Add(gridRow7);
tab4grid.RowDefinitions.Add(gridRow8);
tab4grid.RowDefinitions.Add(gridRow9);
tab4grid.RowDefinitions.Add(gridRow10);
tab4grid.RowDefinitions.Add(gridRow11);
tab4grid.RowDefinitions.Add(gridRow12);
int i = 0;
int j = 1;
System.Windows.Controls.Button XmlparsingbuildButton = new System.Windows.Controls.Button();
Grid.SetColumn(XmlparsingbuildButton, 4);
Grid.SetRow(XmlparsingbuildButton, 12);
XmlparsingbuildButton.Height = 23;
XmlparsingbuildButton.Width = 51;
XmlparsingbuildButton.Content = "Build";
tab4grid.Children.Add(XmlparsingbuildButton);
foreach(string target in abc)
{
if (target != null && !Dictionarycheck.ContainsKey(target))
{
System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox();
chk.Name = target+"CheckBox";
chk.Content = target;
Grid.SetColumn(chk, i); //sets column
Grid.SetRow(chk, j); //sets row
tab4grid.Children.Add(chk); //adds the control
Tabitem5.Visibility = Visibility.Visible;
i++;
if (i == 4)
{
j++;
i = 0;
}
if (chk.IsChecked == true)
{
using (var writer = File.AppendText(#"c:\testing.txt"))
{
writer.WriteLine(target);
}
}
}
}
Tabitem5.Visibility = Visibility.Visible;
Tabcontrol1.SelectedIndex = 4;
}
I have to admit, I have problems understanding your problem. If I get it right, you're trying to do the following:
Parse a Xml document ("C:\Build.xml") with (roughly) this format:
targets>
target if="aaa" />
target if="bbb" />
/targets>
Create a CheckBox for each target tag with if- attribute set
Check the CheckBox containing the string you want to use
Click a button to append the checked strings to a text file ("c:\testing.txt")
If this is correct, then I think your solution won't do that. I not sure if I can understand what you are trying to do in your code. The way you set up the comboboxes they will never be checked the time you use your writer.
How about this:
Use a StackPanel (stackPanel1) instead of a grid. That way you don't have to create all the row definitions. (Ok, I have to admit, I don't know what all these columns are for)
Create a simple logic for a button to append the checked ComboBoxes to the file
code:
private void xmlparsingButton_Click(object sender, RoutedEventArgs e)
{
stackPanel1.Children.Clear();
XDocument xmlDoc = XDocument.Load(#"C:\Build.xml");
var abc = from target in xmlDoc.Descendants("target")
select (string) target.Attribute("if");
foreach (string target in abc)
{
if (target != null)
{
System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox();
chk.Name = target + "CheckBox";
chk.Content = target;
chk.Tag = target;
stackPanel1.Children.Add(chk);
}
}
}
private void btnAppendToTextFile_Click(object sender, RoutedEventArgs e)
{
using (var writer = File.AppendText(#"c:\testing.txt"))
{
foreach (var child in stackPanel1.Children)
{
if ((child is CheckBox) && ((CheckBox) child).IsChecked.Value)
{
writer.WriteLine(((CheckBox)child).Tag);
}
}
}
}
I hope this helps you with your problem.