Windows 窗体:无法在非顶级窗体中单击以聚焦 MaskedTextBox [英] Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form

查看:24
本文介绍了Windows 窗体:无法在非顶级窗体中单击以聚焦 MaskedTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像标题所说的那样,我显示了一个子窗体,它的 TopLevel 属性设置为 False,并且我无法单击它包含的 MaskedTextBox 控件(以便将焦点放在它上面).不过,我可以通过在键盘上使用 TAB 来集中注意力.

Like the title says, I've got a Child form being shown with it's TopLevel property set to False and I am unable to click a MaskedTextBox control that it contains (in order to bring focus to it). I can bring focus to it by using TAB on the keyboard though.

子窗体包含其他常规 TextBox 控件,我可以毫无问题地单击这些控件进行聚焦,尽管它们也表现出一些奇怪的行为:例如,如果我在 TextBox 中有一个值并且我尝试从字符串的结尾到开头,什么也没有发生.事实上,我根本无法使用鼠标在 TextBox 的文本内移动光标(尽管它们键盘箭头键有效).

The child form contains other regular TextBox controls and these I can click to focus with no problems, although they also exhibit some odd behavior: for example if I've got a value in the Textbox and I try to drag-click from the end of the string to the beginning, nothing happens. In fact I can't use my mouse to move the cursor inside the TextBox's text at all (although they keyboard arrow keys work).

我不太担心奇怪的 TextBox 行为,但为什么我不能通过单击它来激活我的 MaskedTextBox?

I'm not too worried about the odd TextBox behavior, but why can't I activate my MaskedTextBox by clicking on it?

以下是显示表单的代码:

Below is the code that shows the form:

Dim newReportForm As New Form
Dim formName As String
Dim FullTypeName As String
Dim FormInstanceType As Type

formName = TreeView1.SelectedNode.Name

FullTypeName = Application.ProductName & "." & formName

FormInstanceType = Type.GetType(FullTypeName, True, True)

newReportForm = CType(Activator.CreateInstance(FormInstanceType), Form)
Try
   newReportForm.Top = CType(SplitContainer1.Panel2.Controls(0), Form).Top + 25
   newReportForm.Left = CType(SplitContainer1.Panel2.Controls(0), Form).Left + 25
Catch
End Try
newReportForm.TopLevel = False
newReportForm.Parent = SplitContainer1.Panel2
newReportForm.BringToFront()                
newReportForm.Show()

推荐答案

我尝试了你的代码,这次得到了很好的重现.正如我在原帖中提到的,这确实是一个窗口激活问题.您可以在 Spy++ 中看到这一点,注意 WM_MOUSEACTIVATE 消息.

I tried your code and got a good repro this time. As I mentioned in my original post, this is indeed a window activation problem. You can see this in Spy++, note the WM_MOUSEACTIVATE messages.

发生这种情况是因为您显示带有标题栏的表单.这使 Windows 窗口管理器相信该窗口可以被激活.这实际上不起作用,它不再是顶级窗口.从标题栏中可以看到,它永远不会使用窗口激活"颜色绘制.

This happens because you display the form with a caption bar. That convinces the Windows window manager that the window can be activated. That doesn't actually work, it is no longer a top-level window. Visible from the caption bar, it never gets drawn with the "window activated" colors.

您必须从表单中删除标题栏.最好将此行添加到您的代码中:

You will have to remove the caption bar from the form. That's best done by adding this line to your code:

    newReportForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None

这会将表单变成一个控件,否则与 UserControl 无法区分.您仍然可以使用以下代码使其与众不同:

Which will turn the form into a control that's otherwise indistinguishable from a UserControl. You can still make it distinctive by using this code instead:

    newReportForm.ControlBox = False
    newReportForm.Text = ""

任一修复都可以解决鼠标点击问题.

Either fix solves the mouse click problem.

这篇关于Windows 窗体:无法在非顶级窗体中单击以聚焦 MaskedTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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