控制的LocationChange事件 [英] LocationChange Event of Control
问题描述
我需要更改控件(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
属性
仅TextBox
的Form
.我为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 theLocation
property
AForm
with just aTextBox
. I handled theLocationChanged
event for theTextBox
to show aMessageBox
with a simple message "Location Changed". I then handled theFormLoad
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 theTextBox
Left at 10 in the designer, so that when I tried to set it to 10 inFormLoad
, it was already there so nothing was changed.
I hope that you will be able to verify this in your project by moving yourTextBox
wildly out of position in the designer so that you can be sure that both properties get changed onFormLoad
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屋!