This code of my button:
int offsetY = 5;
int x = 0;
int y = 0;
int index = 1;
//Start adds new panel and new label
Panel b = new Panel();
Label la = new Label();
//Adds panel properties
b.Controls.Add(la);
b.Location = new Point(x, y + offsetY);
b.Size = new Size(633, 119);
newhaven.Class1 cl = new newhaven.Class1();
b.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
flowLayoutPanel1.Controls.Add(b);
b.ResumeLayout(false);
//Adds label properties
x = 0;
y = -20;
la.Location = new Point(x,y);
// la.Size = new Size(60, 30);
la.Text = "Hello";
flowLayoutPanel1.Controls.Add(la);
I want to hello to be in the panel)
Are can you help me?
You almost got it right, but need to take care of the position co-ordinates for the label, please ensure that they lie inside the panel co-ordinates.
The following works:
int offsetY = 5;
int x = 0;
int y = 0;
int index = 1;
//Start adds new panel and new label
Panel b = new Panel();
Label la = new Label();
//Adds panel properties
b.Controls.Add(la);
b.Location = new Point(x, y + offsetY);
b.Size = new Size(633, 119);
//newhaven.Class1 cl = new newhaven.Class1();
b.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
flowLayoutPanel1.Controls.Add(b);
b.ResumeLayout(false);
// Adds label properties -
// commented the co-ordinates and using the same as that of panel
//x = 0;
//y = -20;
la.Location = new Point(x, y + offsetY);
// la.Size = new Size(60, 30);
la.Text = "Hello";
// no need to add the label separately, its inside the panel
//flowLayoutPanel1.Controls.Add(la);
Related
Why not lable_2 and lable_3 are not displayed?
private void button2_Click(object sender, EventArgs e)
{
int X = 153;
int Y = 34;
for (int i = 1; i < 4; i++)
{
Panel pnl = new Panel();
pnl.SuspendLayout();
pnl.Location = new Point(X, Y);
pnl.Name = "pnl"+i;
pnl.Size = new Size(200, 57);
pnl.BorderStyle = BorderStyle.FixedSingle;
Label lbl = new Label();
lbl.Location = new Point(X - 100, Y - 17);
lbl.Name = "lbl" + i;
lbl.Size = new Size(75, 23);
lbl.Text = "lable_" +i;
pnl.Controls.Add(lbl);
pnl.ResumeLayout(false);
this.Controls.Add(pnl);
Y = Y + 95;
}
}
The Y-position of the 2nd and 3rd labels is outside the bounds of the panel. Instead of giving the location, you can use the Dock property of the Label.
Label lbl = new Label();
lbl.Text = "Label test";
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.Dock = DockStyle.Fill;
panel1.Controls.Add(lbl);
I have multiple GroupBox's, and that's why I set AutoScroll to true. I create all controls in Form_Load. How to place one button after all GroupBox'es?
The code, where I create GroupBoxes:
for (int i = 0; i < 10; i++)
{
GroupBox gb = new GroupBox();
gb.Name = "GroupBox" + (i + 1);
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.Name = "label" + (i + 1);
q_text.Text = "Питання" + (i + 1);
q_text.Font = new Font("Aria", 10, FontStyle.Bold);
q_text.Location = new Point(10, 10);
gb.Controls.Add(q_text);
int iter = q_text.Location.Y + 30;
if (i <= 5)
{
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);
}
}else
if (i > 5)
{
foreach (string key in questions[i].answers.Keys)
{
CheckBox rb = new CheckBox();
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 += 200;
Place all the scrollable group boxes in one panel which is set to AutoScroll = true. Below this panel is another one containing the fixed button. This panel is docked to the bottom.
Since you are using a loc variable, you can do:
btnMyButton.Locaction= new Point(40, loc);
Anyway, if you want to find the position of the last group box dynamically, try this:
double leftPos=0,topPos=0;
foreach(Control c in Forms.Controls)
{
if(c.Name=="GroupBox")
{
if(c.Left>leftPos)
leftPos=c.Left;
if(c.Top>topPos)
topPos=c.Top;
}
}
Good afternoon.
There was a problem with the rendering of dynamically created panel, all panels initially were 100px and I their renders (y + = 100), but now the production has changed a bit, these panels can be of different sizes, and the distance between them actually remains unchanged 100rh ...
Pliz tell me how to make, that they are somehow drawn at equal (10px) distances from each other. Read that you can somehow make a method SetBounds, but did not understand.
http://pastebin.com/TnSuFTti
for (int i = data_list.Count - 1; i >= 0; i--)
{
Panel panel = new Panel(); //создание блока сообщения и наложение картинки
panel.Name = i.ToString();
panel.MouseEnter += new EventHandler(panel_MouseEnter);
Label textBox_name = new Label();
Label textBox_date = new Label();
Label textBox_msg = new Label();
panel.Width = 308;
Bitmap btm_msg = new Bitmap(
Properties.Resources.NotificationCenterWindow_msg_box);
panel.BackgroundImage = btm_msg;
panel.BackgroundImageLayout = ImageLayout.Stretch;
panel.Location = new Point(5, 5);
panel.Controls.Add(textBox_date);
textBox_date.Name = "textBox_date" + i.ToString();
textBox_date.Location = new Point(232, 8);
textBox_date.Size = new System.Drawing.Size(70, 15);
textBox_date.BorderStyle = BorderStyle.None;
textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
textBox_date.TextAlign = ContentAlignment.MiddleRight;
textBox_date.BackColor = Color.DarkGray;
textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
textBox_date.ForeColor = SystemColors.ScrollBar;
panel.Controls.Add(textBox_name);
textBox_name.Size = new System.Drawing.Size(100, 15);
textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
textBox_name.Location = new Point(5, 8);
textBox_name.BorderStyle = BorderStyle.None;
textBox_name.BackColor = Color.DarkGray;
textBox_name.ForeColor = SystemColors.ScrollBar;
panel.Height = textBox_name.Height + 19;
panel1.Controls.Add(panel);
panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
y += 100;
}
panel.Location = new Point(5, y);
//.....
y = panel.Bottom + 10;
Just set the position based on the previous panel...
// This will put the panel to the right of an existing one
panel_b.Left = panel_a.Left + panel_a.Width + 10;
.
// This will put the panel below an existing one
panel_b.Top= panel_a.Top+ panel_a.Height+ 10;
I have a tableLayoutPanel (size of 10x10 columns & rows added from the toolbox) and I have added single Panel into every cells so I could add two objects/components into a cell. Every cell contains a label and a button. The problem is I couldn't add the two components into a panel programatically. What should I do?
Here is my code:
private int[,] grid;
private Button[,] btn_grid;
private Label[,] lbl_grid;
private int timer = 0;
private Panel[,] pnl_grid;
private bool createGrid()
{
Random rnd1 = new Random();
grid = new int[width, height];
pnl_grid = new Panel[width, height];
btn_grid = new Button[width, height];
lbl_grid = new Label[width, height];
for (int x = 0; x <width; x++)
{
for (int y = 0; y < height; y++)
{
btn_grid[x, y] = createButton(x, y);
lbl_grid[x, y] = createLables(x,y);
pnl_grid[x, y] = createPanels(x, y);
**//something's missing here to add the 2 components into a panel**
tableLayoutPanel2.Controls.Add(pnl_grid[x,y]);
}
}}
private Button createButton(int gridX, int gridY)
{
Button bttn = new Button();
bttn.Text = "";
bttn.Name = gridX.ToString() + " " + gridY.ToString();
bttn.Size = new System.Drawing.Size(30, 30);
Controls.AddRange(new System.Windows.Forms.Control[] { bttn, });
bttn.Click += new System.EventHandler(bttnOnclick);
//bttn.MouseClick += new System.Windows.Forms.MouseEventHandler(this.bttnOnRightClick);
return bttn;
}
private Label createLables(int gridX, int gridY)
{
Label lbl = new Label();
lbl.Name = gridX.ToString() + " " + gridY.ToString();
lbl.Text = "0";
lbl.Size = new System.Drawing.Size(30, 30);
//lbl.Font = new Font("Microsoft Sans Serif", 15.75f, lbl.Font.Style, lbl.Font.Unit);
Controls.AddRange(new System.Windows.Forms.Control[] { lbl, });
return lbl;
}
private Panel createPanels(int gridX, int gridY)
{
Panel pnl = new Panel();
pnl.Name = gridX.ToString() + " " + gridY.ToString();
//pnl.Text = "0";
pnl.Size = new System.Drawing.Size(30, 30);
//lbl.Font = new Font("Microsoft Sans Serif", 15.75f, lbl.Font.Style, lbl.Font.Unit);
Controls.AddRange(new System.Windows.Forms.Control[] { pnl, });
return pnl;
}
Thank you for your appreciated attention and help!
Your code is fine until the last moment when you have to add the controls to the tableLayourPanel or to the panel. You just add the panel but not the button/label to the panel.
Thus, you have to options:
Adding button and label to the panel and the panel to the tableLayoutPanel (which seems the most logical one):
btn_grid[x, y] = createButton(x, y);
lbl_grid[x, y] = createLables(x,y);
pnl_grid[x, y] = createPanels(x, y);
pnl_grid[x, y].Controls.Add(btn_grid[x, y]);
pnl_grid[x, y].Controls.Add(lbl_grid[x, y]);
tableLayoutPanel2.Controls.Add(pnl_grid[x,y]);
or adding the three elements directly to the tableLayoutPanel:
btn_grid[x, y] = createButton(x, y);
lbl_grid[x, y] = createLables(x,y);
pnl_grid[x, y] = createPanels(x, y);
tableLayoutPanel2.Controls.Add(btn_grid[x,y]);
tableLayoutPanel2.Controls.Add(lbl_grid[x,y]);
tableLayoutPanel2.Controls.Add(pnl_grid[x,y]);
You can remove the Controls.AddRange call from createLables, createButton createPanels method.
Then you can modify your createpanels method as below
private Panel createPanels(int gridX, int gridY)
{
Label lbl = lbl_grid[gridX,gridY];
Button btn = btn_grid[gridX,gridY];
Panel pnl = new Panel();
pnl.Name = gridX.ToString() + " " + gridY.ToString();
//pnl.Text = "0";
pnl.Size = new System.Drawing.Size(30, 30);
//lbl.Font = new Font("Microsoft Sans Serif", 15.75f, lbl.Font.Style, lbl.Font.Unit);
pnl.Controls.AddRange(new System.Windows.Forms.Control[] { lbl,btn });
lbl.Dock = DockStyle.Top;
btn.Dock = DockStyle.Fill;
return pnl;
}
Hope this helps
I have written loop for picture box, it looks like this:
void labelAdder()
{
List<Label> labels = new List<Label>();
List<TextBox> texboxex = new List<TextBox>();
List<PictureBox> pictureBoxes = new List<PictureBox>();
for (int i = 0; i < args.Length - 1; i++)
{
equals++;
var temp = new TextBox();
var temp2 = new PictureBox();
int x = 10;
int y = xD * equals;
temp.Location = new Point(x, y);
temp2.Location = new Point(x + 100, y);
temp2.Image = global::Xbox360_Complex_Checker.Properties.Resources.button_offline;
temp.Text = args[i];
temp2.Text = status[i];
this.Controls.Add(temp);
this.Controls.Add(temp2);
temp.Show();
temp2.Show();
texboxex.Add(temp);
pictureBoxes.Add(temp2);
}
}
My problem is that PictureBox is loading only next to first textBox, it should load next to all textBoxes. I don't know why it's not working. I tried this with labels, and labels were loading properly.