C# Winforms Usercontrol drag and snap to panel (runtime) - c#

I am trying to write a program to manage tasks from my team. For this I use drag&drop capable custom UserControls. I try the following: When the UserControl is dragged over a slot, the control should then snapped into the slot. The problem seems to be that the slot does not get the events like "MouseEnter" or "DragEnter". Does anyone know a workaround, or is it possible to pass events down?
I think I just need a little push in the right direction.
Edit:
Main functionality:
The user control, let's call it task, should be docked into a slot when released over it.
A slot consists of properties and a panel. The task should appear in the panel.
Bonus:
A task, should dock into the different slots while dragging. So that it is always visible where the 'task' is placed when it is released.
I hope this shows what I am trying to do.
Edit 2
To clarification: I use the "MouseDown", "MouseUp" and "MouseMove" for the drag an drop.
Here you can see my Example the blueish panel is my task. The white the slot it should snap to.
On the second picture I drag the Task over the Slot panel. The mouse is not visable here but it's right beside the "K" of task.
i have added for testing the following code. Which should color the panel inside the slot in a color if an event is raised. But the color stays white.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
this.panel1.BackColor = Color.Black;
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
this.panel1.BackColor = Color.Green;
}
private void panel1_DragEnter(object sender, DragEventArgs e)
{
this.panel1.BackColor = Color.Yellow;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
this.panel1.BackColor = Color.Violet;
}
Why I don't get events on the Slotpanel?
I hope that this helps to understand my problem.

Related

How to erase rectangle drawn in paint event from picture box? c#

I know there are many questions on this, but I just can't get it to work. I have this event:
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawRectangle(new Pen(selectionBrush, 1), selectionRectangle);
}
It works as it should. Draws a colored rectangle over the image I put inside picture box. But after I draw that rectangle, I want to erase it. I basically use it as selection area for cropping the image. Is there a way that I can erase the rectangle that I have drawn? Thanks.
Here is where I want to erase the rectangle:
private void button1_Click(object sender, EventArgs e)
{
//here are some examples of what I have tried
pictureBox1.Invalidate();
Invalidate();
}
Your Paint routine has a call to DrawRectangle. Then you invalidate the rectangle. At that point, your paint routine will get called again, and, if your code to draw the rectangle is still there, it will get redrawn. You need to make it so the DrawRectangle call no longer gets called once you don't need it any more (with an if statement or something).
You also probably want to inflate the size of the invalidation rectangle by one pixel all around to make sure you don't leave any bread crumbs behind

How to make picturebox change image smooth and fast

I got a picture box that got an original image in my program. When i hover with my mouse over it it changes picture to another picture and when i leave it changes back.
But..
There is such a delay to change to the right picture if i hover over it or not. It takes like 1 second before it changes, What should i change to improve the speed of the change? This is the code i am using at the moment:
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Image = ABC_Bok.Properties.Resources.BokVänsterhörn_1;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Image = ABC_Bok.Properties.Resources.BokVänsterhörnet;
}
This is the third time today that I've seen this issue. MouseHover is raised when the mouse pointer STOPS OVER a control. If you want something to happen as soon as the mouse pointer goes over the control then you want MouseEnter, just as you're using MouseLeave for the change back again.

c# wpf - Can I get the coordinates to a pixel clicked in an image control

I have an Image control running in my MainWindow.xaml, I was wondering if I can get the X and Y coordinates to the pixel clicked from the RoutedEventArgs on the MouseUp event for the control? I don't need the color or anything at that pixel, I would just like to know where that pixel was. Thank you for the help.
You can try handling the MouseLeftButtonUp event:
private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e){
Point pos = e.GetPosition(image1);
}

how to detect a mouse click on drawn image?

I am new to c# Windows Form Application Development.
I created a form with a panel in which the user can draw image on it. How do I check if the image is clicked?
In designer mode, right-click on the panel, go to Properties.
In the Properties window, choose EVENTS (Lightning icon).
Double click on Click, then this code will be generated:
private void panel1_Click(object sender, EventArgs e)
{
//--what to do when user clicks on panel--
MessageBox.Show("Clicked");
}
Just double-click on the form's image-panel (or whatever object you wish to detect click events on) and Visual Studio will automatically generate a OnClick() event. Needless to say, I'm talking about the forms designer, not the actual form you will see when testing your code.
Alternatively, you can set which events you want to implement through the object's properties. That way you can also implement OnKeyDown() or OnFocus() or any other kind of events.
Edit: If the image doesn't cover the entire panel, you'll have to check if the mouse position is within the image's dimension. Assuming the image is drawn at position (imgOriginX, imgOriginY) and has the size (imgWidth, imgHeight):
// Fires, when user clicks on panel
private void panel_Click(object sender, EventArgs e)
{
// Cast to MouseEventArgs
MouseEventArgs mouse = (MouseEventArgs)e;
// If mouse is within image
if (mouse.X >= imgOriginX && mouse.Y >= imgOriginY && mouse.X < imgOriginX + imgWidth && mouse.Y < imgOriginY + imgHeight)
{
// do something here
}
}

Hide scrollbars of a RichTextBox

I'm trying to write a simple text editor like DarkRoom with just a RichTextBox (or alternatively a TextBox) in it. My problem is that I can't use the mouse wheel for scrolling unless I have a vertical scrollbar. Is there any way to hide this scrollbar and still be able to scroll with the mouse wheel?
So far I have several ideas how this could be done, but no idea how to implement them.
re-create the scrolling code using a MouseWheel event
change the visual style of the scrollbar to hide it or make it less visible
write my own TextBox widget
overlap the scrollbars with something else to hide them
P.S.: Using any win32 stuff is not an option.
Yes, you will have to capture the .MouseWheel and .MouseMove events. See this post.
Ok, do something like following:
Add a line in form load event.
private void Form1_Load(object sender, EventArgs e)
{
this.richTextBox1.MouseWheel += new MouseEventHandler(richTextBox1_MouseWheel);
}
Add following in mouse wheel event.
void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
{
if (e.Delta > 0)
{
//Handle mouse move upwards
if (richTextBox1.SelectionStart > 10)
{
richTextBox1.SelectionStart -= 10;
richTextBox1.ScrollToCaret();
}
}
else
{
//Mouse move downwards.
richTextBox1.SelectionStart += 10;
richTextBox1.ScrollToCaret();
}
}
Let me know in either cases, if you would want the running sample of the same; or if you are not liking the solution (0:

Categories

Resources