Here is my code to get values from XML file:
foreach (XmlNode node in DOC.SelectNodes("//CheckMarkObject"))
{
FbCheckMark checkmark = new FbCheckMark();
checkmark.Name = node.SelectSingleNode("Name").InnerText;
checkmark.Label = node.SelectSingleNode("Label").InnerText;
if (node.SelectSingleNode("IsChecked").InnerText == "0")
{
checkmark.IsChecked = false;
}
else
{
checkmark.IsChecked = true;
}
listCheckMarks.Add(checkmark);
}
Now the code to create control at runtime:
for (int i = 0; i < listCheckMarks.Count; i++)
{
if (listCheckMarks[i].checkMark.Equals(checkMark))
{
CheckBox cb = new CheckBox();
TextBlock cbtextblock = new TextBlock();
cbtextblock.Text = listCheckMarks[i].Label;
cbtextblock.Height = 27;
cbtextblock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cbtextblock.Margin = new Thickness(12, 20, 0, 0);
cbtextblock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cb.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.Margin = new Thickness(150, 21, 0, 0);
cb.Height = 50;
cb.Width = 100;
cb.Name = listCheckMarks[i].Name;
LayoutRoot.Children.Add(cbtextblock);
LayoutRoot.Children.Add(cb);
}
}
when i made change in my XML file i.e Create "CheckMark" tag two times. the result control overwrite on previous one. i want to arrange the new control under the previous one.
kindly suggest me what can i do ? use linear layout like in android?
thanks
Try to insert elements into StackPanel and set Orientation property for it.
Try that example:
StackPanel container = new StackPanel();
LayoutRoot.Children.Add(container);
for (int i = 0; i < listCheckMarks.Count; i++)
{
if (listCheckMarks[i].checkMark.Equals(checkMark))
{
StackPanel childContainer = new StackPanel();
childContainer.Orientation = Orientation.Horizontal;
CheckBox cb = new CheckBox();
TextBlock cbtextblock = new TextBlock();
cbtextblock.Text = listCheckMarks[i].Label;
cbtextblock.Height = 27;
cbtextblock.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cbtextblock.Margin = new Thickness(12, 20, 0, 0);
cbtextblock.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
cb.VerticalAlignment = System.Windows.VerticalAlignment.Top;
cb.Margin = new Thickness(150, 21, 0, 0);
cb.Height = 50;
cb.Width = 100;
cb.Name = listCheckMarks[i].Name;
childContainer.Children.Add(cbtextblock);
childContainer.Children.Add(cb);
container.Children.Add(childContainer);
}
}
Related
I'm trying assign materialDesign:PackIcon code behind. Here's my code.
for (int i = 0; i<PrinterNumber; i++)
{
Button btn_Restart = new Button();
btn_Restart.Name = string.Format("PrinterRestartButtton{0}", i);
btn_Restart.Width = 50;
btn_Restart.Height = 25;
btn_Restart.VerticalAlignment = VerticalAlignment.Center;
btn_Restart.HorizontalAlignment = HorizontalAlignment.Left;
btn_Restart.Content = "Restart";
//btn_Restart.Visibility = Visibility.Hidden;
btn_Restart.Click += Btn_Restart_Click;
btn_Restart.Style = (Style) Application.Current.TryFindResource("MaterialDesignRaisedDarkButton");
Grid.SetRow(btn_Restart, i + 2);
Grid.SetColumn(btn_Restart, 10);
grd_WorkArea.Children.Add(btn_Restart);
}
How to apply materialDesign:PackIcon Kind="Restart" to
btn_Restart.Content property?
Set the Content property to a PackIcon:
btn_Restart.Content = new MaterialDesignThemes.Wpf.PackIcon
{ Kind = MaterialDesignThemes.Wpf.PackIconKind.Restart };
I have a test, and in Form1_Load I fill up this test with questions and answers. But it shows only first question. What's the matter?
int loc = 20;
for (int i = 0; i < 5; i++)
{
GroupBox gb = new GroupBox();
gb.Size = new Size(500, 200);
gb.Location = new Point(40, loc);
gb.BackColor = System.Drawing.Color.Aquamarine;
Label q_text = new Label(); // текст питання
q_text.Text = "Питання" + (i + 1);
q_text.Font = new Font("Aria", 10, FontStyle.Bold);
q_text.Location = new Point(gb.Location.X, gb.Location.Y);
gb.Controls.Add(q_text);
int iter = q_text.Location.Y + 30;
foreach (string key in questions[i].answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
this.Controls.Add(gb);
loc += 300;
}
After examining your code, there are a few possible issues stemming from q_text
int loc = 20;
for (int i = 0; i < 5; i++)
{
GroupBox gb = new GroupBox();
gb.Size = new Size(500, 200);
gb.Location = new Point(40, loc);
gb.BackColor = System.Drawing.Color.Aquamarine;
Label q_text = new Label(); // текст питання
q_text.Text = "Питання" + (i + 1);
q_text.Font = new Font("Aria", 10, FontStyle.Bold);
q_text.Location = new Point(0, 0);
gb.Controls.Add(q_text);
int iter = q_text.Location.Y + 30;
foreach (string key in questions[i].answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
this.Controls.Add(gb);
loc += 300;
}
The only changes here are q_text getting set to 0,0.
An additional change I would make however is to adjust hight by q_text's height:
int iter = q_text.Location.Y + q_text_Size.Height + 5;
Just in case the label was long and wrapped text.
I have a pointing to null error but i dont see where the problem is since everything gets initalised before used. the points where the error acures i have given a blockquote. Like always im thankfull for every help.
Button topAddBut = null;
//Button botAddBut = null;
Button loadBut = null;
Button saveBut = null;
StackPanel topSP = null;
//StackPanel botSP = null;
public MainWindow()
{
InitializeComponent();
loadBut = new Button { Content = "Load", Width = 70, Height = 23 };
Canvas.SetRight(loadBut, 160);
Canvas.SetBottom(loadBut, 24);
canvas1.Children.Add(loadBut);
saveBut = new Button { Content = "Save", Width = 70, Height = 23 };
Canvas.SetRight(saveBut, 80);
Canvas.SetBottom(saveBut, 24);
canvas1.Children.Add(saveBut);
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
Canvas.SetLeft(topSP, 160);
Canvas.SetTop(topSP, 100);
AddWrapPanelTop();
AddTextBoxTop();
AddTopButton();
}
void AddTextBoxTop()
{
TextBox txtB1 = new TextBox();
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
}
void AddWrapPanelTop()
{
WrapPanel myWrapPanel = new WrapPanel();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 0, 0, 255);
myWrapPanel.Background = System.Windows.Media.Brushes.Magenta;
myWrapPanel.Orientation = Orientation.Horizontal;
myWrapPanel.Width = 4000;
myWrapPanel.HorizontalAlignment = HorizontalAlignment.Left;
myWrapPanel.VerticalAlignment = VerticalAlignment.Top;
topSP.Children.Add(myWrapPanel);
}
void AddTopButton()
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
topAddBut = new Button();
topAddBut.Click += new RoutedEventHandler(this.TopClick);
topAddBut.Content = "Add";
topAddBut.Width = 75;
// Add the buttons to the parent WrapPanel using the Children.Add method.
WrapPanel wp = (WrapPanel)topSP.Children[0];
wp.Children.Add(txtB1);
wp.Children.Add(loadBut);
this.topSP.Children.Add(wp);
}
void TopClick(Object sender, EventArgs e)
{
TextBox txtB1 = new TextBox();
txtB1.Background = System.Windows.Media.Brushes.Magenta;
txtB1.Text = "Text";
txtB1.Width = 75;
txtB1.Height = 75;
Button s = (Button)sender;
WrapPanel wp = (WrapPanel)s.Parent;
wp.Children.Remove(s);
wp.Children.Add(txtB1);
AddTopButton();
// Add the buttons to the parent WrapPanel using the Children.Add method.
}
}
}
You define the following:
StackPanel topSP = null;
Then you have
StackPanel topSP = new StackPanel { Width = 400, Height = 50 };
This is in your local scope though so will be destroyed when you exit the function
Finally you have
topSP.Children.Add(myWrapPanel);
This will still be set to null which is why your error occurs. Basically you are creating a local version of the same variable.
In order to resolve simply change the second definition to:
topSP = new StackPanel { Width = 400, Height = 50 };
I am trying to add different objects to a GroupBox. When I click 'Add Title' button I can get the Combobox and Textbox to appear in the Groupbox.
What I want to now happen is when they click 'Add question' I want to be able to add the questions objects (Combobox, Textbox) to the same Groupbox as the last added title.
C# CODE:
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
gp.Content = sp;
spStandard.Children.Add(gp);
}
private void ViewQuestions(StackPanel sp)
{
gp.Content = sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
ViewQuestions(sp);
}
I have tried to have an attempted but when I click 'Add Question' it just overwrites the Title's objects.
Any help would be amazing.
I am using WPF, and these are created at runtime.
Thanks.
EDIT4:
I have had to move
StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
GroupBox gp;
Just below the
public partial class CreateNewStandard : Page
{
So it is visible to all the methods. Then I get this error.
http://i.stack.imgur.com/f9uqF.png
Try this
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
CurrentSortItem++;
SortItems.Add(CurrentSortItem);
StackPanel outerSp = new StackPanel() { Orientation = Orientation.Vertical };
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
gp = new GroupBox();
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem;
y.SelectedItem = CurrentSortItem;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Title" + CurrentSortItem;
x.Text = "Title...";
x.FontWeight = FontWeights.Bold;
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 200;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.Margin = new Thickness(12, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
outerSp.Children.Add(sp);
gp.Content = outerSp;
spStandard.Children.Add(gp);
}
private void ViewQuestions(StackPanel sp)
{
var stackPanel=gp.Content as StackPanel;
if(stackPanel!=null)
{
stackPanel.Children.Add(sp);
}
else
gp.Content=sp;
}
List<int> SortItems1 = new List<int>();
int CurrentSortItem1 = 0;
int Count = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
if (SortItems.Count == 0)
{
MessageBox.Show("You must add a title before adding a question", "ERROR", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
Count++;
CurrentSortItem1++;
SortItems1.Add(CurrentSortItem1);
ComboBox y = new ComboBox();
y.Name = "Combo" + CurrentSortItem1;
y.SelectedItem = CurrentSortItem1;
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.Margin = new Thickness(20, 15, 0, 0);
foreach (int item in SortItems1)
{
y.Items.Add(item);
}
TextBox x = new TextBox();
x.Name = "Question" + CurrentSortItem1;
x.Text = "Question...";
x.FontStyle = FontStyles.Italic;
x.TextWrapping = TextWrapping.Wrap;
x.Height = 25;
x.Width = 500;
x.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
x.AcceptsReturn = true;
x.Margin = new Thickness(100, 15, 0, 0);
TextBox z = new TextBox();
z.Name = "Points" + CurrentSortItem;
z.FontWeight = FontWeights.Bold;
z.Height = 25;
z.Width = 45;
z.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
z.Margin = new Thickness(250, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
sp.Children.Add(z);
outerSp.Children.Add(sp);
ViewQuestions(sp);
}
You are overwriting it .Though you need to add them to the already existing stackPanel. The code is not tested I am trying to give you an idea .I hope this will help.
I have a WPF form that I have created programatically. It is basically a list of items with 2 DatePicker objects to specify a range of dates. I have everything working for the most part, but I need to fire some logic on the SelectedDateChanged event.
The problem is, since the DatePicker's are generated dynamically based on selections from a previous form, I need to use an anonymous listener (or whatever you call them in .NET). I have been unable to do it the way I know how and I cant seem to find any example or help through google either. Thanks in advance for any tips.
Generating DatePickers:
public SystemInterval(Role role)
{
this.role = role;
InitializeComponent();
lbls = new Label[role.RoleSystems.Length];
dp = new DatePicker[role.RoleSystems.Length * 2];
cks = new CheckBox[role.RoleSystems.Length];
ScrollViewer sv = new ScrollViewer();
sv.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
sv.VerticalAlignment = System.Windows.VerticalAlignment.Top;
sv.Margin = new Thickness(0,50,0,40);
Grid g = new Grid();
g.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
g.VerticalAlignment = System.Windows.VerticalAlignment.Top;
ColumnDefinition col1 = new ColumnDefinition();
col1.Width = new GridLength(46);
ColumnDefinition col2 = new ColumnDefinition();
col2.Width = GridLength.Auto;
ColumnDefinition col3 = new ColumnDefinition();
col3.Width = new GridLength(150);
ColumnDefinition col4 = new ColumnDefinition();
col4.Width = new GridLength(150);
g.ColumnDefinitions.Add(col1);
g.ColumnDefinitions.Add(col2);
g.ColumnDefinitions.Add(col3);
g.ColumnDefinitions.Add(col4);
sv.Content = g;
LayoutRoot.Children.Add(sv);
for (int r = 0; r < cks.Length; r++)
{
g.RowDefinitions.Add(new RowDefinition());
}
g.Height = (lbls.Length * 25) + (lbls.Length * 5);
for (int i = 0, j = 0; i < cks.Length; i++, j+=2)
{
cks[i] = new CheckBox();
cks[i].IsChecked = false;
cks[i].VerticalAlignment = System.Windows.VerticalAlignment.Center;
cks[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
Grid.SetColumn(cks[i], 0);
Grid.SetRow(cks[i], i);
g.Children.Add(cks[i]);
lbls[i] = new Label();
lbls[i].Height = 25;
lbls[i].Content = role.RoleSystems[i].SystemName;
lbls[i].VerticalAlignment = System.Windows.VerticalAlignment.Center;
lbls[i].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
Grid.SetColumn(lbls[i], 1);
Grid.SetRow(lbls[i], i);
g.Children.Add(lbls[i]);
dp[j] = new DatePicker();
dp[j].Height = 25;
dp[j].Width = 115;
dp[j].VerticalAlignment = System.Windows.VerticalAlignment.Center;
dp[j].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
dp[j].IsEnabled = false;
dp[j].BlackoutDates.Add(new CalendarDateRange(new DateTime(0001, 1, 1), DateTime.Now.Subtract(TimeSpan.FromDays(1))));
Grid.SetColumn(dp[j], 2);
Grid.SetRow(dp[j], i);
g.Children.Add(dp[j]);
dp[j + 1] = new DatePicker();
dp[j + 1].Height = 25;
dp[j + 1].Width = 115;
dp[j + 1].VerticalAlignment = System.Windows.VerticalAlignment.Center;
dp[j + 1].HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
dp[j + 1].IsEnabled = false;
dp[j + 1].BlackoutDates.Add(new CalendarDateRange(new DateTime(0001, 1, 1), DateTime.Now));
Grid.SetColumn(dp[j + 1], 3);
Grid.SetRow(dp[j + 1], i);
g.Children.Add(dp[j + 1]);
cks[i].Click += new RoutedEventHandler(delegate(object s, RoutedEventArgs re)
{
CheckBox cb = (CheckBox)re.Source;
for (int r = 0; r < cks.Length; r++)
{
if (cks[r].Equals(cb))
{
dp[r * 2].IsEnabled = true;
dp[r * 2 + 1].IsEnabled = true;
}
}
});
}
The checkboxs have an anonymous event handler, however this does not work for the DatePicker.SelectedDateChanged event.
Do you mean an event handler using an anonymous method like this?
var dp = new DatePicker();
dp.SelectedDateChanged +=
(sender, args) =>
{
var picker = (DatePicker) sender;
/* do stuff here */
};