I'm attempting to dynamically add a series of controls to a form, but the primary problem is that they are completely misaligned. I've tried updated the control information and fixed minor problems but they are still misaligned.
I'm unsure what I'm missing on the alignment problem:
public void loadSkill(string skillName)
{
FlowLayoutPanel panel = new FlowLayoutPanel();
standardSkillsLayoutPanel.Controls.Add(panel);
panel.Dock = DockStyle.Top;
panel.Height = 28;
panel.Width = 450;
Label skillLabel = new Label();
panel.Controls.Add(skillLabel);
skillLabel.MinimumSize = new Size(120, 20);
skillLabel.MaximumSize = new Size(120, 20);
skillLabel.Text = skillName;
skillLabel.Location = new System.Drawing.Point(0, 0);
skillLabel.Dock = DockStyle.Left;
Label skillStats = new Label();
panel.Controls.Add(skillStats);
skillStats.Text = "DEX + STR";
skillStats.MinimumSize = new Size(80, 20);
skillStats.MaximumSize = new Size(80, 20);
skillStats.Margin = new Padding(3, 0, 3, 0);
skillStats.Dock = DockStyle.Left;
Label skillBasePercent = new Label();
panel.Controls.Add(skillBasePercent);
skillBasePercent.MinimumSize = new Size(60, 20);
skillBasePercent.MaximumSize = new Size(60, 20);
skillBasePercent.Text = "10";
skillBasePercent.Dock = DockStyle.Left;
skillBasePercent.Location = new System.Drawing.Point(210, 0);
NumericUpDown skillCulturalUpdown = new NumericUpDown();
panel.Controls.Add(skillCulturalUpdown);
skillCulturalUpdown.MinimumSize = new Size(40, 20);
skillCulturalUpdown.MaximumSize = new Size(40, 20);
skillCulturalUpdown.Margin = new Padding(3, 0, 3, 0);
skillCulturalUpdown.Value = 0;
skillCulturalUpdown.Dock = DockStyle.Left;
NumericUpDown skillProfessionalUpdown = new NumericUpDown();
panel.Controls.Add(skillProfessionalUpdown);
skillProfessionalUpdown.MinimumSize = new Size(40, 20);
skillProfessionalUpdown.MaximumSize = new Size(40, 20);
skillProfessionalUpdown.Margin = new Padding(3, 0, 3, 0);
skillProfessionalUpdown.Value = 0;
skillProfessionalUpdown.Dock = DockStyle.Left;
NumericUpDown bonusUpDown = new NumericUpDown();
panel.Controls.Add(bonusUpDown);
bonusUpDown.Value = 0;
bonusUpDown.MinimumSize = new Size(40, 20);
bonusUpDown.MaximumSize = new Size(40, 20);
bonusUpDown.Margin = new Padding(3, 0, 3, 0);
bonusUpDown.Dock = DockStyle.Left;
Label skillTotalPercent = new Label();
panel.Controls.Add(skillTotalPercent);
skillTotalPercent.Text = "10";
skillTotalPercent.MinimumSize = new Size(50, 20);
skillTotalPercent.Dock = DockStyle.Left;
}
Related
I have problems with Styling my grid, I am working on small app for caffe bars lets say, customer is able to choose drink from the middle of screen and drink will be shown in the right part of screen in my grid, this is how that looks like right now (when customer choose something - lets say Coke, sprite etc.):
This is my code for now :
public DrinksPanel(byte[] image)
: base()
{
BrushConverter MyBrush8 = new BrushConverter();
var myBorder = new Border();
myBorder.Background = (Brush)MyBrush8.ConvertFrom("#83D744");
Thickness marginT = this.Margin;
marginT.Top = 10;
this.Margin = marginT;
ColumnDefinition col1 = new ColumnDefinition();
col1.Width = new GridLength(45);
ColumnDefinition col2 = new ColumnDefinition();
col2.Width = new GridLength(80, GridUnitType.Star);
ColumnDefinition col3 = new ColumnDefinition();
col3.Width = new GridLength(45);
this.ColumnDefinitions.Add(col1);
this.ColumnDefinitions.Add(col2);
this.ColumnDefinitions.Add(col3);
this.Height = 45;
this.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
this.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
ArticleNameLabel.VerticalContentAlignment = VerticalAlignment.Center;
ArticleNameLabel.HorizontalContentAlignment = HorizontalAlignment.Left;
ArticleNameLabel.FontSize = fontSize;
ArticleNameLabel.Foreground = System.Windows.Media.Brushes.White;
ArticleNameLabel.FontFamily = Util.Font;//new System.Windows.Media.FontFamily("HelveticaNeueLTStd-Bd.otf#Helvetica Neue LT Std");
ArticleNameLabel.Content = tekst;
ArticleNameLabel.SetValue(Grid.RowProperty, 0);
ArticleNameLabel.SetValue(Grid.RowSpanProperty, 1);
ArticleNameLabel.SetValue(Grid.ColumnProperty, 1);
ArticleNameLabel.SetValue(Grid.ColumnSpanProperty, 1);
ArticleNameLabel.Width = Double.NaN;
ArticleNameLabel.Height = Double.NaN;
QuantityNumberLabel.Width = Double.NaN;
QuantityNumberLabel.Height = Double.NaN;
QuantityNumberLabel.SetValue(Grid.RowProperty, 0);
QuantityNumberLabel.SetValue(Grid.RowSpanProperty, 1);
QuantityNumberLabel.SetValue(Grid.ColumnProperty, 2);
ArticleNameLabel.SetValue(Grid.ColumnSpanProperty, 1);
QuantityNumberLabel.HorizontalAlignment = HorizontalAlignment.Center;
QuantityNumberLabel.VerticalAlignment = VerticalAlignment.Center;
QuantityNumberLabel.FontFamily = Util.Font;
QuantityNumberLabel.FontSize = numberSize;
QuantityNumberLabel.Content = "x1";
QuantityNumberLabel.Foreground = System.Windows.Media.Brushes.White;
BrushConverter MyBrush3 = new BrushConverter();
QuantityNumberLabel.Background = (Brush)MyBrush3.ConvertFrom("#83D744");
_image.Width = Double.NaN;
_image.Height = Double.NaN;
_image.HorizontalAlignment = HorizontalAlignment.Stretch;
_image.VerticalAlignment = VerticalAlignment.Stretch;
_image.SetValue(Grid.RowProperty, 0);
_image.SetValue(Grid.RowSpanProperty, 1);
_image.SetValue(Grid.ColumnProperty, 0);
_image.SetValue(Grid.ColumnSpanProperty, 1);
Image.Source = GetSlikaFromByte(image);
Rectangle background = new Rectangle();
background.SetValue(Grid.RowProperty, 0);
background.SetValue(Grid.RowSpanProperty, 1);
background.SetValue(Grid.ColumnProperty, 0);
background.SetValue(Grid.ColumnSpanProperty,3);
background.HorizontalAlignment = HorizontalAlignment.Stretch;
background.VerticalAlignment = VerticalAlignment.Stretch;
background.Width = Double.NaN;
background.Height = Double.NaN;
background.Fill = (Brush)MyBrush.ConvertFrom("#50000000");
_animation = new DoubleAnimation(numberSize, numberSize + 15, new Duration(new TimeSpan(0, 0, 0, 0, 300)));
_animation.AutoReverse = true;
this.Children.Add(background);
this.Children.Add(_image);
this.Children.Add(ArticleNameLabel);
this.Children.Add(QuantityNumberLabel);
}
***So my question is guys, how could apply
myborder
around grid?***
If anyone could help me about this I would appreciate that so much =)
I have a while loop inside a method that is supposed to create 24 dynamically created objects. However the loop creates 3 and then stops.
int test = 24;
while (placenum< test)
{
FlowLayoutPanel name = new FlowLayoutPanel();
FlowLayoutPanel data = new FlowLayoutPanel();
FlowLayoutPanel pic = new FlowLayoutPanel();
int sort = 0;
while (sort == 0)
{
SearchR.Searchrdata.Controls.Add(first);
first.Margin = new Padding(50, 50, 3, 50);
first.Size = new Size(320, 320);
first.BackColor = Color.Azure;
first.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
first.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
first.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
sort = 1;
placenum = placenum + 1;
}
while (sort == 1)
{
SearchR.Searchrdata.Controls.Add(two);
two.Margin = new Padding(0, 50, 3, 50);
two.Size = new Size(320, 320);
two.BackColor = Color.DarkGray;
two.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
two.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
two.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
sort = 2;
placenum = placenum + 1;
}
while (sort == 2)
{
SearchR.Searchrdata.Controls.Add(last);
last.Margin = new Padding(0, 50, 10, 50);
last.Size = new Size(320, 320);
last.BackColor = Color.Bisque;
last.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
last.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
last.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
sort = 0;
placenum = placenum + 1;
}
};
return null;
The FlowLayoutPanel called Searchrdata is the one that the other objects are supposed to be made but, it makes only 3 and stops.
Remove ; from the end of outer while loop. It causes termination of loop after 1 iteration. P.S it will create 3x24 = 72 total objects instead of 24.
A better solution will be :
for( int i = 0; i < 8; i++)
{
FlowLayoutPanel name = new FlowLayoutPanel();
FlowLayoutPanel data = new FlowLayoutPanel();
FlowLayoutPanel pic = new FlowLayoutPanel();
SearchR.Searchrdata.Controls.Add(first);
first.Margin = new Padding(50, 50, 3, 50);
first.Size = new Size(320, 320);
first.BackColor = Color.Azure;
first.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
first.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
first.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
SearchR.Searchrdata.Controls.Add(two);
two.Margin = new Padding(0, 50, 3, 50);
two.Size = new Size(320, 320);
two.BackColor = Color.DarkGray;
two.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
two.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
two.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
SearchR.Searchrdata.Controls.Add(last);
last.Margin = new Padding(0, 50, 10, 50);
last.Size = new Size(320, 320);
last.BackColor = Color.Bisque;
last.Controls.Add(name);
name.Size = new Size(320, 22);
name.Margin = new Padding(0);
last.Controls.Add(data);
data.Size = new Size(320, 22);
data.Margin = new Padding(0);
last.Controls.Add(pic);
pic.Size = new Size(320, 276);
pic.Margin = new Padding(0);
}
}
return null;
You have three variables named first, two, and last. Presumably these are the controls you are trying to add. The issue is you are adding the same instance of the three variables over and over again. You should be creating a new instance of first, two, and last each time if you want a new one added each time. Note that you are already doing this with name, data, and pic.
Also, as others have noted, you also don't need while (sort == 1) it should be if (sort == 1).
Finally, note that if you ran your code through the debugger and stepped through, you would see that your loop is running the number of times you want, and you might have figured out what the real issue is.
When I set a Margin from the Top to 15 for the TextBox:
x.Margin = new Thickness(100, 15, 0, 0);
This works fine and everything it ok, but then I want to make a ComboBox also appear 15px from the top - it doesn't work.
y.Margin = new Thickness(0, 15, 0, 0);
This is the code for the button I click to create the ComboBox and the TextBox:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
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.Margin = new Thickness(100, 15, 0, 0);
spStandard.Children.Add(x);
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.Margin = new Thickness(0, 15, 0, 0);
spStandard.Children.Add(y);
t++;
}
Here is a picture of what happens when I run the application - it shows where the ComboBox gets put:
Here is a solution for your problem, you shouldn't create controls in code behind but it will get the job done:
int t = 0;
private void btnAddTitle_Click(object sender, RoutedEventArgs e)
{
//a new stackpanel is used to arrange items Horizontally for each line
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.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.Margin = new Thickness(0, 15, 0, 0);
sp.Children.Add(y);
sp.Children.Add(x);
spStandard.Children.Add(sp);
t++;
}
I have some trouble when i'm trying to click my dynamic controls in StackPanel.
I'm adding controls to Grid in this way...
void Opt()
{
TextBlock Title_1 = new TextBlock();
TextBlock Title_2 = new TextBlock();
CheckBox Kwota_exists = new CheckBox();
TextBox Title = new TextBox();
StackPanel Frame = new StackPanel();
Button OK = new Button();
Title_1.Text = "Dodaj kategorię";
Title_2.Text = "Aktywne kategorię";
Kwota_exists.Content = "Stała kwota?";
Title.Text = "Nazwa kategorii";
OK.Content = "Dodaj";
OK.IsEnabled = true;
OK.IsHitTestVisible = true;
OK.IsTabStop = true;
OK.ClickMode = ClickMode.Release;
Frame.IsHitTestVisible = true;
Kwota_exists.Checked +=Kwota_exists_Checked;
Title_1.FontSize = 50;
Title_2.FontSize = 50;
Title.FontSize = 20;
Frame.Height = 100;
Frame.Width = 400;
Title_1.Margin = new Thickness(0, 0, 0, 0);
Title_2.Margin = new Thickness(0, 220, 0, 0);
Frame.Margin = new Thickness(0, 70, 0, 0);
Frame.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Left;
Frame.VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Top;
Frame.Orientation = Orientation.Horizontal;
Frame.Background = new SolidColorBrush(Windows.UI.Color.FromArgb(145, 56, 234, 21));
Frame.Children.Add(Kwota_exists);
Frame.Children.Add(Title);
Frame.Children.Add(OK);
GrdContent.Children.Add(Frame);
GrdContent.Children.Add(Title_1);
GrdContent.Children.Add(Title_2);
}
But when i'm trying to click button or check checkbox controls doesn't seem to response (unclickable).
Looks like i can't access them or i'm doing something wrong. I would be gradefull if someone explain me where i'm doing mistake.
TableLayoutPanel t = new TableLayoutPanel();
t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Label lbl = new Label();
lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
lbl.Text = "Hello";
t.Controls.Add(lbl, 0, 0);
this.Text = t.Size.Height.ToString();
this.Controls.Add(t);
Why the t.Size.Height property gives me 100 always ?
The reason this always was returning 100 is that you need:
AutoSize = true
AutoSizeMode =AutoSizeMode.GrowAndShrink
t.RowCount >= 1
t.ColumnCount >= 1
TableLayoutPanel t = new TableLayoutPanel();
t.AutoSize = true; //added
t.AutoSizeMode =AutoSizeMode.GrowAndShrink; //added
t.RowCount = 1; //added
t.ColumnCount = 1; //added
t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
Label lbl = new Label();
lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
lbl.Text = "Hello";
t.Controls.Add(lbl, 0, 0);
this.Controls.Add(t);
this.Text = t.Size.Height.ToString(); //moved
You also need to move your Height check to after you have added the Table to the form, else no layout operations will have happened.