2

I know that label can't receive focus, it doesn't seem to be responsive to tab switching. But all I need is perform some action when user uses tab consequently on a form which has only labels. For example, each label has an associated textbox but this textbox is hidden when the label is visible and vice-versa. What I want is allowing user to use tab to switch between the hidden textboxes on the form, normally, all the textboxes are hidden while all the labels are shown, the labels are supposed to be focusable so that when using tab, it can know that (as some event) to show the associated textbox and hide itself, when switching to another label, the current label whose the associated textbox is shown will become visible again and its associated textbox will become hidden.

I have to implement this kind of 2 in 1 control (textbox and label in a composited control) because I just want to show only the text (no border and background) as if the textbox has a transparent background and only show the textbox (and hide the label) when user need to edit (start by clicking on the field or using tab). This should have been easier for me if there was a transparent background textbox but there isn't a decent one in the world of windows forms. Please notice that I also know of the alpha blend transparent textbox presented in an article in codeproject but it can't meet my need because the text is rendered wrong with ugly border around the text path (some kind of missing antialiasing but it's even worse than that).

I'm really pity if this mechanism can't be implemented, the forms look better when all the fields seem to show info only but a click or tab switch can let user jump in edit mode. I hope there is some solution out there. Thank you in advance.

4

1 回答 1

2

我一时兴起找到了这个解决方案。我不认为有这样的解决方案,但它确实有助于解决我的问题(我相信许多其他人会从中受益)。简单地说,我必须创建自己的 Label 继承UserControl。我之前没有想到UserControl,它非常有帮助。可聚焦性、透明背景、无边框都是UserControl可以轻松完成的。我必须自己做的唯一自定义功能是渲染文本,这也很简单,有很多方法可以做。我只是在UserControl中添加一个 Label 并将 Label 的 Dock 设置为 DockStyle.Fill,正确调整UserControl的高度,仅此而已。

感谢上帝在尝试任何其他复杂的解决方案(例如收听 TAB 和 SHIFT + TAB 按键事件)之前帮助我想到了 UserControl。

于 2013-04-17T18:16:32.407 回答