控制的LocationChange事件 [英] LocationChange Event of Control

查看:639
本文介绍了控制的LocationChange事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改控件(TextBox)的位置运行时
为此,我正在使用LocationChange事件

在FormLoad事件中加载表单时,我在设置控件的Left/Top属性
我的代码片段就像

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

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





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

推荐答案

您好,Khaniya,

得到您的意见后,我刚刚做了一个小型测试项目.结果表明,我以前的答案是错误的,对此我必须表示歉意.在后台更改顶部左侧属性确实修改了Location属性

TextBoxForm.我为TextBox处理了LocationChanged事件,以显示带有简单消息位置已更改"的MessageBox.然后,我将FormLoad事件的处理方式与您相同,除了将Top和Left都设置为10而不是变量.

在我的测试中,两次位置更改",一次是顶部",一次是左侧".
我只能触发一次的唯一方法是,如果将TextBox留在设计器中的位置为10,那么当我尝试在FormLoad中将其设置为10时,该位置已经存在,因此没有任何更改.

我希望您可以通过在设计器中将TextBox完全移出位置来验证您的项目,以便确保在FormLoad
上都更改了这两个属性.
再次感谢您之前给您的坏建议,但我想我现在已经对自己进行了测试.

祝你好运! :)
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! :)


如果您阅读了MS文档
If you read the MS documentation here[^] it clearly states
Location 属性值更改时发生.
Occurs when the Location property value has changed.



更改Left属性不会更改Location属性.

为了获得这种效果,您必须以某种方式修改代码以在左侧更改时更新Location,例如myControl.Location = new Point(newLeftValue, myControl.Location.Y);.



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);.


这篇关于控制的LocationChange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆