3

I'm building a C# WinForms program, and my textboxes do not allow the user to highlight the text consistently throughout the program.

In some places, the highlighting works normally: you type something in the box, click and drag over some text, and it highlights where you dragged.

In other places, clicking and dragging does not select the text. The only way to do it is by double clicking on the text.

I haven't changed any default properties of these textboxes or messed with any event listeners. I placed brand new textboxes in different places, and they behave differently.

I'm wondering if it has something to do with the properties of the Form the TextBox is contained in, since it seems to appear that either all textboxes in a particular form work, or none do. However, as far as I can tell the properties look to be the same across the board, and I don't ever remember changing anything.

To me it seems like it's happening randomly. I can't find any information on the topic. Does anybody have any idea what I'm talking about?

EDIT: Ok, I figured out where the problem lies, but I still don't know how to fix it.

It happens only in forms which have been added to a SplitContainer in my main window like so:

myForm.TopLevel = false;
this.splitContainer.Panel2.Controls.Add(myForm);
myForm.Show();

EDIT 2: I now know that this is the same issue encountered here: Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form . The accepted answer isn't useful to me, and the other answers seem impractical, since I'd have to add event handlers to every single textbox...

Community
  • 1
  • 1
C Walker
  • 793
  • 2
  • 9
  • 19
  • Are you using data binding or doing anything on the OnFocus() method? – rein Oct 08 '10 at 16:28
  • Nope, nothing at all. I am totally baffled. – C Walker Oct 08 '10 at 16:34
  • Interesting, can you provide a sample? – rein Oct 08 '10 at 16:39
  • This is the *exact* same problem of course. Use Spy++ if you are not convinced. – Hans Passant Oct 08 '10 at 17:28
  • It's not that the accepted answer doesn't fix the problem. It's that I require the Form Border so that I can grab my window and move it around and it functions like a normal window. Yes, it works. But it doesn't help me. – C Walker Oct 08 '10 at 17:46
  • Recoisiche> How did you end up resolving this? If you aren't able to mark one of the given answers as correct, can you create a new answer, detailing how you did resolve it, and mark that as correct? Thanks! – James King Oct 26 '10 at 14:59
  • Unfortunately, I haven't solved it yet. I moved onto other high priority things, and will come back to this issue when I get the chance. In the meantime I was hoping someone would have some insight. – C Walker Oct 26 '10 at 22:10

2 Answers2

2

I had the same problem today. I tried changing TopLevel as others have suggested. This didn't work. Somewhere along my search I saw a suggestion to create a click event for the text box and use it to force focus on the control. This made no difference either. There were no events that should intercept and block a click event. It was just an MDI child with a few controls on it stuffed inside a panel on a split container. I couldn't highlight text in textboxes or textbox-derived controls though.

Turns out the solution was to switch the order of childform.Show() and panel.Controls.Add(childform). If you add the child form before it is shown, you apparently cause this bug.

Bob Kaufman
  • 12,864
  • 16
  • 78
  • 107
dazed.dnc
  • 21
  • 2
  • Darn, was so hopeful for this, but for me, showing the form before adding it doesn't appear to make a difference. Good idea though! – Tsaukpaetra Feb 25 '16 at 04:37
1

I'm a little perplexed at what you're trying to accomplish. I'm used to using a user control if I want to embed something on a SplitPanel, and using an MDI form if I want child forms.

Do either of these approaches work for you, and if not, can you explain why not/what you are trying to accomplish?

Thanks! James


* Edit *

You can add a panel (regular panel, not a split panel) to an MDI parent form and dock it to the left. Add whatever you currently have in the left panel of the SplitContainer to this left-docked panel, instead. Now you can instantiate forms, set them as children to the main MDI parent, and have all the window functionality you're looking for... You can maximize them, and they will fill the right-side of the MDI parent; you can pick cascade or tile from the window menu, etc.

If you want to let the user dynamically resize the left panel, drop a splitter panel into the right-hand portion of the main MDI form container; it will dock left by default, and show up to the immediate right of the panel. Now when you run, you can drag the border of the panel to resize.

Remember, an MDI form is like any other form... you can add any control you want to its surface, and .NET is pretty smart about how it incorporates the child windows.

If you're still not sure of what I'm trying to describe, I'll try to find somewhere I can drop a sample project... because everything is really done in the designer, there's not really any code I can show you. Here's the code for creating a form as an MDI child (running from within the MDI parent):

MyForm frm = new MyForm();
frm.MdiParent = this;
frm.Show();

That's all there is to it.

HTH! James

James King
  • 6,233
  • 5
  • 42
  • 63
  • Basically, what I would like to do is similar to using an MDI form in order to have child forms. However, I don't want to add my child forms to a parent Form, but rather a SplitContainer (my children need to be opened only in the right-side panel, and I have other things going on in the left-side panel). SplitContainer doesn't have an IsMdiContainer property, but I was able to make it act similarly by setting the top level of my children to false, and adding them to the SplitContainer (as in the code in the original question). But, this causes my problem. Any ideas? – C Walker Oct 13 '10 at 16:46
  • After coming back to this problem months later and with fresh eyes, I have realized that your solution does in fact solve my problem. I think I still had the TopLevel of my child forms set to false somewhere, and that is where the issue lies. Sorry for the delayed accepting of your answer, but thanks very much! – C Walker Apr 26 '11 at 22:35