C# DoubleClick Event

I know this is "C++".com, but I've not been able to find a good alternative, and I like the community on this site better than any other site I've ever used for programming. So, my apologies for this being miscategorized or whatnot.

My dilemma is that I have a windows forms application, and when people double click on the form itself, the background color of the form is supposed to change. I have copy/pasted/tweaked code from a previous assignment where this worked perfectly, but it seems to not work at ALL in this situation.

The code that I have is

1
2
3
4
        private void Counter_DoubleClick(object sender, EventArgs e)
        {
            this.BackColor = Color.Red;
        }


It just seems to not be recognizing the double click event happening. Any help as to why this might be happening would be GREATLY appreciated.
I'm not too familiar with .NET at all, I personally don't like using it. But, I think you just need to make it public.
I tried that and it's still not working. I have all of two weeks of experience with this, and the exact same syntax worked for another program I did. I'm at a loss.
this in this case is talking about the button, isnt it?
So, by doing this
this.BackColor
you actually telling it to change the BackColor of the button.

That's what I thought.
From my limited experience with c# and .net:
When you use windows forms and you doubleclick an object to create an event for it it will typically come up with similar code to what you've written here. If you're form isnt called "Counter" I would assume that you've clicked a different component with that *design* name.
If you dont have any other components in the form, make sure you did not add a counter by accident.


Been a while since I messed about with .net though.

//Zerk

Edit: ThangDo, I think because of c# delegate system that this will actually apply to the application/main form, just the delegate function is added to the wrong unit in the designer generated code due to missclick.
Last edited on
Hmm thangdo may be on to something. Try me.BackColor. Even though this should refer to the class that it takes place in, me also refers to the form your in
closed account (z05DSL3A)
Have you wired up the event handler?

you should have something like this in your code:
this.DoubleClick += new System.EventHandler(this.Counter_DoubleClick);
this works for me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Counter : Form
    {
        public Counter()
        {
            InitializeComponent();
        }

        private void Counter_Load(object sender, EventArgs e)
        {
            this.MouseDoubleClick += new MouseEventHandler(Counter_DoubleClick);
        }
        private void Counter_DoubleClick(object sender, MouseEventArgs e)
        {
            this.BackColor = Color.Red;
        }
    }
}
1. 你可以在InitializeComponent()函数里面,添加上this.DoubleClick += new
System.EventHandler(this.Counter_DoubleClick)这句代码。
2. 它的含义是:Form(this)的"双击"事件由Counter_DoubleClick(objece sender,EventArgs e)
方法来处理。

1.you should lay "this.DoubleClick += new System.EventHandler(this.Counter_DoubleClick)" in the
function "InitializeComponent".
2. It means that Form's doubleclick event is dealed by the function "Counter_DoubleClick(objece
sender,EventArgs e)" that you have defined.
Topic archived. No new replies allowed.