Windows Forms: Cannot click to focus MaskedTextBox on Non TopLevel form

As in the title, I have a child form with the TopLevel property set to False, and I cannot click on the MaskedTextBox control that it contains (to draw attention to it). I can focus on this using the TAB on the keyboard.

The child form contains other regular TextBox controls, and I can click to focus without problems, although they also exhibit odd behavior: for example, if I have a value in the text box and I try to drag the end of the line to the beginning nothing happens. In fact, I can’t use my mouse to move the cursor inside the TextBox text at all (although they work with keyboard arrows).

I'm not too worried about the strange behavior of the TextBox, but why can't I activate 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()
+2
source share
3 answers

I tried your code and got a good repro this time. As I mentioned in my original post, this is really a window activation issue. You can see this in Spy ++, pay attention to the WM_MOUSEACTIVATE messages.

, . Windows, . , . , " " .

. , :

    newReportForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None

, UserControl. , :

    newReportForm.ControlBox = False
    newReportForm.Text = ""

.

+5

, , . , OP, . MaskedTextBox Click:

    private void MaskedTextBoxSetFocus(object sender, EventArgs e)
    {
        var mtb = (MaskedTextBox)sender;
        mtb.Focus();
    }

MaskedTextBox, - , , , .

+2

. - . . , , TopLevel False.

. - Control.Capture, MouseDown , MouseUp, , . . , MouseDown.

- - IMessageFilter , WM_LBUTTONDOWN.

+1

Source: https://habr.com/ru/post/1768653/


All Articles