Click here to Skip to main content
15,887,924 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I need to change control(TextBox) location runtime
for that I am using LocationChange event

When I am loading form in FormLoad event I am setting controls Left/Top Property
My code snipt is like

C#
private void Form_Load(object sender, EventArgs e)
{
      this.PayTextBox.Top = this.objChequeTemplate.PayTop;
      this.PayTextBox.Left = this.objChequeTemplate.PayLeft;
}


C#
private void PayTextBox_LocationChanged(object sender, EventArgs e)
{
    objChequeTemplate.PayLeft = ((Control)sender).Left;
    objChequeTemplate.PayTop = ((Control)sender).Top;
}


When loading form it change top location that time event is fire
but when it change left event is not fire

Why It happends?
I think it should fir both time

any suggestion
Thanks
Posted

Hi Khaniya,

After getting your comments I have just made a small test project. The results show that my previous answer was incorrect, for which I must apologize. Behind the scenes changing the Top or Left properties does modify the Location property

A Form with just a TextBox. I handled the LocationChanged event for the TextBox to show a MessageBox with a simple message "Location Changed". I then handled the FormLoad event the same as you have except that I set both Top and Left to 10 rather than a variable.

In my test I got "Location Changed" twice, once for Top and once for Left.
The only way that I could get it to only fire once was if I placed the TextBox Left at 10 in the designer, so that when I tried to set it to 10 in FormLoad, it was already there so nothing was changed.

I hope that you will be able to verify this in your project by moving your TextBox wildly out of position in the designer so that you can be sure that both properties get changed on FormLoad

Once again, sorry for giving you bad advice before but I think I am right now, having tested it for myself.

Good luck! :)
 
Share this answer
 
Comments
Khaniya 12-Jun-10 7:24am    
Yes you are right
but in this case I have to be sorry for asking such stupied question.
If you see my code carefully you come to know my silly mistake.
thank for your support
If you read the MS documentation here[^] it clearly states
Occurs when the Location property value has changed.


Changing the Left property doesn't change the Location property.

To get that effect you would have to somehow modify your code to update the Location when left changes e.g. myControl.Location = new Point(newLeftValue, myControl.Location.Y);.
 
Share this answer
 
Comments
Khaniya 12-Jun-10 0:32am    
Thanks Henry
But I thing It should also fire on changing Top and Left Proeprty also
Any way thanks for you answer
Khaniya 12-Jun-10 0:38am    
My question is little bit differ
see my code, I am asking it is firing only once using above code
even I am not changing Location property
why so?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900