I have two panels in my winform. panel1 is blue, and panel2 is red. I would like to know if there is a function that tells what panel i clicked on, otherwise then using private void panel1_Click(object sender, EventArgs{} function. Thank you, here is my code:
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
Control control = this.GetChildAtPoint(e.Location);
if (control is Panel)
{
Panel clickedPanel = (Panel)control;
if (clickedPanel.Name.Equals("panel1"))
{
MessageBox.Show("you clicked on blue panel");
}
else
{
MessageBox.Show("you clicked on red panel");
}
}
}
This function doesn't work, and I don't know what to do.
I tried many functions, but i can't search for any "universal" function, that tells what object i clicked on.
You have attached a function for the form click event handler; this will handle mouse click events on the form and not on the panel. If you can view the Panels on the designer, then you can double-click each and the IDE will auto generate the method that will handle the mouse click event for each Panel.
Alternatively, you can add the event handlers for each Panel in the constructor of the form like below:
public class MyForm: Form{
public MyForm(){
InitializeComponent();
//register the event listeners for the panels
panel1.MouseClick += panel1_Clicked;
panel2.MouseClick += panel2_Clicked;
}
//implement the methods
private void panel1_Clicked(object sender, MouseEventArgs e){
//handle the click for panel1
}
private void panel2_Clicked(object sender,
MouseEventArgs e){
//handle the click for panel2
}
}
Related
So I have built an application in C# using Winforms and my application uses a few different buttons. I'd like to have a highlight on the button that has been clicked to show what 'tab' you're in.
I've tried doing the following;
// BUTTONS //
private void dashboard_btn_Click(object sender, EventArgs e)
{
// Load Form
OpenChildForm(new FormDashboard());
dashboard_btn.FlatAppearance.BorderColor = Color.Red;
dashboard_btn.FlatAppearance.BorderSize = 1;
}
However, this of course doesn't work nicely since it adds a border around the button but when I click another button the border also stays around the previous button.
How would you implement a feature to add a border around the button that get's clicked but have the border disappear after you click another button?
Thank you for any feedback!
EDIT:
I've implemented Jimi's advice and used the Leave event to change the border around the button back to 0. However I'm not sure how to implement this in a global way so all my buttons are subscribed to this event.
My code now looks like this;
// BUTTONS //
private void dashboard_btn_Click(object sender, EventArgs e)
{
// Load Form
OpenChildForm(new FormDashboard());
// Button Highlight
dashboard_btn.FlatAppearance.BorderColor = Color.Red;
dashboard_btn.FlatAppearance.BorderSize = 1;
}
// BUTTON REMOVE HIGHLIGHT //
private void dashboard_btn_Leave(object sender, EventArgs e)
{
dashboard_btn.FlatAppearance.BorderSize = 0;
}
EDIT 2:
I ended up using Jimi's example and this worked for me :)
This might lend itself to a RadioButton style functionality because clicking a different radio button in the same container will uncheck the others. So, to implement the "generalized approach" that you mention in your comment, you could make a simple custom RadioButtonEx class where the Appearance property is set to Button then change your border style when the Checked property changes. In this example, the Click event has been changed to static so that clicking on any button directs the event to the common onAnyClick method.
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
RadioButtonEx.Click += onAnyClick;
}
private void onAnyClick(object sender, EventArgs e)
{
label1.Text = ((RadioButtonEx)sender).Text;
}
}
public class RadioButtonEx : RadioButton
{
public static new event EventHandler Click;
public RadioButtonEx()
{
FlatAppearance.BorderColor = Color.Red;
FlatAppearance.BorderSize = 1;
Appearance = Appearance.Button;
}
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
if(Checked)
{
FlatStyle = FlatStyle.Flat;
Click?.Invoke(this, EventArgs.Empty);
}
else
{
FlatStyle = FlatStyle.Standard;
}
}
}
The question is this:
when the mouse cursor moved on the button some thing should be happen but I don't know what exactly have to write
When you select the button in the VS-designer you will have access to the properties and events (lightning Icon in the property window).
In the events-listing are all events that the button can fire. May be for your purpose the events: ´MouseEnter´ and ´MouseLeave´ would be a good choice. Just double click the event and Visual Studio will generate the appropriate method. Like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Green;
}
In my example I just change the backcolour of the button when the mouse is on the button and change it again when it leaves the button.
Practically you could run any code inside the generated method.
You can create eventHandler like this :
myButton.MouseMove += new MouseEventHandler(doSomething);
Where myButton is the button from which you want to trigger the event when mouse moves over it. and doSomething() is the method defined as like the following:
public void doSomething(object sender, MouseEventArgs e)
{
// do what ever you want
}
I have a parent panel in a form.
This panel will display an array of different user control which will take up the full panel dimension.
I tried to use the panel 'Click' event. However when the user control is added to the panel, it does not fire the event when click.
Due to there are many widget in each user control, it would be tedious to implement 'Click' on each widget.
Is there anyway when i click on a user control, it fires the form panel event instead?
I suggest to loop the panel's controls on the form loading:
private void MyClick(object sender, EventArgs e) {
...
}
private void MyForm_Load(object sender, EventArgs e) {
foreach (Control control in myPanel.Controls)
control.Click += MyClick;
...
}
I have the following problem: I have a panel which has a specific color, say red.
When the user presses his mouse, the color of this panel gets stored in a variable. Then the user moves, his mouse still pressed, over to another panel. When he releases the mouse there, this panel should get the background color of the first that had been stored in the variable. My code looks something like this:
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
I need to identify the sender first because there are many possible panels that can trigger this event. The first MouseDown method works totally fine, the color is stored nicely in the variable. The secon one however doesn't even get triggered when the user does what I described above. When the ser clicks on the second panel, it works (there is an MouseUp part in a click aswell I guess).
What's wrong here? Why is the event not triggered when the user holds the mouse key down before?
(This answer assumes you are using Windows Forms.)
It could be that you need to capture the mouse by setting this.Capture = true in the MouseDown of the source control. (See Control.Capture)
If you did that, the source window would get the MouseUp event, and it would be the source window that had to determine the destination window under the mouse coords. You can do that using Control.GetChildAtPoint() (see this answer on Stack Overflow).
Use Windows Forms Drag and Drop Support Instead! <- Click for more info
I'm going to suggest you bite the bullet and use the .Net Drag and Drop methods to do this. It requires some reading up, but it will be much better to use it.
You start a drag in response to a MouseDown event by calling Control.DoDragDrop().
Then you need to handle the Control.DragDrop event in the drop target control.
There's a few more things you might need to do to set it up; see the Control.DoDragDrop() documentation for an example.
(For WPF drag and drop support, see here.)
when your mouse enter the target control , mouse down triggerd ang get target BackColor! you need add an boolean flag to your code :
public Color currentColor;
bool flag=false;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
if(flag==false)
{
flag=true
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
}
//assume mouse up for panles
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
if(flag==true)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
flag=flase;
}
}
and also you need change your flag in mouseMove( if )
As I mentioned in my comment Mouse Events are captured by the originating control, You would probably be better off using the DragDrop functionality built into Windows Forms. Something like this should work for you. I assigned common event handlers, so they can be assigned to all of your panels and just work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
((Control)sender).DoDragDrop(((Control)sender).BackColor,DragDropEffects.All);
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Control)sender).BackColor = (Color)e.Data.GetData(BackColor.GetType());
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I know it's an old question but I had the same issue and none of the above answers worked for me. In my case I had to handle the MouseMove event in the target control and check for the mouse to be released. I did set 'BringToFront' on my target panel just in case that helped at all.
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void panelTarget_MouseMove(object sender, MouseEventArgs e)
{
//the mouse button is released
if (SortMouseLocation == Point.Empty)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
}
I was on my way to create a custom Progress Bar when I hit this problem [WinForm]
**The Structure:**
-panel
-> panel
so I have panel which inside the panel, there another panel.
**The Goals:**
-I want to use my parent panel as all event handler,
while make the child panel have no event at all.
**The Problem:**
- when I press my mouse inside the child panel. the event in parent wont called.
explanation : -> I still wanted to call parent panel mouse down
even if I click on top of my child panel.
So you want the click event to fire on the parent Panel when the child panel is clicked.
I can think of 2 ways you can do this.
First way is to simply call the Click event handler method for Panel1 from inside the Panel2's Click event handler method:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("Panel 1 clicked.");
}
private void panel2_Click(object sender, EventArgs e)
{
this.panel1_Click(sender, e);
}
Probably a better way would be to register both click events to the 1 handler method:
private void panel1_Click(object sender, EventArgs e)
{
MessageBox.Show("Panel 1 clicked.");
}
Then from the Form Designer or manually register the event for the second panel:
this.panel2.Click += new System.EventHandler(this.panel1_Click);