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();
}
Related
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;
}
I have bunch of buttons on my form. And I would like to make it a bit nicer so button changing color and font to bold when mousei s over it seems like good idea. I would appreciate any help
button.BackColor = Color.Cyan;
button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);
EDIT:
this is working for me:
private void button1_MouseEnter(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.PaleTurquoise;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Bold);
}
private void button1_MouseLeave(object sender, EventArgs e)
{
((Button)sender).BackColor = Color.WhiteSmoke;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, FontStyle.Regular);
}
(there is button1_mousenter (or mouseleave) set as action for every button
Just select them all in your Form view and go to the mousehover event.
and write your code like this:
private void button_mousehover (object sender, EventArgs e)
{
((Button)sender).BackColor = Color.Cyan;
((Button)sender).Font = new Font(((Button)sender).Font.Name, ((Button)sender).Font.Size, ((Button)sender).FontStyle.Bold;
}
You can add MouseEnter and MouseLeave events to your buttons that changes the buttons' colors.
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseenter(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseleave(v=vs.110).aspx
// bind handler to MouseEnter Event
this.yourButton1.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);
this.yourButton2.MouseEnter += new System.EventHandler(this.allButtons_MouseEnter);
// bind handler to MouseLeave Event
this.yourButton1.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);
this.yourButton2.MouseLeave += new System.EventHandler(this.allButtons_MouseLeave);
// enter handler
private void allButtons_MouseEnter(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = Color.Cyan;
btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Bold);
}
// leave handler
private void allButtons_MouseLeave(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = Color.DeepPink; // whatever your original color was
btn.Font = new Font(btn.Font.Name, btn.Font.Size, FontStyle.Regular);
}
I have a Form with a LayoutPanel that has dynamically added buttons inside. The buttons are added at runtime, which works, but my problem is I'd like to set the properties to one of the buttons to disabled if a textBox is empty and enable it when it the textBox is not empty.
Here is a code example, with the error I am receiving below:
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button1";
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
CS0103 The name 'button1' does not exist in the current context
Should I be declaring the buttons elsewhere so the entire code can see that they do in fact exist or is my problem elsewhere? Thanks.
You must declare a Button outside Form1_Load if you want to access the button with name directly from other methods, because in your case the buttons are just available in the Form1_Load method :
Button button1;
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
button1 = new Button();
button1.Name = "button1";
tableLayoutPanel1.Controls.Add(button1, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
button1.Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
or if you want declare Buttons inside Form1_Load, you can access to Button like this :
private void textBox1_TextChanged(object sender, EventArgs e)
{
var btn = tableLayoutPanel1.Controls.OfType<Button>().Where(x => x.Name == "button1").FirstOrDefault();
(btn as Button).Enabled = !string.IsNullOrEmpty(textBox1.Text);
}
As an alternative, you could dynamically get the control from the Layout Panel and set the enabled property like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
Control button = tableLayoutPanel1.Controls["button1"];
button.Enabled = string.IsNullOrEmpty(textBox1.Text) ? false : true;
}
This approach is not as "safe" as declaring the buttons at form level, but I thought it would be useful to mention for times when you need to be genuinely dynamic in referencing controls.
There are a couple ways you can do this, one would be to use the Find method of the TableLayoutPanel's Controls Collection like this.
private void textBox1_TextChanged(object sender, EventArgs e)
{
Button btn =(Button)tableLayoutPanel1.Controls.Find("button1", true)[0];
if (string.IsNullOrEmpty(textBox1.Text))
{
btn.Enabled = false;
}
else
{
btn.Enabled = true;
}
}
The second would be to use the buttons Tag Property to determine which control to use, I have used this in the past for dynamically generated controls.
private void Form1_Load(object sender, EventArgs e)
{
// Create 3 buttons
Button button1 = new Button();
button1.Name = "button1";
button1.Tag = 1; //note the Tag property being used
tableLayoutPanel1.Controls.Add(button1, 0, 0);
Button button2 = new Button();
button1.Name = "button2";
button2.Tag = 2;
tableLayoutPanel1.Controls.Add(button2, 0, 0);
Button button3 = new Button();
button3.Name = "button3";
button3.Tag = 3;
tableLayoutPanel1.Controls.Add(button3, 0, 0);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
foreach (Control c in tableLayoutPanel1.Controls) //iterate through controls
{
if ((int)c.Tag == 1) //if Tag is equal then process
{
if (c is Button)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
((Button)c).Enabled = false;
}
else
{
((Button)c).Enabled = true;
}
break; //if you have multiple controls to process remove this
} //and assign the same tag to the controls you want processed
}
}
}
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
When I click a button I want to delete that particular flowlayout panel along with the check box and the button itself.But I have no clue how to do this.
Here is my code to do this:
private static CheckBox _taskCompletionCheckBox;
public static void DisplaySingleTask(LayoutType layoutType, FlowLayoutPanel layoutPanel,TodoItem item)
{
//creates a panel
var parentPanel = new FlowLayoutPanel {Parent = layoutPanel, AutoSize = true, BorderStyle = BorderStyle.FixedSingle};
//Based on layout type, the panel's content's are determined
switch (layoutType)
{
case LayoutType.Small:
_taskCompletionCheckBox = new CheckBox {Parent = parentPanel, Dock = DockStyle.Left,Text = item.Name,AutoSize = true,BackColor = Color.Transparent};
_taskCompletionCheckBox.CheckedChanged += checkBox_CheckedChanged;
_taskCompletionCheckBox.Show();
var delBtn = new Button { Parent = parentPanel, Dock = DockStyle.Left, Size = new Size(30, _taskCompletionCheckBox.Size.Width),Image = Resources.DeleteTaskImage};
delBtn.Click += delBtn_Click;
break;
case LayoutType.Normal:
break;
case LayoutType.Full:
break;
default:
throw new ArgumentOutOfRangeException("layoutType");
}
}
static void delBtn_Click(object sender, EventArgs e)
{
//I would like to know how can I get a reference to the the flowlayout panel here so I can call the dispose() method on it.
}
If I have got your point, the FlawLayoutPanel you are looking for is parent of the button. Cast the sender parameter of the event handler to button and get it's parent.
static void delBtn_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
FlowLayoutPanel panel = (FlowLayoutPanel)button.Parent;
// ..
}