One handler for several events of different classes? - c#

I want to create a program that will draw a circle at the cursor position when it is moved.
I tried to create a method that will be an event handler for MouseEventHandler and PaintEventHandler, but I got the error cs0123. How to fix it, and is it even possible?
My code:
Pen pen = new Pen(Color.Black, 3)
private void Form1_Load(object sender, EventArgs e)
{
PictureBox pictureBox1 = new PictureBox();
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.Paint += new PaintEventHandler(My_func);//cs0123
pictureBox1.MouseMove += new MouseEventHandler(My_func);//cs0123
}
private void My_func(object sender, PaintEventArgs pe, MouseEventArgs me)
{
Graphics g = e.Graphics;
g.DrawEllipse(pen, pictureBox1.PointToClient(Cursor.Position), 3, 3);
}

You can't just pop in random arguments to eventhandlers with fixed signatures. Both of them expect 2 arguments only, PaintEventHandler MouseEventHandler so they can't be handled by a method which expects three arguments.
Either create two different eventhandlers, one for each event.
pictureBox1.Paint += new PaintEventHandler(pehandler);
pictureBox1.MouseMove += new MouseEventHandler(mehandler);
private void mehandler(object sender, MouseEventArgs args) {
...
}
private void pehandler(object sender, PaintEventArgs args) {
...
}
Or create a more generic one, which fits the signature
private void myfunc(object sender, EventArgs args) {
if (args is PaintEventArgs) {
//do something with the args
}
else if (args is MouseEventArgs {
//do something with the args
}
}

Related

Clearing lines drawn on picture box in c# windows form

I have a c# windows form program where the user can draw lines with a mouse on an image in the picture box. The graphics are meant to be created by the pictureBox1_Paint method. How can I erase the drawn lines and keep the image in tact?
Defined default image here:
public lineTest()
{
InitializeComponent();
defaultImage = pictureBox1.Image;
}
Drew lines like this:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
lines.Push(new Line { Start = e.Location });
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (lines.Count > 0 && e.Button == System.Windows.Forms.MouseButtons.Left)
{
lines.Peek().End = e.Location;
pictureBox1.Invalidate();
}
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
foreach (var line in lines)
{
Pen magenta = new Pen(Color.Magenta, 5);
e.Graphics.DrawLine(magenta, line.Start, line.End);
}
}
And tried erasing lines with:
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Image = defaultImage;
pictureBox1.Invalidate();
}
and nothing appears to happen.
Paint is called every time you invalidate the control so your lines are re-drawn every time. In your button1_Click event handler add this line:
lines.Clear();
before you call pictureBox1.Invalidate();
That will stop the lines being re-drawn when the paint event next fires.

Tab Control blocking mouse scroll event C#

I have a tab control that contains multiple tab controls. All of the tab controls were made using the winforms designer. The embedded tab controls each have Chart objects assigned them. These were created after the program was ran, and each chart was given three events:
chart.MouseWheel += new MouseEventHandler((sender, e) => this.Chart_MouseWheel(sender, e, chart, raw, condensed, bounds));
chart.MouseHover += new EventHandler(Chart_Hover);
chart.MouseClick += new MouseEventHandler((sender, e) => this.Chart_Click(sender, e, chart));
For easy debugging, I added a simple Console.WriteLine(); to each method to see which methods were actually being fired.
private void Chart_MouseWheel(object sender, MouseEventArgs e, Chart chart, DataTable raw, DataCondenser condensed, List<double> bounds)
{
Console.WriteLine("a");
}
private void Chart_Hover(object sender, EventArgs e)
{
Console.WriteLine("b");
}
private void Chart_Click(object sender, MouseEventArgs e, Chart chart)
{
Console.WriteLine("c");
}
After hovering, clicking, and scrolling a lot, I can only get b and c to be outputted. For some reason the scroll event will not be picked up. I have a feeling this has to do with being inside a tab control.
Any ideas why this is happening?
EDIT:
Tried a small-scale version of this and the same thing is happening.
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Chart test = new Chart();
test.Width = 500;
test.Height = 500;
test.MouseWheel += new MouseEventHandler(Chart_Scroll);
test.MouseHover += new EventHandler(Chart_Hover); //mousehover event for the tooltip to activate
test.MouseClick += new MouseEventHandler(Chart_Click);
tabPage3.Controls.Add(test);
}
private void Chart_Scroll(object sender, MouseEventArgs e)
{
Console.WriteLine("a");
}
private void Chart_Hover(object sender, EventArgs e)
{
Console.WriteLine("b");
}
private void Chart_Click(object sender, MouseEventArgs e)
{
Console.WriteLine("c");
}
The same issue occurred. tabPage3 was a tabpage of a tabcontrol inside of a tab control.
EDIT 2:
so if I give the chart this event handler:
test.MouseEnter += new EventHandler(mouseEnter);
with the method:
private void mouseEnter(object sender, EventArgs e)
{
this.Focus();
}
it still doesn't work. However, if I use this:
private void mouseEnter(object sender, EventArgs e)
{
if (sender is Chart)
{
Chart temp = (Chart) sender;
temp.Focus();
}
}
It will work, even if it's embedded in other controls.
You could wire up the MouseEnter() event and give the Chart focus with a one-liner like this:
test.MouseEnter += (s, evt) => { ((Control)s).Focus(); };

Call paint event from mouse move event

When mouse move over a panel2, I need to draw lines. So far I have done following
public Form1()
{
InitializeComponent();
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
if (isDragging)
{
letsPaint(sender, e);
}
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
mouseMoveX = e.X;
mouseMoveY = e.Y;
this.Paint += new PaintEventHandler(panel2_Paint);
}
}
private void letsPaint(object sender, PaintEventArgs e)
{
Pen blackpen = new Pen(Color.Black, 3);
Graphics g = e.Graphics;
g.DrawLine(blackpen, mouseClickedX, mouseClickedY, mouseMoveX, mouseMoveY);
g.Dispose();
}
But nothing happens when I move mouse. I think I did something wrong PaintEventHandler() here. Please tell me how to do this and also if there is any better way for this.
Also I think my method will drawline on the form but I need to draw line on the panel2. How to do? Thanks in advance.
You invalidate:
public Form1()
{
InitializeComponent();
panel2.Paint += new letsPaint;
}
private void panel2_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging) {
mouseMoveX = e.X;
mouseMoveY = e.Y;
panel2.Invalidate();
}
}
and you don't dispose the graphic object (you didn't create it), but you do the pen:
private void letsPaint(object sender, PaintEventArgs e) {
using (Pen blackpen = new Pen(Color.Black, 3)) {
e.Graphics.DrawLine(blackpen,
mouseClickedX, mouseClickedY, mouseMoveX, mouseMoveY);
}
}
Here is a quick little method that works with a bitmap:
Bitmap bmp;
Point lastPoint;
public Form1() {
InitializeComponent();
bmp = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height,
System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
panel1.MouseDown += panel1_MouseDown;
panel1.MouseMove += panel1_MouseMove;
panel1.Paint += panel1_Paint;
}
void panel1_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawImage(bmp, Point.Empty);
}
void panel1_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
using (Graphics g = Graphics.FromImage(bmp)) {
g.DrawLine(Pens.Black, lastPoint, e.Location);
}
lastPoint = e.Location;
panel1.Invalidate();
}
}
void panel1_MouseDown(object sender, MouseEventArgs e) {
lastPoint = e.Location;
}
This will flicker, so you would want to replace your panel with a double-buffered panel. Something like this:
public class PanelEx : Panel {
public PanelEx() {
this.DoubleBuffered = true;
}
}

no overload for matches delegate 'system.eventhandler'

As I'm pretty new to C#, I struggle with the following piece of code. When I click to button 'knop', the method 'klik' has to be executed. The method has to draw the Bitmap 'b', generated by 'DrawMandel' on the form. But I constantly get the error 'no overload for matches delegate 'system.eventhandler'.
using System;
using System.Windows.Forms;
using System.Drawing;
class Mandelbrot : Form
{
public Bitmap b;
public Mandelbrot()
{
Button knop;
knop = new Button();
knop.Location = new Point(370, 15);
knop.Size = new Size(50, 30);
knop.Text = "OK";
this.Text = "Mandelbrot 1.0";
this.ClientSize = new Size(800, 800);
knop.Click += this.klik;
this.Controls.Add(knop);
}
public void klik(PaintEventArgs pea, EventArgs e) {
Bitmap c = this.DrawMandel();
Graphics gr = pea.Graphics;
gr.DrawImage(b, 150, 200);
}
public Bitmap DrawMandel()
{
//function that creates the bitmap
return b;
}
static void Main() {
Application.Run(new Mandelbrot());
}
}
You need to change public void klik(PaintEventArgs pea, EventArgs e) to public void klik(object sender, System.EventArgs e) because there is no Click event handler with parameters PaintEventArgs pea, EventArgs e.
Yes there is a problem with Click event handler (klik) - First argument must be an object type and second must be EventArgs.
public void klik(object sender, EventArgs e) {
//
}
If you want to paint on a form or control then use CreateGraphics method.
public void klik(object sender, EventArgs e) {
Bitmap c = this.DrawMandel();
Graphics gr = CreateGraphics(); // Graphics gr=(sender as Button).CreateGraphics();
gr.DrawImage(b, 150, 200);
}
You need to wrap button click handler to match the pattern
public void klik(object sender, EventArgs e)
Change the klik method as follows:
public void klik(object pea, EventArgs e)
{
Bitmap c = this.DrawMandel();
Button btn = pea as Button;
Graphics gr = btn.CreateGraphics();
gr.DrawImage(b, 150, 200);
}

How to get the Coordinates of a Custom Image Cursor in c#?

Bitmap hh = (Bitmap)System.Drawing.Bitmap.FromFile("example.png");
Graphics.FromImage(hh);
IntPtr ptr = hh.GetHicon();
Cursor c = new Cursor(ptr);
this.Cursor = c;
I use this code to create a custom image cursor. I want to retrieve the coordinates of this custom image cursor when on a Click event. So that these coordinates can be used to draw the image of this cursor in a picture box when clicked on the image loaded in the picture box. I'm doing this in C#.
I tried another approach
public partial class Form1 : Form
{
private Bitmap _bmp = new Bitmap(250, 250);
public Form1()
{
InitializeComponent();
panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
panel1.Paint += new PaintEventHandler(panel1_Paint);
using (Graphics g = Graphics.FromImage(_bmp))
g.Clear(SystemColors.Window);
}
private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
label1.Text = mouseDownLocation.X.ToString();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(_bmp, new Point(0, 0));
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
using (Graphics g = Graphics.FromImage(_bmp))
{
g.DrawString("Mouse Clicked Here!", panel1.Font, Brushes.Black, e.Location);
}
panel1.Invalidate();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Image.Save(#"C:\test.jpg", ImageFormat.Jpeg);
}
But when i try so save the image i get an Exception: Object reference not set to an instance of an object.
Please note that panel1 in the code above refers to a picture box
To get the coordinates of the mouse on a PictureBox you should not handle the OnClick event but the OnMouseDown, for example in this way:
private void pb_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
}
now you have the mouseDownLocation which contains the coordinates you were looking for.
i know the way to get the coordinate of mouse you can code it like
Cursor.Position.X and Cursor.Position.Y to get the Coordinate under the mouse

Categories

Resources