I'm trying to create a project in c# , mainly a memory game. It was all going pretty well till i came upon an issue that i haven't seen anyone else have and that really baffles me since i don't think i'm doing anything really complicated. On my "Form" are 16 labels , all with a single method on click. When i run my program , the click part never happens. The buttons work fine and the line-by-line debug starts when they are clicked on.But on the labels it just does nothing , never calls it.Here are a few things that might be needed:
Forum1.Designer.cs*
namespace MatchingGame1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(567, 57);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 69);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(570, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(93, 81);
this.button2.TabIndex = 2;
this.button2.Text = "Hide";
this.button2.UseVisualStyleBackColor = true;
this.button2.Visible = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label5
//
this.label5.Dock = System.Windows.Forms.DockStyle.Fill;
this.label5.Location = new System.Drawing.Point(5, 129);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(125, 125);
this.label5.TabIndex = 4;
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label5.Click += new System.EventHandler(this.label1_Click);
//
// label7
//
this.label7.Dock = System.Windows.Forms.DockStyle.Fill;
this.label7.Location = new System.Drawing.Point(271, 129);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(125, 125);
this.label7.TabIndex = 6;
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label7.Click += new System.EventHandler(this.label1_Click);
//
// label9
//
this.label9.Dock = System.Windows.Forms.DockStyle.Fill;
this.label9.Location = new System.Drawing.Point(5, 256);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(125, 125);
this.label9.TabIndex = 8;
this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label9.Click += new System.EventHandler(this.label1_Click);
//
// label10
//
this.label10.Dock = System.Windows.Forms.DockStyle.Fill;
this.label10.Location = new System.Drawing.Point(138, 256);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(125, 125);
this.label10.TabIndex = 9;
this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label10.Click += new System.EventHandler(this.label1_Click);
//
// label11
//
this.label11.Dock = System.Windows.Forms.DockStyle.Fill;
this.label11.Location = new System.Drawing.Point(271, 256);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(125, 125);
this.label11.TabIndex = 10;
this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label11.Click += new System.EventHandler(this.label1_Click);
//
// label12
//
this.label12.Dock = System.Windows.Forms.DockStyle.Fill;
this.label12.Location = new System.Drawing.Point(404, 256);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(125, 125);
this.label12.TabIndex = 11;
this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label12.Click += new System.EventHandler(this.label1_Click);
//
// label13
//
this.label13.Dock = System.Windows.Forms.DockStyle.Fill;
this.label13.Location = new System.Drawing.Point(5, 383);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(125, 127);
this.label13.TabIndex = 12;
this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label13.Click += new System.EventHandler(this.label1_Click);
//
// label14
//
this.label14.Dock = System.Windows.Forms.DockStyle.Fill;
this.label14.Location = new System.Drawing.Point(138, 383);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(125, 127);
this.label14.TabIndex = 13;
this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label14.Click += new System.EventHandler(this.label1_Click);
//
// label15
//
this.label15.Dock = System.Windows.Forms.DockStyle.Fill;
this.label15.Location = new System.Drawing.Point(271, 383);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(125, 127);
this.label15.TabIndex = 14;
this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label15.Click += new System.EventHandler(this.label1_Click);
//
// label16
//
this.label16.Dock = System.Windows.Forms.DockStyle.Fill;
this.label16.Location = new System.Drawing.Point(404, 383);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(125, 127);
this.label16.TabIndex = 15;
this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label16.Click += new System.EventHandler(this.label1_Click);
//
// label6
//
this.label6.Dock = System.Windows.Forms.DockStyle.Fill;
this.label6.Location = new System.Drawing.Point(138, 129);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(125, 125);
this.label6.TabIndex = 5;
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label6.Click += new System.EventHandler(this.label1_Click);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(5, 2);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(125, 125);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
this.label2.Location = new System.Drawing.Point(138, 2);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(125, 125);
this.label2.TabIndex = 1;
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label2.Click += new System.EventHandler(this.label1_Click);
//
// label3
//
this.label3.Dock = System.Windows.Forms.DockStyle.Fill;
this.label3.Location = new System.Drawing.Point(271, 2);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(125, 125);
this.label3.TabIndex = 2;
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label3.Click += new System.EventHandler(this.label1_Click);
//
// label4
//
this.label4.Dock = System.Windows.Forms.DockStyle.Fill;
this.label4.Location = new System.Drawing.Point(404, 2);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(125, 125);
this.label4.TabIndex = 3;
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label4.Click += new System.EventHandler(this.label1_Click);
//
// label8
//
this.label8.Dock = System.Windows.Forms.DockStyle.Fill;
this.label8.Location = new System.Drawing.Point(404, 129);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(125, 125);
this.label8.TabIndex = 7;
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label8.Click += new System.EventHandler(this.label1_Click);
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.HotTrack;
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Inset;
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Controls.Add(this.label8, 3, 1);
this.tableLayoutPanel1.Controls.Add(this.label13, 0, 3);
this.tableLayoutPanel1.Controls.Add(this.label14, 1, 3);
this.tableLayoutPanel1.Controls.Add(this.label15, 2, 3);
this.tableLayoutPanel1.Controls.Add(this.label16, 3, 3);
this.tableLayoutPanel1.Controls.Add(this.label12, 3, 2);
this.tableLayoutPanel1.Controls.Add(this.label11, 2, 2);
this.tableLayoutPanel1.Controls.Add(this.label10, 1, 2);
this.tableLayoutPanel1.Controls.Add(this.label9, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.label5, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.label6, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.label7, 2, 1);
this.tableLayoutPanel1.Controls.Add(this.label4, 3, 0);
this.tableLayoutPanel1.Controls.Add(this.label3, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.label2, 1, 0);
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 4;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(534, 512);
this.tableLayoutPanel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
this.ClientSize = new System.Drawing.Size(671, 512);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Matching Game";
this.tableLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
}
Form1.cs
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MatchingGame1
{
public partial class Form1 : Form
{
Random random = new Random();
private List<Image> takeIm()
{
string directory = #".\";
List<Image> ImageList = new List<Image>();
foreach (string myFile in Directory.GetFiles(directory, "*.jpg", SearchOption.AllDirectories))
{
ImageList.Add(Image.FromFile(myFile));
ImageList.Add(Image.FromFile(myFile));
}
return ImageList;
}
private void AssignImagesToSquares(List<Image> a)
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label imageLabel = control as Label;
if (imageLabel != null)
{
int randomNumber = random.Next(a.Count);
imageLabel.Image = a[randomNumber];
a.RemoveAt(randomNumber);
}
}
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
clickedLabel.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
List<Image> a = new List<Image>();
a = takeIm();
AssignImagesToSquares(a);
button2.Visible = true;
button2.Enabled = true;
button1.Visible = false;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button2.Visible = false;
foreach (Control control in tableLayoutPanel1.Controls)
{
Label imageLabel = control as Label;
imageLabel.Visible = false;
}
}
}
}
I assume your labels was hidden before you try click on it
Control will not fire event Click if property Visible set to false.
If you want show/hidden control by clicking on it, then try to play with background color or picture in the control.
Hidden - no picture/transparent
Visible - picture or some background color will be shown
Related
I have a tabControl in a Winform application. The user is able to change settings that will impact each tab visually, and I need to get each tabs as images.
I'm trying to understand why/how some controls are updating even if not visible and some other don't.
Is there a way to force an existing UserControl to update/redraw even if not visible so that Control.DrawToBitmap(...) gets a valid image?
I created an example with a Chart and a FlowLayoutPanel to explain what I mean and to make sure I could reproduce the issue I have.
In the example below:
The image created from the FlowLayoutPanel is only correct if it was updated and visible before DrawToBitmap is called.
The image from the Chart is alway up to date even if updated when not visible.
Questions:
How does it work with one control and not the other?
How could I ensure the FlowLayoutPanel has the same behavior as the Chart?
(EDIT) I have tried a few things without success:
Suspend/Resume layout when updating
call Refresh / Update / Invalidate on the layout that do not update as expected
Here is a gif showing that the chart get updated even when not visible but not the FlowLayoutPanel:
Here is the code of this example:
using LiveChartsCore.SkiaSharpView;
using System.Collections.ObjectModel;
namespace WinFormsDrawToBitmapTest
{
public partial class Form1 : Form
{
int legendCount = 1;
ViewModel viewModel;
public Form1()
{
InitializeComponent();
viewModel = new ViewModel();
cartesianChart1.Series = viewModel.Series;
updateLegend(this.flowLayoutPanel1);
}
private async void button1_Click(object sender, EventArgs e)
{
Bitmap bitmap = await Task.Run(() => getImg3());
this.pictureBox1.Image = bitmap;
this.pictureBox1.Invoke((MethodInvoker)delegate
{ this.pictureBox1.Image = bitmap; });
}
private Bitmap getImg3()
{
Bitmap bitmap = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height + this.flowLayoutPanel1.Height);
Bitmap chart = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height);
Bitmap legend = new Bitmap(this.flowLayoutPanel1.Width, this.flowLayoutPanel1.Height);
this.cartesianChart1.Invoke((MethodInvoker)delegate
{
this.cartesianChart1.DrawToBitmap(chart, new Rectangle(new Point(0, 0), this.cartesianChart1.Size));
});
this.flowLayoutPanel1.Invoke((MethodInvoker)delegate
{
this.flowLayoutPanel1.DrawToBitmap(legend, new Rectangle(new Point(0, 0), this.flowLayoutPanel1.Size));
});
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(legend, 0, 0);
g.DrawImage(chart, 0, legend.Height);
}
return bitmap;
}
private void button2_Click(object sender, EventArgs e)
{
Random r = new Random();
viewModel.Data.Add(r.Next(0, 10));
legendCount++;
updateLegend(this.flowLayoutPanel1);
}
private void updateLegend(FlowLayoutPanel flow)
{
flow.Controls.Clear();
Color[] colorList = new Color[]
{
Color.FromArgb(15, 1, 215),
Color.FromArgb(255, 0, 0),
Color.FromArgb(0, 176, 80),
Color.FromArgb(112, 48, 160)
};
for (int i = 0; i < legendCount; i++)
{
//flow.Controls.Add(new LegendLabel($"Label {i}", colorList[i % colorList.Length]));
Button btn = new Button();
btn.Name = $"Button {i}";
btn.TabIndex = 0;
btn.Text = $"Button {i}";
flow.Controls.Add(btn);
}
}
}
public partial class ViewModel
{
public ObservableCollection<double> Data { get; set; }
public List<LineSeries<double>> Series { get; set; }
public ViewModel()
{
Data = new ObservableCollection<double>();
Data.Add(1); Data.Add(2); Data.Add(5); Data.Add(4);
Series = new List<LineSeries<double>>();
Series.Add(new LineSeries<double> { Values = Data, Fill = null });
}
}
}
With the associated .Designer.cs in case it helps
namespace WinFormsDrawToBitmapTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.cartesianChart1 = new LiveChartsCore.SkiaSharpView.WinForms.CartesianChart();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel2.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// panel2
//
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(1042, 24);
this.panel2.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(167, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(86, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Change Tab1";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(161, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Get Tab1 as Image in Tab 2";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 24);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1042, 585);
this.tabControl1.TabIndex = 2;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.cartesianChart1);
this.tabPage1.Controls.Add(this.flowLayoutPanel1);
this.tabPage1.Location = new System.Drawing.Point(4, 24);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(1034, 557);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// cartesianChart1
//
this.cartesianChart1.Location = new System.Drawing.Point(8, 116);
this.cartesianChart1.Name = "cartesianChart1";
this.cartesianChart1.Size = new System.Drawing.Size(1018, 435);
this.cartesianChart1.TabIndex = 4;
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Location = new System.Drawing.Point(8, 6);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(1018, 100);
this.flowLayoutPanel1.TabIndex = 3;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.pictureBox1);
this.tabPage2.Location = new System.Drawing.Point(4, 24);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1034, 557);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1028, 551);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1042, 609);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.panel2);
this.Name = "Form1";
this.Text = "Form1";
this.panel2.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Panel panel2;
private Button button1;
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private PictureBox pictureBox1;
private FlowLayoutPanel flowLayoutPanel1;
private LiveChartsCore.SkiaSharpView.WinForms.CartesianChart cartesianChart1;
private Button button2;
}
}
Thank you for your help with this.
I can only guess the problem lies within the following code, but I'm not 100% sure. The problem only occurs when I try to move the CtrlComputer object in the panel in the RoomDesigner form. The aim of the code is to get the CtrlComputer object to move as one when clicked. Unfortunately without the foreach loop, moves the label1 separately, but still makes an grey box around it when dragged. See here for a clearer picture. I'm not sure why dragging objects around would enlarge the custom control itself.
CtrlComputer
public CtrlComputer()
{
InitializeComponent();
label1.MouseMove += new MouseEventHandler(textbox_MouseMove);
CompButton.MouseMove += new MouseEventHandler(textbox_MouseMove);
this.MouseMove += new MouseEventHandler(textbox_MouseMove);
void textbox_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
foreach (Control control in this.Controls)
{
var location = control.Location;
location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
control.Location = location;
}
}
CtrlComputer.Designer
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.CompButton = new WindowsFormsApplication1.DoubleClickButton();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Lime;
this.label1.Location = new System.Drawing.Point(7, 20);
this.label1.Margin = new System.Windows.Forms.Padding(0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 39;
this.label1.Text = "label1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// CompButton
//
this.CompButton.BackColor = System.Drawing.Color.Transparent;
this.CompButton.BackgroundImage = global::SchoolAdmin.Properties.Resources.memeio;
this.CompButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.CompButton.Location = new System.Drawing.Point(0, 0);
this.CompButton.Name = "CompButton";
this.CompButton.Size = new System.Drawing.Size(62, 57);
this.CompButton.TabIndex = 40;
this.CompButton.UseVisualStyleBackColor = false;
//
// CtrlComputer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.label1);
this.Controls.Add(this.CompButton);
this.Name = "CtrlComputer";
this.Size = new System.Drawing.Size(65, 60);
this.ResumeLayout(false);
this.PerformLayout();
}
CtrlComputer - http://pastebin.com/GJ3aXJFb
I am developing a simple windows form application. But I am not able to center align the contents of the form. here are the images:
Here's the code of the form:
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(1, 1);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.button1.FlatAppearance.BorderColor = System.Drawing.SystemColors.Highlight;
this.button1.FlatAppearance.BorderSize = 5;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.HotTrack;
this.button1.ForeColor = System.Drawing.Color.Coral;
this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
this.button1.Location = new System.Drawing.Point(330, 275);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 123);
this.button1.TabIndex = 1;
this.button1.UseMnemonic = false;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Enabled = false;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.ImageLocation = "";
this.pictureBox1.Location = new System.Drawing.Point(270, 27);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(221, 176);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("OCR A Extended", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(251, 226);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(251, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Please Touch Your Card";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(359, 433);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Highlight;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(752, 502);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox pictureBox1;
}
}
I tried using this,
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
But even that did not help.
I suggest you try the TableLayoutPanel, if you add one you shall get a small 2x2 grid. If you right click and say edit rows and columns you can add a third column and the amount of rows you need. for the settings of the panel I'd suggest something along these lines:
column1 : 50%
column2 : autosize
column3 : 50%
This will make it so the middle column is the minimal required size for your controls, and the remainder of the width is divided over the other 2 columns. Don't forget to dock the tableLayoutPanel to the form.
Thought I'd post it as an awnser instead of the comment I left before.
One way to do is to use TableLayoutPanel as #maam27 commented. Another way is to use the Anchor property of the control. By default a control is anchored to the top-left corner of the parent form. So when form resizes, that point remains fixed. You may choose to break the left anchor. Manually center-align your components in designer. After that if your form resizes, then the relative position will remain unchanged.
you take a DIV than assign align inline property for label
I am new to C# and programming in general. I was able to create the required program in Console but want to get one working with Forms as well. I am running into an issue when trying to get int from textboxes.
On Debug I am getting error:
Error 3 'int' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) D:\Dropbox\Classwork\C_Sharp\InProgress\PaintDeterminator\Paint Determinator Form\Paint Determinator Form\Form1.cs 30 57 Paint Determinator Form
Here is the code I've written so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Paint_Determinator_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int LengthtextBox;
int WidthtextBox;
int HeighttextBox;
int paint;
int answer;
LengthtextBox = int.Parse(LengthtextBox.Text);
WidthtextBox = int.Parse(WidthtextBox.Text);
HeighttextBox = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (LengthtextBox * WidthtextBox * HeighttextBox) / paint;
MessageBox.Show( answer.ToString() );
}
}
}
namespace Paint_Determinator_Form
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.WidthtextBox = new System.Windows.Forms.TextBox();
this.HeighttextBox = new System.Windows.Forms.TextBox();
this.LengthtextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(454, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" +
"ields below!";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 91);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Width";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(38, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Height";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 183);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Length";
//
// WidthtextBox
//
this.WidthtextBox.Location = new System.Drawing.Point(175, 83);
this.WidthtextBox.Name = "WidthtextBox";
this.WidthtextBox.Size = new System.Drawing.Size(100, 20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location = new System.Drawing.Point(175, 131);
this.HeighttextBox.Name = "HeighttextBox";
this.HeighttextBox.Size = new System.Drawing.Size(100, 20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location = new System.Drawing.Point(175, 183);
this.LengthtextBox.Name = "LengthtextBox";
this.LengthtextBox.Size = new System.Drawing.Size(100, 20);
this.LengthtextBox.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 402);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Paint";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(511, 447);
this.Controls.Add(this.button1);
this.Controls.Add(this.LengthtextBox);
this.Controls.Add(this.HeighttextBox);
this.Controls.Add(this.WidthtextBox);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox WidthtextBox;
private System.Windows.Forms.TextBox HeighttextBox;
private System.Windows.Forms.TextBox LengthtextBox;
private System.Windows.Forms.Button button1;
}
Why do you name your ints like your textboxes? It is really a bad practice and confusing at the uttermost level. As you can see, the compiler thinks that you are using the int variables instead of the textboxes and complains that an int type have no property called Text.
So simply change the name of the ints inside the click method
private void button1_Click(object sender, EventArgs e)
{
int l;
int w;
int h;
int paint;
int answer;
l = int.Parse(LengthtextBox.Text);
w = int.Parse(WidthtextBox.Text);
h = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (l * w * h) / paint;
MessageBox.Show( answer.ToString() );
}
Said that, I suggest to use Int32.TryParse to convert the data typed by your user in a valid integer. The Parse method will throw an exception if your user types something that cannot be translated to an integer, instead TryParse returns false without a costly exception
For example
int l;
if(!Int32.TryParse(LengthtextBox.Text, out l))
{
MessageBox.Show("Please type a valid number for Length");
return;
}
When the Int32.TryParse returns, the out parameter (l) contains the
32-bit signed integer value equivalent of the number contained in your
textbox, if the conversion succeeds, or zero if the conversion fails
You're declaring locally scoped variables that will replace your actual textboxes. Yo ushould use local variables that have different names like:
int length = int.Parse(LengthtextBox.Text);
int width = int.Parse(WidthtextBox.Text);;
int height = int.Parse(HeighttextBox.Text);;
I updated your code, you were using variables with the same names as your actual text boxes... That's not a good idea:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Paint_Determinator_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int length;
int width;
int height;
int paint;
int answer;
length = int.Parse(LengthtextBox.Text);
width = int.Parse(WidthtextBox.Text);
height = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (length* width* height) / paint;
MessageBox.Show( answer.ToString() );
}
}
}
namespace Paint_Determinator_Form
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.WidthtextBox = new System.Windows.Forms.TextBox();
this.HeighttextBox = new System.Windows.Forms.TextBox();
this.LengthtextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(454, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" +
"ields below!";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 91);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Width";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(38, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Height";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 183);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Length";
//
// WidthtextBox
//
this.WidthtextBox.Location = new System.Drawing.Point(175, 83);
this.WidthtextBox.Name = "WidthtextBox";
this.WidthtextBox.Size = new System.Drawing.Size(100, 20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location = new System.Drawing.Point(175, 131);
this.HeighttextBox.Name = "HeighttextBox";
this.HeighttextBox.Size = new System.Drawing.Size(100, 20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location = new System.Drawing.Point(175, 183);
this.LengthtextBox.Name = "LengthtextBox";
this.LengthtextBox.Size = new System.Drawing.Size(100, 20);
this.LengthtextBox.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 402);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Paint";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(511, 447);
this.Controls.Add(this.button1);
this.Controls.Add(this.LengthtextBox);
this.Controls.Add(this.HeighttextBox);
this.Controls.Add(this.WidthtextBox);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox WidthtextBox;
private System.Windows.Forms.TextBox HeighttextBox;
private System.Windows.Forms.TextBox LengthtextBox;
private System.Windows.Forms.Button button1;
}
Remove those definitions from button1_Click() method as you use the same names as the class variables inside this method:
int LengthtextBox;
int WidthtextBox;
int HeighttextBox;
And change the name of the variables:
int length;
int width;
int height;
length= int.Parse(LengthtextBox.Text);
width= int.Parse(WidthtextBox.Text);
height= int.Parse(HeighttextBox.Text);
paint = 350;
answer = (length* width* height) / paint;
In your situatuation it's better to rename ints, but you are ablealso to use this in simmilar situation
LengthtextBox = int.Parse(this.LengthtextBox.Text);
WidthtextBox = int.Parse(this.WidthtextBox.Text);
HeighttextBox = int.Parse(this.HeighttextBox.Text);
I think if you are new in programing it's good to know about this too, instead just renaming variables without understanding.
How to put a button on top of panel by using C# code?
public partial class SpaceExplorer : Form
{
Button btnPlayer = new Button();
Panel pGame = new Panel();
public SpaceExplorer()
{
InitializeComponent();
// panel: pGame
pGame.BackColor = System.Drawing.Color.Black;
pGame.Dock = System.Windows.Forms.DockStyle.Fill;
pGame.Location = new System.Drawing.Point(0, 24);
pGame.Name = "panelMain";
Controls.Add(pGame);
}
private void subMenuStart_Click(object sender, EventArgs e)
{
// button: btnPlayer
btnPlayer.Location = new System.Drawing.Point(0, 23);
btnPlayer.Name = "player";
btnPlayer.Size = new System.Drawing.Size(20, 20);
btnPlayer.BackColor = Color.White;
btnPlayer.Text = string.Empty;
btnPlayer.Enabled = false;
Controls.Add(btnPlayer);
}
}
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
panel1.Controls.Add(button1);
This will add button1 on panel1