当TopLevel = false时,文本控件不允许用鼠标选择文本 [英] Text controls doesn't let to select the text with mouse when TopLevel = false

查看:230
本文介绍了当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- MainFormIsMdiContainer设置为true
2- ChildForm,其中MdiParent设置为MainForm
3- AnotherChildForm.而且您希望这个人是ChildForm的孩子.

如果这是正确的,那么您将无法摆脱该错误.要删除该错误,ChildForm应该同时是 MdiContainer MdiChild.您必须将第3种形式设为MainForm的子代,或者不使用形式(可能是UserControl之类的东西). TopLevelfalse?为什么要呼叫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?

解决方案

Replace Parent with MdiParent:

Form2 frm = new Form2();
frm.StartPosition = FormStartPosition.CenterParent;
frm.TopLevel = false;
frm.MdiParent = this;
frm.Show();
frm.BringToFront();



And make sure your main form has IsMdiContainer 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 with IsMdiContainer set to true
2- ChildForm with MdiParent set to MainForm
3- AnotherChildForm. And you want this one to be a child of ChildForm.

If this is correct, then you can''t get rid of the bug. To remove the bug ChildForm should be both MdiContainer and MdiChild which is not possible. You must either make the 3rd form a child of MainForm, or not use a form (maybe a UserControl or something).


First, what are you trying to accomplish by setting TopLevel to false? And why are you calling BringToFront()? 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 property IsMdiContainer of Form1 to true, your code should read like this:

Form2 frm2 = new Form2();
frm2.MdiParent = this;
frm2.Show();


这篇关于当TopLevel = false时,文本控件不允许用鼠标选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆