当TopLevel = false时,文本控件不允许用鼠标选择文本 [英] Text controls doesn't let to select the text with mouse when TopLevel = false
问题描述
大家好!
我的表单中的TextBoxes出现问题.
屏幕截图为图片http://rghost.ru/5011999/image.png
让我们通过一个简单的例子来说明这种情况.
1)使用Form1创建一个新的winforms项目;
2)创建一个新的Form2,将TextBox控件放到它上面;
3)让我们写一些简单的代码来创建非模式形式;
Form frm = new Form();
frm.StartPosition = FormStartPosition.CenterParent;
frm.TopLevel = false; // 这是问题
frm.Parent = this;
frm.Show();
frm.BringToFront();
执行此代码后,将显示带有文本框的表单.
在文本框中输入内容,例如这是一个测试".现在,尝试用鼠标选择部分或全部文本-您不能,因为它不起作用.
是的,您可以双击选择整个文本,但是之后不能取消选择它.此外,如果您只需要选择一部分文本来删除它,该怎么办?!
当表单为模式形式时,一切正常.
我和Memo的情况相同,我猜想还有其他一些文本控件.
我需要的所有这些都是因为我们使用了认真的MDI项目,并且在每个MDI表单内用户都可以打开不同的表单(例如搜索表单).当然,我们可以使'em成为模态,但是从那时起,这将不是真正的MDI.
用MdiParent
替换Parent
:
Form2 frm = new Form2(); frm.StartPosition = FormStartPosition.CenterParent; frm.TopLevel = false; frm.MdiParent = this; frm.Show(); frm.BringToFront();
并确保您的主窗体已将IsMdiContainer
设置为true.
---------------
不要怪我们,因为您没有正确解释问题:
如果我从您的评论中很好地理解了,您可以使用以下三种形式:
1-MainForm
且IsMdiContainer
设置为true
2-ChildForm
,其中MdiParent
设置为MainForm
3-AnotherChildForm
.而且您希望这个人是ChildForm
的孩子.
如果这是正确的,那么您将无法摆脱该错误.要删除该错误,ChildForm
应该同时是MdiContainer
和MdiChild
.您必须将第3种形式设为MainForm
的子代,或者不使用形式(可能是UserControl
之类的东西).TopLevel
到false
?为什么要呼叫BringToFront()
?创建无模式表单时,它会自动显示(显示在最前面).
其次,听起来您不太了解MDI应用程序的性质,也不了解子窗口与其他形式之间的相互关系.
第三,如果要创建无模式窗口,则应执行以下操作:
公共 部分 class MyForm { MyModelessForm m_modelessForm = 空; 私有 无效 ShowModelessForm() { 如果(m_modelessForm == 空) { m_modelessForm = 新 ModelessForm(); // 必要时在此处配置表单 } m_modelessForm.Show(); m_modelessForm.BringToFront(); } }
假设您将Form1
的属性IsMdiContaine
r设置为true,您的代码应如下所示:
Form2 frm2 = new Form2(); frm2.MdiParent = this; frm2.Show();
Hi, all!
I have a problem with TextBoxes in my form.
Screenshot as picture http://rghost.ru/5011999/image.png
Let''s show the situation on a simple example.
1) Create a new winforms project with Form1;
2) Create a new Form2, put TextBox control onto it;
3) Let''s write simple code that creates the non-modal form;
Form frm = new Form();
frm.StartPosition = FormStartPosition.CenterParent;
frm.TopLevel = false; // here is the problem
frm.Parent = this;
frm.Show();
frm.BringToFront();
Once you execute this code, the form with textbox will appear.
Type something in textbox, e.g. "this is a test". Now, try to select a part or a whole text with mouse - you can''t, ''cause it doesn''t work.
Yes you can select the whole text with double click, but you can''t deselect it after that. Besides, what if you need to select only a part of the text to erase it?!
When the form is modal everything works fine.
The same situation for Memo and I suppose for some other text controls.
All this I need because we use serious MDI project, and inside each MDI form user can open different forms (e.g. search form). Of course, we can make ''em modal, but this won''t be the real MDI from that point.
Any sugesstions, please?
ReplaceParent
withMdiParent
:
Form2 frm = new Form2(); frm.StartPosition = FormStartPosition.CenterParent; frm.TopLevel = false; frm.MdiParent = this; frm.Show(); frm.BringToFront();
And make sure your main form hasIsMdiContainer
set to true.
---------------
Don''t blame us because you didn''t explain your problem properly:
If I understood well from your comments you have 3 forms:
1-MainForm
withIsMdiContainer
set to true
2-ChildForm
withMdiParent
set toMainForm
3-AnotherChildForm
. And you want this one to be a child ofChildForm
.
If this is correct, then you can''t get rid of the bug. To remove the bugChildForm
should be bothMdiContainer
andMdiChild
which is not possible. You must either make the 3rd form a child ofMainForm
, or not use a form (maybe aUserControl
or something).
First, what are you trying to accomplish by settingTopLevel
tofalse
? And why are you callingBringToFront()
? When you create a modeless form, it automatically shows up (it''s brought to the front).
Second, it sounds like you don''t quite understand the nature of a MDI application, nor the interrelationship between child windows and other forms.
Third, if you''re creating a modeless window, It should be done something like this:
public partial class MyForm { MyModelessForm m_modelessForm = null; private void ShowModelessForm() { if (m_modelessForm == null) { m_modelessForm = new ModelessForm(); // configure the form here if necessary } m_modelessForm.Show(); m_modelessForm.BringToFront(); } }
Assuming you set the propertyIsMdiContaine
r ofForm1
to true, your code should read like this:
Form2 frm2 = new Form2(); frm2.MdiParent = this; frm2.Show();
这篇关于当TopLevel = false时,文本控件不允许用鼠标选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!