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.
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.
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.
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
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();
}
privatevoid Counter_Load(object sender, EventArgs e)
{
this.MouseDoubleClick += new MouseEventHandler(Counter_DoubleClick);
}
privatevoid 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.