问题描述
就像标题中所说的那样,我已经显示了一个Child窗体,其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窗体:无法单击以使非TopLevel窗体中的MaskedTextBox集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!