I seem to have some bad luck getting any event开发者_开发技巧s beyond formX_load(..) to run. That's what I'm hoping someone here can help me with.
I started an empty project, added a label onto it (label1) and just copied the code off of MSDN's Example. (I added in my own label text).
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move%28v=vs.80%29.aspx
private void Form1_Move(object sender, System.EventArgs e)
{
this.Text = "Form screen position = " + this.Location.ToString();
label1.Text = "You Moved Me!";
}
Everything compiles and runs, but it doesn't matter how much I move or re-size that form it doesn't change the label text or the Form text.
I've also tried OnMove, OnMouseMove, and LocationChanged examples with the same problem...they never ever seem to be triggered.
What am I missing here? This seems too easy to be able to screw up, but alas...
Thanks for your time.
This works:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Move +=new EventHandler(Form1_Move);
}
private void Form1_Move(object sender, System.EventArgs e)
{
this.Text = "Form screen position = " + this.Location.ToString();
}
}
Overriding OnMove should have worked and is the preferred method. Did you call the base class OnMove method in your overrided method?
For the _Move event, did you wire up the event handler upon creating the instance of the Form?
Something like:
this.Move += this.Form1_Move;
There are 2 answers already explaining how to add the handler programmatically -as I mentioned on my comment-. If you prefer to do it from Visual Studio, simply load the Form Designer, look at the properties (usually located on the bottom right corner), click on the Events tab and double click on the Event for OnMove. Once you double click it VS will add the handler for you automatically and take you to the body of the method so you can put your code in there.
精彩评论