尝试突出显示 C# TextBox 时行为不一致

发布于 2024-09-26 08:45:07 字数 884 浏览 3 评论 0原文

我正在构建一个 C# WinForms 程序,并且我的文本框不允许用户在整个程序中一致地突出显示文本。

在某些地方,突出显示正常工作:您在框中键入内容,单击并拖动一些文本,它会突出显示您拖动的位置。

在其他地方,单击并拖动不会选择文本。唯一的方法是双击文本。

我没有更改这些文本框的任何默认属性或弄乱任何事件侦听器。我将全新的文本框放置在不同的地方,它们的行为有所不同。

我想知道它是否与包含 TextBox 的表单的属性有关,因为似乎特定表单中的所有文本框都起作用,或者没有。然而,据我所知,这些属性看起来都是一样的,而且我不记得改变过任何东西。

对我来说,这似乎是随机发生的。我找不到有关该主题的任何信息。有人知道我在说什么吗?

编辑:好吧,我找出了问题所在,但我仍然不知道如何解决它。

它仅发生在已添加到主窗口中的 SplitContainer 中的表单中,如下所示:

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

编辑 2: 我现在知道这与此处遇到的问题相同:Windows 窗体:无法单击在非顶级表单中聚焦 MaskedTextBox 。接受的答案对我来说没有用,其他答案似乎不切实际,因为我必须向每个文本框添加事件处理程序......

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒灬来后我 2024-10-03 08:45:07

我今天也遇到了同样的问题。我尝试按照其他人的建议更改 TopLevel 。这不起作用。在我的搜索过程中,我看到了一条建议,为文本框创建一个单击事件并使用它来强制将焦点集中在控件上。这也没有什么区别。没有任何事件应该拦截和阻止单击事件。它只是一个 MDI 子项,上面有一些控件,填充在拆分容器的面板内。但我无法突出显示文本框或文本框派生控件中的文本。

事实证明,解决方案是交换 childform.Show()panel.Controls.Add(childform) 的顺序。如果您在显示子表单之前添加子表单,则显然会导致此错误。

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.

眼趣 2024-10-03 08:45:07

我对你想要实现的目标有点困惑。如果我想在 SplitPanel 上嵌入某些内容,我习惯使用用户控件;如果我想要子窗体,我习惯使用 MDI 窗体。

这些方法中的任何一种对您有用吗?如果没有,您能解释一下为什么不/您想要实现什么目标吗?

谢谢!
詹姆斯

* 编辑*

您可以将面板(常规面板,而不是分割面板)添加到 MDI 父窗体并将其停靠在左侧。相反,将 SplitContainer 左侧面板中当前拥有的任何内容添加到此左侧停靠面板中。现在您可以实例化表单,将它们设置为主 MDI 父级的子级,并拥有您正在寻找的所有窗口功能...您可以最大化它们,它们将填充 MDI 父级的右侧;您可以从窗口菜单中选择级联或平铺等。

如果您想让用户动态调整左侧面板的大小,请将拆分面板放入主 MDI 表单容器的右侧部分;默认情况下它将停靠在左侧,并显示在面板的右侧。现在,当您运行时,您可以拖动面板的边框来调整大小。

请记住,MDI 窗体与任何其他窗体一样...您可以在其表面添加任何所需的控件,并且 .NET 对于如何合并子窗口非常智能。

如果您仍然不确定我要描述的内容,我会尝试找到可以放置示例项目的地方...因为一切实际上都是在设计器中完成的,我实际上没有任何代码可以向您展示。下面是创建窗体作为 MDI 子级(从 MDI 父级内部运行)的代码:

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

这就是全部内容。

哈!
詹姆斯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文