I have four panels and in each click events I have loaded different User controls. So when I click a particular panel I want the clicked panel background changed and rest panel to be same.
How can I do it pro-grammatically?
Panel pan1 = new Panel();
Panel pan2 = new Panel();
Panel pan3 = new Panel();
Panel pan4 = new Panel();
private void Form1_Load(object sender, EventArgs e)
{
pan1.Name = "pan1";
pan1.Location = new Point(0, 0);
pan1.Size = new Size(100, 100);
pan1.BackColor = Color.LightGray;
pan1.Click += new EventHandler(this.Panel_Click);
pan2.Name = "pan2";
pan2.Location = new Point(110, 0);
pan2.Size = new Size(100, 100);
pan2.BackColor = Color.LightGray;
pan2.Click += new EventHandler(this.Panel_Click);
pan3.Name = "pan3";
pan3.Location = new Point(220, 0);
pan3.Size = new Size(100, 100);
pan3.BackColor = Color.LightGray;
pan3.Click += new EventHandler(this.Panel_Click);
pan4.Name = "pan4";
pan4.Location = new Point(330, 0);
pan4.Size = new Size(100, 100);
pan4.BackColor = Color.LightGray;
pan4.Click += new EventHandler(this.Panel_Click);
this.Controls.Add(pan1);
this.Controls.Add(pan2);
this.Controls.Add(pan3);
this.Controls.Add(pan4);
}
private void Panel_Click(object sender , EventArgs e)
{
this.pan1.BackColor = Color.LightGray;
this.pan2.BackColor = Color.LightGray;
this.pan3.BackColor = Color.LightGray;
this.pan4.BackColor = Color.LightGray;
Panel pan = (Panel)sender;
pan.BackColor = Color.Red;
}
Related
I am trying to set my panel so it will scroll horizontally. I have tried:
MainPanel.HorizontalScroll.Enabled = true;
MainPanel.HorizontalScroll.Visible = true;
MainPanel.VerticalScroll.Enabled = false;
MainPanel.VerticalScroll.Visible = false;
My MainPanel is a parent to 2 sub Panels that are Horizontaly larger than the Panel itself but the horizontal scroll bar still doesn't appear. How can I fix this?
This is the code in the .Designer.cs file of the Parent Form of my Panels:
this.panel0.Dock = System.Windows.Forms.DockStyle.Top;
this.panel0.Location = new System.Drawing.Point(0, 0);
this.panel0.Name = "panel0";
this.panel0.Size = new System.Drawing.Size(828, 28);
this.panel0.TabIndex = 2;
//
// list
//
this.list.Dock = System.Windows.Forms.DockStyle.Fill;
this.list.Location = new System.Drawing.Point(0, 28);
this.list.Name = "list";
this.list.Size = new System.Drawing.Size(828, 444);
this.list.TabIndex = 3;
//
// MainPanel
//
this.MainPanel.AutoScroll = true;
this.MainPanel.Controls.Add(this.list);
this.MainPanel.Controls.Add(this.panel0);
this.MainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.MainPanel.Location = new System.Drawing.Point(152, 104);
this.MainPanel.Name = "MainPanel";
this.MainPanel.Size = new System.Drawing.Size(828, 472);
this.MainPanel.TabIndex = 7;
So I'm creating some panels with the following code
private void button1_Click(object sender, EventArgs e)
{
int xlocation = 5;
for (int i = 0; i < 10; i++)
{
Panel panel = new Panel();
{
panel.Name = string.Format("{0}", i);
panel.Text = string.Format(i.ToString());
panel.BackColor = Color.White;
panel.Location = new System.Drawing.Point(xlocation, 30);
panel.Width = flowLayoutPanel1.Width;
panel.Height = 50;
panel.MouseEnter += new System.EventHandler(this.panel_MouseEnter);
flowLayoutPanel1.Controls.Add(panel);
Label label = new Label();
label.Location = new System.Drawing.Point(15, 10);
label.AutoSize = true;
label.Font = new System.Drawing.Font("Calibri", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
label.Text = string.Format("{0}", "GNU" + i);
panel.Controls.Add(label);
Label label10 = new Label();
label10.Location = new System.Drawing.Point(15, 35);
label10.AutoSize = true;
label10.Font = new System.Drawing.Font("Calibri", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label10.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
label10.Text = string.Format("{0}", "hest");
panel.Controls.Add(label10);
}
xlocation = xlocation + 85;
}
}
The problem is when I resize my form, my flowLayoutPanel obviously gets bigger, but the panels inside doesn't.
I have tried using the
private void Form1_Resize(object sender, EventArgs e)
{
}
But it doesn't seem to work.
Any answer is much appreciated!
Thanks!
You can either use the Anchor property and set it to Right and Left, or can dock each panel to parent control. Something like this should work:
Panel panel = new Panel();
panel.Dock = DockStyle.Top; // Docks the panel to top, and fills to the width of Parent control
panel.Anchor = (AnchorStyles.Right | AnchorStyles.Left); // Set anchor to right and left
I found a solution. I just created a list with my panels like so
List<Panel> pnls = new List<Panel>();
And just did like so to make them follow the width of my flowlayoutpanel, so you can actually use a flowlayoutpanel ;-)
private void Kontrol_Resize(object sender, EventArgs e)
{
for (int i = 0; i < pnls.Count; i++)
{
pnls[i].Width = activityData.Width - 24;
pnls[i].Height = activityData.Height / 4;
}
}
So I have an Image Map and on it I want to appear 3 buttons each time I clicked on a location. Those 3 button would be : hotspot, delete hotspot, save hotspot. These Buttons are Dynamically generated. The question is, how can I the hotspot from delete hotspot Button and also close the other 2 Buttons.
Some code for a little bit of understanding of what am I doing:
private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Locatia
PictureBox C = new PictureBox();
int i = 0;
C.Location = new Point(e.X-13, e.Y-30);
C.Name = "Problema_" + (i + 1).ToString();
C.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_GPS_500px.png";
C.Size = new Size(26, 30);
C.SizeMode = PictureBoxSizeMode.StretchImage;
C.BackColor = Color.Transparent;
C.Cursor = Cursors.Hand;
// C.Click += new EventHandler(this.StartRecordingToolStripMenuItem_Click_1);;
PictureBox1.Controls.Add(C);
//salveaza Locatia
PictureBox S = new PictureBox();
S.Name = "Salveaza_" + (i + 1).ToString();
S.Location = new Point(e.X - 45, e.Y+10);
S.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Checked_Checkbox_500px.png";
S.Size = new Size(35, 35);
S.SizeMode = PictureBoxSizeMode.StretchImage;
S.BackColor = Color.Transparent;
S.Cursor = Cursors.Hand;
PictureBox1.Controls.Add(S);
//sterge Locatia
PictureBox St = new PictureBox();
St.Name = "Sterge_" + (i + 1).ToString();
St.Location = new Point(e.X +10, e.Y+10);
St.Cursor = Cursors.Hand;
St.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Close_Window_500px.png";
St.Size = new Size(35, 35);
St.SizeMode = PictureBoxSizeMode.StretchImage;
St.BackColor = Color.Transparent;
PictureBox1.Controls.Add(St);
S.Click += new EventHandler(this.stergeAprob);
C.Click += new EventHandler(this.clickHotspot);
}
The solution could be that when creating dynamically you also delete it dynamically using anonymous functions
S.Click += (o, e) => {
//....actions
PictureBox1.Controls.remove(C)
//... other actions
}
we can have access to dynamically created variables since the compiler creates inline functions
I want put a bunch of buttons at the top section of my form, and a SplitContainer right below these buttons. All these are done in the code (not thru the designer), so I can manage the number and text of the buttons dynamically. I reckon it is handy to put the button into a FlowLayoutPanel, so they can response with the flow feature when the form gets resized.
My question is how do I set up the SplitContainer layout settings (size, location, anchor, docking etc.), to make sure no matter how I resize the form, the SplitContainer is right below the buttons (no gap), and it fills up the rest of the form. Below is what I've done.
this.Size = new Size(300, 400);
string[] tags = new string[] { "action", "romance", "drama", "sci-fi", "horror" };
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Size = new Size(this.Width, 50);
flp.Location = new Point(0, 0);
flp.AutoSize = true;
flp.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
flp.FlowDirection = FlowDirection.LeftToRight;
this.Controls.Add(flp);
//flp.Resize += flp_Resize;
foreach (string tag in tags)
{
Button btn = new Button();
btn.Text = tag;
flp.Controls.Add(btn);
}
SplitContainer sc = new SplitContainer();
ListBox lb = new ListBox(); lb.Dock = DockStyle.Fill; lb.Parent = sc.Panel1; lb.Items.Add("test");
PictureBox pb = new PictureBox(); pb.Dock = DockStyle.Fill; pb.Parent = sc.Panel2; pb.BackColor = Color.LightBlue;
sc.Size = new Size(this.Width, this.Height-flp.Height);
sc.Location = new Point(0, flp.Height);
sc.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
this.Controls.Add(sc);
Here is the solution. Thanks to #Fabio.
this.Size = new Size(300, 400);
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.RowCount = 2;
tlp.ColumnCount = 1;
tlp.Dock = DockStyle.Fill;
this.Controls.Add(tlp);
string[] tags = new string[] { "action", "romance", "drama", "sci-fi", "horror" };
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.AutoSize = true;
flp.Dock = DockStyle.Fill;
flp.BackColor = Color.LightCyan;
flp.FlowDirection = FlowDirection.LeftToRight;
tlp.Controls.Add(flp, 0, 0);
foreach (string tag in tags)
{
Button btn = new Button();
btn.Text = tag;
flp.Controls.Add(btn);
}
SplitContainer sc = new SplitContainer();
ListBox lb = new ListBox(); lb.Dock = DockStyle.Fill; lb.Parent = sc.Panel1; lb.Items.Add("test");
PictureBox pb = new PictureBox(); pb.Dock = DockStyle.Fill; pb.Parent = sc.Panel2; pb.BackColor = Color.LightBlue;
sc.Dock = DockStyle.Fill;
tlp.Controls.Add(sc,0,1);
I made a custom form.
Now I'm trying to implement the feature to drag the form around while holding the titlebar. But it always throws this exception:
system.ObjectDisposedException
Has anyone an idea what the problem is?
This is my code:
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
Form form;
Button buttonCancel;
Button buttonOk;
DialogResult InputBox(string title, string promptText, ref string value)
{
form = new Form();
buttonCancel = new Button();
buttonOk = new Button();
Label label = new Label();
TextBox textBox = new TextBox();
Panel titlebar = new Panel();
Label lbltitle = new Label();
Panel pnlexit = new Panel();
Label lblexit = new Label();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
buttonCancel.Location = new Point(250, 100);
buttonCancel.Parent = form;
buttonCancel.BackColor = ColorTranslator.FromHtml("#CDDC39");
buttonCancel.FlatStyle = FlatStyle.Flat;
buttonCancel.FlatAppearance.BorderSize = 0;
buttonCancel.MouseClick += ButtonCancel_MouseClick;
buttonOk.Location = new Point(170, 100);
buttonOk.Parent = form;
buttonOk.BackColor = ColorTranslator.FromHtml("#CDDC39");
buttonOk.FlatStyle = FlatStyle.Flat;
buttonOk.FlatAppearance.BorderSize = 0;
form.Height = 150;
form.Width = 350;
form.BackColor = ColorTranslator.FromHtml("#455A64");
form.FormBorderStyle = FormBorderStyle.None;
form.AutoSize = false;
form.StartPosition = FormStartPosition.CenterScreen;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
titlebar.Width = form.Width;
titlebar.Height = 25;
titlebar.BackColor = ColorTranslator.FromHtml("#37474F");
titlebar.Parent = form;
titlebar.Controls.Add(pnlexit);
titlebar.MouseDown += Titlebar_MouseDown;
lblexit.Text = "X";
lblexit.Font = new Font("Arial", 9.5f, FontStyle.Bold);
lblexit.TextAlign = ContentAlignment.MiddleCenter;
lblexit.Dock = DockStyle.Fill;
lblexit.MouseClick += lblexit_click;
pnlexit.Height = titlebar.Height - 10;
pnlexit.Width = 30;
pnlexit.BackColor = ColorTranslator.FromHtml("#CDDC39");
pnlexit.Location = new Point(titlebar.Width - 33, 0);
pnlexit.Parent = titlebar;
pnlexit.Controls.Add(lblexit);
lbltitle.Parent = titlebar;
lbltitle.Text = title;
lbltitle.ForeColor = ColorTranslator.FromHtml("#CDDC39");
lbltitle.Font = new Font("Arial", 9.5f, FontStyle.Bold);
lbltitle.Location = new Point(5, 3);
label.AutoSize = true;
label.Location = new Point(25, 50);
label.Parent = form;
label.ForeColor = Color.White;
textBox.Parent = form;
textBox.Location = new Point(25, 65);
textBox.Width = 300;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
Console.WriteLine("Value = " + value);
return dialogResult;
}
private void Titlebar_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
private void ButtonCancel_MouseClick(object sender, MouseEventArgs e)
{
// throw new NotImplementedException();
}
private void lblexit_click(object sender, MouseEventArgs e)
{
form.Close();
}