Dock Panel on Button Click C# - c#

I'm using WinForms. In my form i have 2 panels which i want to dock up or down on button click.
The issue i'm running into is that my panels is not docking correctly.
When i click on the up button, panel one label gets covered by panel2.
Panel 1: (Anchor: Top, Left, Right)
Panel 2: (Anchor: Top, Bottom, Left, Right)
private void Up_Btn_Click(object sender, EventArgs e)
{
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel2.Dock = System.Windows.Forms.DockStyle.Top;
}
private void Down_Btn_Click(object sender, EventArgs e)
{
panel1.Dock = System.Windows.Forms.DockStyle.Fill;
panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
}
Incorrect the label should not be covered by the panels
What supposed to happen when Up button is clicked
What supposed to happen when Down button is clicked

public Form1()
{
InitializeComponent();
panel1.BringToFront();
}
private void Up_Click(object sender, EventArgs e)
{
panel1.Dock = DockStyle.Fill;
panel2.Dock = DockStyle.Top;
}
private void Down_Click(object sender, EventArgs e)
{
panel1.Dock = DockStyle.Fill;
panel2.Dock = DockStyle.Bottom;
}
The trick is to correct the order of the controls.
See here: Docking multiple controls - one fills remaining space

Related

How to remove panels from button C#

I have created a function that creates a new panel each time a button is pressed, alongside with a button, which should remove the entire panel when pressed.
Here is my code for creating the panels and the button :
Panel panel;
private void button1_Click(object sender, EventArgs e)
{
panel = new Panel();
panel.BackColor = Color.FromArgb(38, 38, 38);
panel.Margin = new System.Windows.Forms.Padding(10);
flowLayoutPanel1.Controls.Add(panel);
panel.Show();
Button delbutton = new Button();
delbutton.Text = "X";
flowLayoutPanel1.Controls.Add(delbutton);
delbutton.Click += new EventHandler(this.ButtonFunction_Click);
}
Considering that every panel created has this delbutton , how can i remove the panel of which delbutton button was pressed?
I tried to add this method to the button, but it removes panel randomly :
void ButtonFunction_Click (Object sender,EventArgs e)
{
foreach (Control controlObj in flowLayoutPanel1.Controls)
{
flowLayoutPanel1.Controls.Remove(controlObj);
controlObj.Dispose();
}
}
You can add the relevant Panel as a Tag property of its Button, then you can get that in the Click handler
private void button1_Click(object sender, EventArgs e)
{
var panel = new Panel
{
BackColor = Color.FromArgb(38, 38, 38),
Margin = new Padding(10),
};
flowLayoutPanel1.Controls.Add(panel);
panel.Show();
var delbutton = new Button
{
Text = "X",
Tag = panel,
};
delbutton.Click += ButtonFunction_Click;
flowLayoutPanel1.Controls.Add(delbutton);
}
private void ButtonFunction_Click(Object sender, EventArgs e)
{
var button = (Button)sender;
((Control)button.Tag).Dispose();
button.Dispose();
}

How to make button a invisible when other buttons are clicked

I'm creating a new project with Guna Framework UI
I set another button(btnMove) behind the first button "Dashboard" that moves to the new button location that gets clicked in that left panel.
I want to make the btnMove to be invisible when I click buttons from the right panel.
These are my codes
//moving disabled button across buttons in the same panel when clicked
private void MoveButton(object sender)
{
Guna2Button b = (Guna2Button)sender;
btnMove.Visible = true;
btnMove.FillColor = Color.White;
btnMove.Animated = true;
btnMove.Location = new Point(b.Location.X + 24, b.Location.Y + 1);
btnMove.SendToBack();
}
//end here
private void guna2Button1_CheckedChanged(object sender, EventArgs e)
{
//Calling the move function
MoveButton(sender);
}
I tried to set the btnMove to be invisible only when I click on the buttons on the right panel but it doesn't seem to work
private void guna2Button3_Leave(object sender, EventArgs e)
{
guna2Button1.Checked = false;
btnMove.Visible = false;
}

Is there a way to toggle/fix a menu while scrolling?

I want to build a Windows Forms App that has a menu (several labels) on it's left side which is toggled. On the right side there should be some columns i can scroll through. Jst like Excel with it's fixed rownumbers.
Is there a way to do this? Preferably an easy one.
I think you can use two panels to make the form like the picture you provided.
The following code is a code example and you can refer to it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ScrollBar hScrollBar1 = new HScrollBar();
private void Form1_Load(object sender, EventArgs e)
{
panel1.BorderStyle = BorderStyle.FixedSingle;
panel1.Dock = DockStyle.Left;
panel2.BorderStyle = BorderStyle.FixedSingle;
panel2.Dock = DockStyle.Fill;
hScrollBar1.Dock = DockStyle.Bottom;
hScrollBar1.Scroll += new ScrollEventHandler(hScroller_Scroll);
panel2.Controls.Add(hScrollBar1);
panel2.HorizontalScroll.Visible = false;
panel2.HorizontalScroll.Enabled = true;
}
private void hScroller_Scroll(object sender, ScrollEventArgs e)
{
panel2.HorizontalScroll.Value = e.NewValue;
}
}
The specific result:

how to show icon inside a button windowsForms

I want to add icon inside a button. Here is my code
private void Printbutton_Click(object sender, EventArgs e)
{
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
if (SetupThePrinting())
printDocument1.Print();
}
The problem here is that the icon doesn't appear at first , it appears when I click to the button.
What's wrong here ?
you added the icon in printbutton_click event instead defining it in Form initializecomponents
public Form2()
{
InitializeComponent();
// Assign an image to the button.
Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
// Align the image and text on the button.
Printbutton.ImageAlign = ContentAlignment.MiddleRight;
Printbutton.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
Printbutton.FlatStyle = FlatStyle.Flat;
}
private void Printbutton_Click(object sender, EventArgs e)
{
if (SetupThePrinting())
printDocument1.Print();
}
Like this
public Form1()
{
InitializeComponent();
// Assign an image to the button.
button1.Image = Image.FromFile("C:\\Yourfolder");
// Align the image and text on the button.
button1.ImageAlign = ContentAlignment.MiddleRight;
button1.TextAlign = ContentAlignment.MiddleLeft;
// Give the button a flat appearance.
button1.FlatStyle = FlatStyle.Flat;
}
private void button1_Click(object sender, EventArgs e)
{
// Your print code.
}

MouseLeave detection not working with ImageForm

I've got a smaller image in my form. When the user hovers over the image it brings up a larger view of the image (that follows the mouse, but stays a certain distance from the mouse). In order to do this I am generating a Form with no FormBorderStyles when the cursor hovers the image.
The problem I'm running into is that the first form doesn't seem to detect any longer that the mouse is hovering or leaving the PictureBox once the form activates. The Form also doesn't follow the cursor.
Here is the slimmed down version of what I've got:
C#
bool formOpen = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseHover += pictureBox1_MouseHover;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
if (formOpen == false)
{
Form form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form.BackColor = Color.Orchid;
//Show the form
form.Show();
form.Name = "imageForm";
this.AddOwnedForm(form);
//Set event handler for when the mouse leaves the image area.
//form.MouseLeave += form_MouseLeave;
//Set the location of the form and size
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
this.TopMost = true;
formOpen = true;
form.Location = new Point(Cursor.Position.X, Cursor.Position.Y);
}
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
//MessageBox.Show("Worked");
}
}
The MouseLeave (and other events) was not recognized because the opening of the popup window and especially making it topmost=true took away the focus from the original form and its PictureBox.
It also didn't move because not code for moving was provided..
Here are a few changes that will make the form move:
You need a reference to it at the form1 level
you need to move it in the MouseMove event
Note that Hover is a once-only type of event. It fires only once until you leave the control.. (Note: Setsu has switched from Hover to Enter. This works fine, but lacks the short delay before showing the 2nd Form. If you want that back you can either switch back to Hover or you can fake the hover delay by a Timer, which is what I often do.)
// class level variable
Form form;
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.MouseEnter += pictureBox1_MouseEnter;
pictureBox1.MouseLeave +=pictureBox1_MouseLeave;
pictureBox1.MouseMove += pictureBox1_MouseMove; // here we move the form..
}
// .. with a little offset. The exact numbers depend on the cursor shape
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if ((form != null) && form.Visible)
{
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
}
}
void pictureBox1_MouseEnter(object sender, EventArgs e)
{
// we create it only once. Could also be done at startup!
if (form == null)
{
form = new Form();
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
//form.BackColor = Color.Orchid;
form.Name = "imageForm";
this.AddOwnedForm(form);
form.BackColor = Color.Black;
form.Dock = DockStyle.Fill;
form.Size = new Size(30, 30);
form.BackgroundImageLayout = ImageLayout.Center;
//this.TopMost = true; // wrong! this will steal the focus!!
form.ShowInTaskbar = false;
}
// later we only show and update it..
form.Show();
form.Location = new Point(Cursor.Position.X + 5, Cursor.Position.Y + 5);
// we want the Focus to be on the main form!
Focus();
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
if (form!= null) form.Hide();
}
MouseHover = Occurs when the mouse pointer rests on the control. (msdn)
Try MouseMove instead.

Categories

Resources