自定义 Winforms 控件中的基线对齐线
我有一个带有文本框的自定义用户控件,我想在自定义控件之外公开(文本框中文本的)对齐线的基线。 我知道您创建一个设计器(继承自 ControlDesigner)并重写 SnapLines 以访问对齐线,但我想知道如何获取我通过自定义用户控件公开的控件的文本基线。
I have a custom user control with a textbox on it and I'd like to expose the baseline (of the text in the textbox) snapline outside of the custom control. I know that you create a designer (inherited from ControlDesigner) and override SnapLines to get access to the snaplines, but I'm wondering how to get the text baseline of a control that I have exposed by my custom user control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
VB.Net版本:
注意:您必须将
txtDescription
更改为文本框或您使用的其他内部控件名称。 和ctlUserControl
到您的usercontrol
名称VB.Net Version:
Note: you have to change the
txtDescription
to the Textbox or another internal control name that you use. andctlUserControl
to yourusercontrol
name你走在正确的轨道上。 您需要在设计器中覆盖 SnapLines 属性并执行如下操作:
在本示例中,用户控件包含一个文本框。 该代码添加了一条新的对齐线,表示文本框的基线。 重要的是正确计算偏移量。
You're on the right track. You will need to override the SnapLines property in your designr and do something like this:
In this example the usercontrol contains a textbox. The code adds a new snapline that represents the baseline for the textbox. The important thing is to calculate the offset correctly.
感谢所有人的帮助。 这是一件难以下咽的事。 在每个 UserControl 中都有一个私有子类的想法不太令人愉快。
我想出了这个基类来帮助您。
接下来,从这个基类派生您的 UserControl:
再次感谢您发布此内容。
Thanks to all those for the help. This was a tough one to swallow. The thought having a private sub-class in every UserControl wasn't very palatable.
I came up with this base class to help out..
Next, derive your UserControl from this base:
Thanks again for posting this.
我只是有类似的需求,我是这样解决的:
这样它实际上是为子控件创建一个临时子设计器,以便找出“真正的”基线对齐线在哪里。
这在测试中似乎表现相当不错,但如果性能成为一个问题(并且如果内部文本框不移动),那么大部分代码可以提取到 Initialize 方法。
这还假设文本框是 UserControl 的直接子级。 如果还有其他影响布局的控件,那么偏移计算会变得更加复杂。
I just had a similar need, and I solved it like this:
This way it's actually creating a temporary sub-designer for the subcontrol in order to find out where the "real" baseline snapline is.
This seemed reasonably performant in testing, but if perf becomes a concern (and if the internal textbox doesn't move) then most of this code can be extracted to the Initialize method.
This also assumes that the textbox is a direct child of the UserControl. If there are other layout-affecting controls in the way then the offset calculation becomes a bit more complicated.
作为 Miral 答案的更新.. 对于正在寻找如何做到这一点的新人来说,这里有一些“缺失的步骤”。 :) 上面的 C# 代码几乎已经“直接”准备就绪,除了更改一些值以引用将要修改的 UserControl 之外。
可能需要的参考资料:
System.Design (@robyaw)
所需的使用:
在您的 UserControl 上,您需要以下属性:
然后您需要一个具有 SnapLines 覆盖的“设计器”类:
As an update to the Miral's answer.. here are a few of the "missing steps", for someone new that's looking how to do this. :) The C# code above is almost 'drop-in' ready, with the exception of changing a few of the values to reference the UserControl that will be modified.
Possible References Needed:
System.Design (@robyaw)
Usings needed:
On your UserControl you need the following Attribute:
Then you need a "designer" class that will have the SnapLines override: