So, I made a button that is draggable with these events:
bool isMouseClicked;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isMouseClicked = true;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseClicked == true)
//You then set your location of your control. See below:
button1.Location = new Point(MousePosition.X, MousePosition.Y);
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isMouseClicked = false;
}
Now, I want to insert something to the "Click" event. When I try to drag the button, the action from the "Click" event is performed. I only want the button to move when I drag it and to perform the action from the "Click" event when I click it. How can I do this?
I would suggest your bool should be named isMouseDown. Now, the Click event fires when a MouseDown and then MouseUp event occurs on the same control. So add another bool justDragged, set it to false on MouseDown, set it to true on MouseMove and isMouseDown and test it in the Click event.
BTW, this information is on SO, just a bit scattered around.
bool isMouseDown = false;
bool justDragged = false;
private void button1_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
isMouseDown = true;
justDragged = false;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e) {
if (isMouseDown) {
//You then set your location of your control. See below:
button1.Location = new Point(MousePosition.X, MousePosition.Y);
justDragged = true;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e) {
isMouseDown = false;
}
private void button1_Click(object sender, MouseEventArgs e) {
if (!justDragged) { // then we clicked
// do click handling
}
}
Related
customBtn1 is the custom scrollbar. The below code sample works, but the scrolling action is jittery and stuttery. It does not scroll smoothly. Any idea why this could be happening and how to fix it?
int PreviousBarLoc;
private void MainForm_Load(object sender, EventArgs e)
{
PreviousBarLoc= customBtn1.Location.Y;
}
//move the scrollbar up and down when the user drags it
private void customBtn1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{ MouseDownLocation = e.Location; }
}
private Point MouseDownLocation;
private void customBtn1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
customBtn1.Top = e.Y + customBtn1.Top - MouseDownLocation.Y;
}
}
//scroll the panel
private void customBtn1_LocationChanged(object sender, EventArgs e)
{
int locDifference = customBtn1.Location.Y - PreviousBarLoc;
if (steamSrvListPnl.VerticalScroll.Value + locDifference <= 255 && steamSrvListPnl.VerticalScroll.Value + locDifference >= 0)
{
steamSrvListPnl.VerticalScroll.Value += locDifference;
}
PreviousBarLoc= customBtn1.Location.Y;
}
Your deltas are not quite right. The way it is currently written, if you drag the mouse down and then start dragging back up, it will not scroll back up because your current e.Y is still below the original MouseDownLocation.Y. I'm not entirely sure of your setup, but below is an example of how to get smooth scrolling to work when the left mouse button is pressed and dragged on a button:
private int _prevY = 0;
private bool _mouseDown = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_mouseDown = true;
_prevY = e.Y;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
panel1.VerticalScroll.Value = Math.Max(panel1.VerticalScroll.Minimum, Math.Min(panel1.VerticalScroll.Maximum, panel1.VerticalScroll.Value + e.Y - _prevY));
_prevY = e.Y;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
if (_mouseDown)
{
_mouseDown = false;
_prevY = 0;
}
}
I want to hide the cursor after a certain time (if the mouse is not moving), and I want to show the cursor when I move the mouse in a picturebox. I just can't get it to work... This is what I have tried:
// this Never seem to hide the cursor
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Show();
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Hide();
tim.Stop();
}
-
// works but in this case I want cursor.ico to be a resource
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor("cursor.ico");
tim.Stop();
}
-
// Properties.Resources.cursor gives an error even though I added it to my resources
// cannot convert from 'System.Drawing.Icon' to 'System.IntPtr'
private void picBox_MouseMove(object sender, MouseEventArgs e)
{
Cursor.Current = Cursors.Default;
tim.Stop();
tim.Start();
}
private void tim_Tick(object sender, EventArgs e)
{
Cursor.Current = new Cursor(Properties.Resources.cursor);
tim.Stop();
}
You need to have a timer and handle its Tick event. In the Tick event, check if the last movement of the mouse was before the certain time, then hide the cursor using Cursor.Hide(). Also handle MouseMove of the PictureBox and show the cursor using Cursor.Show() method.
Note: Don't forget to enable the timer and set Interval of timer to a short value, for example 300 and change duration value in the following code, for a shorter/longer inactive time:
DateTime? lastMovement;
bool hidden = false;
int duration = 2;
void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
lastMovement = DateTime.Now;
if (hidden)
{
Cursor.Show();
hidden = false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (!lastMovement.HasValue)
return;
TimeSpan elaped = DateTime.Now - lastMovement.Value;
if (elaped >= TimeSpan.FromSeconds(duration) && !hidden)
{
Cursor.Hide();
hidden = true;
}
}
How can I change a button's style, in particular the image, upon the mouse being pressed and held and then the cursor dragged away from the button?
You'll notice that, on this action, the default behaviour for the button is to revert it's style to hover style. This can be partially configured using the MouseOverBackColor. I want to ensure that, whenever the MouseOverBackColor is applied, I also have a specific image on the button.
I have tried the code below, to have an "isMouseDown" flag which is checked in the leave event. However, this doesn't work for me.
private void btnFormMinimize_MouseClick(object sender, MouseEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
bool isMouseDown = false;
private void btnFormMinimize_MouseDown(object sender, MouseEventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_click;
isMouseDown = true;
}
private void btnFormMinimize_MouseEnter(object sender, EventArgs e)
{
Button b = (Button)sender;
b.Image = Properties.Resources.icon_minimize_hover;
}
private void btnFormMinimize_MouseLeave(object sender, EventArgs e)
{
Button b = (Button)sender;
if (isMouseDown)
{
b.Image = Properties.Resources.icon_minimize_hover;
}
else
{
b.Image = Properties.Resources.icon_minimize;
}
}
private void btnFormMinimize_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown= false;
}
Thanks in advance.
I want to drag panels from one tableLayoutPanel to another. I also want the panels to be copied, not moved; that is, I want them to be copied (from tableLayoutPanel1 to tableLayoutPanel2), leaving the item in tableLayoutPanel1.
Can I do this? If you can give me an idea, it will be great. Thank you
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel2.AllowDrop = true;
panel3.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel2.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel2.DragDrop += panel_DragDrop;
}
private void panel3_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel3_MouseMove(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Panel)e. Data . GetData(typeof(Panel))).Parent = (Panel)sender;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
You need to create a new panel in the MouseDown handler, set its properties like those of the original, add it to the form and use DoDragDrop with this new panel.
I createbtn1_MouseDown Event.
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
btn1.BackgroundImage = Properties.Resources.click;
}
I want to disable that btn1_MouseDownt Event behind this RadioButton when it checked. I mean to say when Radiobutton checked then this btn1_MouseDown event should not work.
private void r1_CheckedChanged(object sender, EventArgs e)
{
if (r1.Checked == true)
clock = 5;
radioButton2.Enabled = false;
radioButton3.Enabled = false;
radioButton4.Enabled = false;
}
Is that possible ?
inside r1_checkeChanged add:
btn1.MouseDown -=btn1_MouseDown;
You can remove the event:
btn1.MouseDown -= btn1_MouseDown
Of course don't forget to add it back again when you need it.
However, instead of doing that I would change your event handler's code to check the state of the radio button:
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (!r1.Checked)
btn1.BackgroundImage = Properties.Resources.click;
}
1st option can be to remove the event at radio button check_change event like this:
btn1.MouseDown -=btn1_MouseDow;
but i dont prefer this way.
Second is to add a flag at start of this event and check whether to execute the event or not
like this:
bool flag;
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
if (flag) return;
btn1.BackgroundImage = Properties.Resources.click;
}