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.
Related
I'm trying to make a form where the user can add group boxes with other controls inside.
groupboxes and other controls are all created dynamically.
Program image. *the button "Adicionar Pergunta" adds a new group box
Inside the groupbox is a combobox where the user can choose the preset of controls inside the groupbox.
each preset have different controls or controls in different locations of the group box.
my problem is that when the user change the preset of one groupbox all the other combobox stop working.
Here is the my script:
Variables:
int groupboxcount = 2;
RadioButton rb1 = new RadioButton();
RadioButton rb2 = new RadioButton();
TextBox tx1 = new TextBox();
TextBox tx2 = new TextBox();
GroupBox gb;
ComboBox cb;
RadioButton rb_1;
RadioButton rb_2;
RadioButton rb_V;
RadioButton rb_F;
TextBox tx_1;
TextBox tx_2;
Button Click:
{
gb = new GroupBox();
NumericUpDown Nud1 = new NumericUpDown();
cb = new ComboBox();
cb.SelectedIndexChanged += new EventHandler(cb2_selectec_index_changed);
RichTextBox RichTextBox2 = new RichTextBox();
Label labelA1 = new Label();
Label labelA2 = new Label();
Label labelA3 = new Label();
gb.Name = "groupBox" + groupboxcount;
gb.Text = "Pergunta ";
gb.Height = 182;
gb.Width = 520;
gb.Location = new Point(groupBox1.Location.X, gb.Location.Y + gb.Height + 125);
Nud1.Height = 20;
Nud1.Width = 41;
Nud1.Location = new Point(6, 37);
Nud1.Value = groupboxcount;
cb.Items.Add("Enunciado");
cb.Items.Add("Pergunta normal");
cb.Items.Add("Escolha multipla");
cb.Items.Add("Verdadeiro/Falso");
cb.Height = 21;
cb.Width = 121;
cb.Location = new Point(53, 36);
RichTextBox2.Height = 80;
RichTextBox2.Width = 249;
RichTextBox2.Location = new Point(211, 37);
labelA1.Text = "Nº";
labelA2.Text = "Tipo de pergunta";
labelA3.Text = "Pergunta";
labelA1.Location = new Point(6, 19);
labelA2.Location = new Point(59, 19);
labelA3.Location = new Point(208, 19);
labelA1.Width = 20;
this.Controls.Add(gb);
gb.Controls.Add(Nud1);
gb.Controls.Add(cb);
gb.Controls.Add(RichTextBox2);
gb.Controls.Add(labelA1);
gb.Controls.Add(labelA2);
gb.Controls.Add(labelA3);
button1.Location = new Point(button1.Location.X, gb.Location.Y + button1.Height + 200);
groupboxcount++;
}
Combobox cb2 selected index changed:
{
if (cb.SelectedIndex == 0)
{
//Apenas mostra enunciado
}
if (cb.SelectedIndex == 1)
{
//recebe resposta
}
if (cb.SelectedIndex == 2)
{
rb_1 = new RadioButton();
rb_2 = new RadioButton();
tx_1 = new TextBox();
tx_2 = new TextBox();
rb_1.Text = "opção 1";
rb_1.Location = new Point(120, 75);
rb_2.Text = "opção 2";
rb_2.Location = new Point(120, 100);
tx_2.Location = new Point(5, 100);
tx_1.Location = new Point(5, 75);
this.gb.Controls.Add(rb_1);
this.gb.Controls.Add(rb_2);
this.gb.Controls.Add(tx_1);
this.gb.Controls.Add(tx_2);
}
else
{
this.gb.Controls.Remove(rb_1);
this.gb.Controls.Remove(rb_2);
this.gb.Controls.Remove(tx_1);
this.gb.Controls.Remove(tx_2);
}
if (cb.SelectedIndex == 3)
{
rb_V = new RadioButton();
rb_F = new RadioButton();
rb_V.Text = "Verdadeiro";
rb_V.Location = new Point(120, 75);
rb_F.Text = "Falso";
rb_F.Location = new Point(120, 100);
this.gb.Controls.Add(rb_V);
this.gb.Controls.Add(rb_F);
}
else
{
this.gb.Controls.Remove(rb_V);
this.gb.Controls.Remove(rb_F);
}
}
I would appreciate if you could help me.
I am doing C# application, and I want to change the style of a message box. Is it possible or not?
Example: change button style, fore color, etc.
You can't restyle the default MessageBox as that's dependant on the current Windows OS theme, however you can easily create your own MessageBox. Just add a new form (i.e. MyNewMessageBox) to your project with these settings:
FormBorderStyle FixedToolWindow
ShowInTaskBar False
StartPosition CenterScreen
To show it use myNewMessageBoxInstance.ShowDialog();. And add a label and buttons to your form, such as OK and Cancel and set their DialogResults appropriately, i.e. add a button to MyNewMessageBox and call it btnOK. Set the DialogResult property in the property window to DialogResult.OK. When that button is pressed it would return the OK result:
MyNewMessageBox myNewMessageBoxInstance = new MyNewMessageBox();
DialogResult result = myNewMessageBoxInstance.ShowDialog();
if (result == DialogResult.OK)
{
// etc
}
It would be advisable to add your own Show method that takes the text and other options you require:
public DialogResult Show(string text, Color foreColour)
{
lblText.Text = text;
lblText.ForeColor = foreColour;
return this.ShowDialog();
}
MessageBox::Show uses function from user32.dll, and its style is dependent on Windows, so you cannot change it like that, you have to create your own form
Here is the code needed to create your own message box:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyStuff
{
public class MyLabel : Label
{
public static Label Set(string Text = "", Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color())
{
Label l = new Label();
l.Text = Text;
l.Font = (Font == null) ? new Font("Calibri", 12) : Font;
l.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor;
l.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor;
l.AutoSize = true;
return l;
}
}
public class MyButton : Button
{
public static Button Set(string Text = "", int Width = 102, int Height = 30, Font Font = null, Color ForeColor = new Color(), Color BackColor = new Color())
{
Button b = new Button();
b.Text = Text;
b.Width = Width;
b.Height = Height;
b.Font = (Font == null) ? new Font("Calibri", 12) : Font;
b.ForeColor = (ForeColor == new Color()) ? Color.Black : ForeColor;
b.BackColor = (BackColor == new Color()) ? SystemColors.Control : BackColor;
b.UseVisualStyleBackColor = (b.BackColor == SystemColors.Control);
return b;
}
}
public class MyImage : PictureBox
{
public static PictureBox Set(string ImagePath = null, int Width = 60, int Height = 60)
{
PictureBox i = new PictureBox();
if (ImagePath != null)
{
i.BackgroundImageLayout = ImageLayout.Zoom;
i.Location = new Point(9, 9);
i.Margin = new Padding(3, 3, 2, 3);
i.Size = new Size(Width, Height);
i.TabStop = false;
i.Visible = true;
i.BackgroundImage = Image.FromFile(ImagePath);
}
else
{
i.Visible = true;
i.Size = new Size(0, 0);
}
return i;
}
}
public partial class MyMessageBox : Form
{
private MyMessageBox()
{
this.panText = new FlowLayoutPanel();
this.panButtons = new FlowLayoutPanel();
this.SuspendLayout();
//
// panText
//
this.panText.Parent = this;
this.panText.AutoScroll = true;
this.panText.AutoSize = true;
this.panText.AutoSizeMode = AutoSizeMode.GrowAndShrink;
//this.panText.Location = new Point(90, 90);
this.panText.Margin = new Padding(0);
this.panText.MaximumSize = new Size(500, 300);
this.panText.MinimumSize = new Size(108, 50);
this.panText.Size = new Size(108, 50);
//
// panButtons
//
this.panButtons.AutoSize = true;
this.panButtons.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.panButtons.FlowDirection = FlowDirection.RightToLeft;
this.panButtons.Location = new Point(89, 89);
this.panButtons.Margin = new Padding(0);
this.panButtons.MaximumSize = new Size(580, 150);
this.panButtons.MinimumSize = new Size(108, 0);
this.panButtons.Size = new Size(108, 35);
//
// MyMessageBox
//
this.AutoScaleDimensions = new SizeF(8F, 19F);
this.AutoScaleMode = AutoScaleMode.Font;
this.ClientSize = new Size(206, 133);
this.Controls.Add(this.panButtons);
this.Controls.Add(this.panText);
this.Font = new Font("Calibri", 12F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Margin = new Padding(4);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.MinimumSize = new Size(168, 132);
this.Name = "MyMessageBox";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.CenterScreen;
this.ResumeLayout(false);
this.PerformLayout();
}
public static string Show(Label Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
List<Label> Labels = new List<Label>();
Labels.Add(Label);
return Show(Labels, Title, Buttons, Image);
}
public static string Show(string Label, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
List<Label> Labels = new List<Label>();
Labels.Add(MyLabel.Set(Label));
return Show(Labels, Title, Buttons, Image);
}
public static string Show(List<Label> Labels = null, string Title = "", List<Button> Buttons = null, PictureBox Image = null)
{
if (Labels == null) Labels = new List<Label>();
if (Labels.Count == 0) Labels.Add(MyLabel.Set(""));
if (Buttons == null) Buttons = new List<Button>();
if (Buttons.Count == 0) Buttons.Add(MyButton.Set("OK"));
List<Button> buttons = new List<Button>(Buttons);
buttons.Reverse();
int ImageWidth = 0;
int ImageHeight = 0;
int LabelWidth = 0;
int LabelHeight = 0;
int ButtonWidth = 0;
int ButtonHeight = 0;
int TotalWidth = 0;
int TotalHeight = 0;
MyMessageBox mb = new MyMessageBox();
mb.Text = Title;
//Image
if (Image != null)
{
mb.Controls.Add(Image);
Image.MaximumSize = new Size(150, 300);
ImageWidth = Image.Width + Image.Margin.Horizontal;
ImageHeight = Image.Height + Image.Margin.Vertical;
}
//Labels
List<int> il = new List<int>();
mb.panText.Location = new Point(9 + ImageWidth, 9);
foreach (Label l in Labels)
{
mb.panText.Controls.Add(l);
l.Location = new Point(200, 50);
l.MaximumSize = new Size(480, 2000);
il.Add(l.Width);
}
int mw = Labels.Max(x => x.Width);
il.ToString();
Labels.ForEach(l => l.MinimumSize = new Size(Labels.Max(x => x.Width), 1));
mb.panText.Height = Labels.Sum(l => l.Height);
mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
LabelWidth = mb.panText.Width;
LabelHeight = mb.panText.Height;
//Buttons
foreach (Button b in buttons)
{
mb.panButtons.Controls.Add(b);
b.Location = new Point(3, 3);
b.TabIndex = Buttons.FindIndex(i => i.Text == b.Text);
b.Click += new EventHandler(mb.Button_Click);
}
ButtonWidth = mb.panButtons.Width;
ButtonHeight = mb.panButtons.Height;
//Set Widths
if (ButtonWidth > ImageWidth + LabelWidth)
{
Labels.ForEach(l => l.MinimumSize = new Size(ButtonWidth - ImageWidth - mb.ScrollBarWidth(Labels), 1));
mb.panText.Height = Labels.Sum(l => l.Height);
mb.panText.MinimumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), ImageHeight);
mb.panText.MaximumSize = new Size(Labels.Max(x => x.Width) + mb.ScrollBarWidth(Labels), 300);
LabelWidth = mb.panText.Width;
LabelHeight = mb.panText.Height;
}
TotalWidth = ImageWidth + LabelWidth;
//Set Height
TotalHeight = LabelHeight + ButtonHeight;
mb.panButtons.Location = new Point(TotalWidth - ButtonWidth + 9, mb.panText.Location.Y + mb.panText.Height);
mb.Size = new Size(TotalWidth + 25, TotalHeight + 47);
mb.ShowDialog();
return mb.Result;
}
private FlowLayoutPanel panText;
private FlowLayoutPanel panButtons;
private int ScrollBarWidth(List<Label> Labels)
{
return (Labels.Sum(l => l.Height) > 300) ? 23 : 6;
}
private void Button_Click(object sender, EventArgs e)
{
Result = ((Button)sender).Text;
Close();
}
private string Result = "";
}
}
Answered, Thanks to Rohit Vats & Panagiotis Kanavos for their answers worked perfectly!
I want a ComboBox to go on the same line as a TextBox in a Stackpanel but it just puts it on the line below when the margins are the same.
The ComboBox and the Textboxs are being generated when I click a button.
C# Code:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal };
TextBox x = new TextBox();
x.Name = "Title" + t;
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.VerticalAlignment = System.Windows.VerticalAlignment.Top;
x.Margin = new Thickness(0, 15, 0, 0);
ComboBox y = new ComboBox();
y.Name = "Combo" + t;
y.Text = (t + 1).ToString();
y.Height = 25;
y.Width = 45;
y.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
y.VerticalAlignment = System.Windows.VerticalAlignment.Top;
y.Margin = new Thickness(0, 15, 0, 0);
spStandard.Children.Add(x);
spStandard.Children.Add(y);
spStandard.Children.Add(sp);
t++;
}
int q = 0;
private void btnQuestion_Click(object sender, RoutedEventArgs e)
{
TextBox x = new TextBox();
x.Name = "Question" + q;
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(70, 15, 0, 0);
spStandard.Children.Add(x);
q++;
}
Picture of what is happening:
http://i.stack.imgur.com/F3Nk8.png
As you can see the Combobox object is getting put under the Textbox when I need it to the left of the Textbox.
Is there any way that i can get around this but by keeping the Stackpanel?
(I asked a question similar to this before but it wasn't for this exact reason.)
You should add it in a StackPanel sp with Orientation set to Horizontal instead of directly adding to outer panel.
Change
spStandard.Children.Add(x);
spStandard.Children.Add(y);
to
sp.Children.Add(x);
sp.Children.Add(y);
It looks like you are adding the elements to the wrong StackPanel. Instead of adding them to sp, which has Orientation = Orientation.Horizontal you add them to spStandard.
You should change:
spStandard.Children.Add(x);
spStandard.Children.Add(y);
to
sp.Children.Add(x);
sp.Children.Add(y);
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);
}
}
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 */
};