How to Show ContextMenuStrip Above the Mouse Postion? - c#

I'm trying make a contextMenuStrip with Notify Icon, But I can't put the location of this Context above the mouse Position. it's show at same position mouse
Context menu strip position Image
private void ntfy2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(Cursor.Position.X, Cursor.Position.Y); //Show at Postion Mouse
}
}

Does this code help you?
private void ntfy2_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var relativeClickedPosition = e.Location;
var screenClickedPosition = (sender as Control).PointToScreen(relativeClickedPosition);
contextMenuStrip1.Show(screenClickedPosition);
}
}
#DuckFterminal if my post helped you enough, please click on bird of acceptance, ok? :)

Related

Mouse right in textbox

I everyone,
I have development this application, ... my objective is user, if click on right side of the mouse the url is paste to textbox.
MenuStrip
But when i click in right side, appear the menustrip of textbox. My question is I can disable this "menustrip"?!?
I send my code at the moment:
private void TxtUrl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
TxtUrl.Paste();
}
This should work for your code:
private MouseButtons e_Button = new MouseButtons();
private void TxtUrl_MouseDown(object sender, MouseEventArgs e)
{
e_Button = e.Button;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
TxtUrl.Paste();
}
private void cms_Opening(object sender, CancelEventArgs e)
{
if (e_Button == System.Windows.Forms.MouseButtons.Right)
e.Cancel = true;
}

How we move a label when mouse click on it and when mouse key up then stop moving inside a group box

When is try with code, there appear two label and when move, screen become white from where they move. I want single label move with mouse move.
bool mDown = false;
private void label13_MouseMove(object sender, MouseEventArgs e)
{
if (mDown)
{
label13.Location = e.Location;
}
}
private void label13_MouseDown(object sender, MouseEventArgs e)
{
mDown = true;
}
private void label13_MouseUp(object sender, MouseEventArgs e)
{
mDown = false;
}
The e.Location gives you a mouse position relative to the control that is being clicked. So to fix that, instead of
label13.Location = e.Location;
use
var pos = this.PointToClient(Cursor.Position);
label13.Location = new Point(pos.X - offset.X, pos.Y - offset.Y);`
Create the offset variable as a property of the form (type Point) and initialize it on the mouse down event:
offset = e.Location;

MouseMove doesn't get the right coordinate points

I have a label I am trying to drag. I click on the label, and on the MouseMove() event I am trying to relocate the position of the label.
public void MyLabel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Label)sender).Location = Cursor.Position;
// I have also tried e.Location but none of these moves the label to
// where the the cursor is, always around it, sometimes completely off
}
}
You usually need to store the offset location of the initial mouse down point in the control, or else the control will move on you in a jittering fashion. Then you just do the math:
Point labelOffset = Point.Empty;
void MyLabel_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
labelOffset = e.Location;
}
}
void MyLabel_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Label l = sender as Label;
l.Location = new Point(l.Left + e.X - labelOffset.X,
l.Top + e.Y - labelOffset.Y);
}
}

C# Drag and Drop from one Picture box into Another

I'm working in visual studio 2012 with C# and I need to Drag a Picture box into another picture box, basically replace the target Picturebox Image with the Dragged Picture box image.
How do I do this?
Please be specific and try to explain as simplest and as best as possible.
I'm extremely new to programming, and a bit desperate so please be patient with me.
Drag+drop is hidden on the PictureBox control. Not sure why, it works just fine. The probable guidance here is that it will not be obvious to the user that you could drop an image on the control. You'll have to do something about that, at least set the BackColor property to a non-default value so the user can see it.
Anyhoo, you'll need to implement the MouseDown event on the first picturebox so you can click it and start dragging:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) {
var img = pictureBox1.Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move) {
pictureBox1.Image = null;
}
}
I assumed you wanted to move the image, tweak if necessary if copying was intended. Then you'll have to implement the DragEnter and DragDrop events on the second picturebox. Since the properties are hidden, you should set them in the form's constructor. Like this:
public Form1() {
InitializeComponent();
pictureBox1.MouseDown += pictureBox1_MouseDown;
pictureBox2.AllowDrop = true;
pictureBox2.DragEnter += pictureBox2_DragEnter;
pictureBox2.DragDrop += pictureBox2_DragDrop;
}
void pictureBox2_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
void pictureBox2_DragDrop(object sender, DragEventArgs e) {
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
This does allow you to drag an image from another application into the box. Let's call it a feature. Use a bool flag if you want to disallow this.
Hans's answer led me to the correct solution. The problem with that answer is that putting DoDragDrop inside MouseDown will prevent MouseClick events from firing.
Here's my solution:
private void PictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var pb = (PictureBox)sender;
if (pb.BackgroundImage != null)
{
pb.DoDragDrop(pb, DragDropEffects.Move);
}
}
}
private void PictureBox_DragEnter (object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void PictureBox_DragDrop (object sender, DragEventArgs e)
{
var target = (PictureBox)sender;
if (e.Data.GetDataPresent(typeof(PictureBox)))
{
var source = (PictureBox)e.Data.GetData(typeof(PictureBox));
if (source != target)
{
// You can swap the images out, replace the target image, etc.
SwapImages(source, target);
}
}
}
Full working example on my GitHub.
You can use mouse enter and leave events to do this easily. For example you have two picture boxes pictureBox1 and pictureBox2. And you want to drag the image from picture box1 and drop it onto picture box2 do somthing like this.
private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
if (a == 1)
{
pictureBox1.Image = pictureBox2.Image;
a = 0;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
a = 1;
}
Where 'a' is just a lock or key which checks whether the mouse has entered the control on which we want to drop this image on. Hope it helped, worked for me.
You can't set AllowDrop on PictureBox...set it for your whole form.
Code Snippet
Form1.AllowDrop = true;
Use the Form DragEnter, DragDrop events, they will work even if you drop it over the pictureBox.
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
int x = this.PointToClient(new Point(e.X, e.Y)).X;
int y = this.PointToClient(new Point(e.X, e.Y)).Y;
if(x >= pictureBox1.Location.X && x <= pictureBox1.Location.X + pictureBox1.Width && y >= pictureBox1.Location.Y && y <= pictureBox1.Location.Y + pictureBox1.Height)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
pictureBox1.Image = Image.FromFile(files[0]);
}
}

Double click to Windows form in C#

How can I detect, which mouse button have double clicked the form i.e. Left, Right or Middle?
Updated:
I am using .NET2.0
Store the last clicked button in MouseUp event and then check that in the double click event. Sample code:
MouseButtons _lastButtonUp = MouseButtons.None;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_lastButtonUp = e.Button;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
switch (_lastButtonUp)
{
case System.Windows.Forms.MouseButtons.Left:
MessageBox.Show("left double click");
break;
case System.Windows.Forms.MouseButtons.Right:
MessageBox.Show("right double click");
break;
case System.Windows.Forms.MouseButtons.Middle:
MessageBox.Show("middle double click");
break;
}
}
Have a look at MouseDoubleClick and MouseEventArgs and MouseButtons Enumeration
MouseDoubleClick is one of the Form events.
In Whatever_Click or DoubleClick event, you can check the MouseEventArgs e, which contains what key was pressed.
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Console.WriteLine("Left Mouse Button was clicked!");
else if (e.Button == MouseButtons.Middle)
Console.WriteLine("Middle Mouse Button was clicked!");
}
Other buttons include MouseButtons.Right, MouseButtons.Left
in form_MouseDoubleClick event you can trace
void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Do Operation
}
}

Categories