简体   繁体   English

停靠2个富文本框

[英]Docking 2 rich text boxes

I am trying to make a text editor and I have 2 rich text boxes. 我正在尝试制作文本编辑器,但我有2个富文本框。 I am uses the 1st rich text box as a number like and setting its Enabled property to false. 我将第一个RTF文本框用作数字,并将其Enabled属性设置为false。 Then the second text box is going next to it 然后第二个文本框在它旁边

I have currently set the dock of the first text box to left and the second one to fill. 我目前已将第一个文本框的停靠栏设置为左侧,将第二个文本框的停靠栏设置为填充。 But the 2nd one keeps taking up the whole tabpage? 但是第二个继续占据整个标签页吗? And going slightly more left towards the number line and its hidden under there a little bit. 向数字线左移一点,并隐藏在数字线下面。 Here is my create new document void I have... T2 is the number line and T is the default text box they will type into. 这是我创建的新文档,我有... T2是数字行,T是将在其中键入的默认文本框。

TabPage t = new TabPage("new " + getNumber());
tabControl1.TabPages.Add(t);
tabControl1.SelectedTab = t;
RichTextBox T2 = new RichTextBox();
t.Controls.Add(T2);
T2.Dock = DockStyle.Left;
T2.Enabled = false;

RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
Random R = new Random();
int RandomNumberHere = R.Next(1000, 100000);
T.Text = "Welcome, type your text...";
T.Select();

The problem is that you define your fill-docked text box at the end of the control list... winforms likes the fill-docked elements first in the list. 问题是您在控件列表的末尾定义了fill-docked文本框... winforms喜欢列表中第一个填充对接的元素。

Easily solved: 轻松解决:

RichTextBox T = new RichTextBox();
t.Controls.Add(T);
T.Dock = DockStyle.Fill;
T.Font = new Font("Microsoft San Serif", 11);
// Add this line
T.BringToFront();

Or you could also do T2.SendToBack(); 或者,您也可以执行T2.SendToBack(); after T is added to the controls collection. T添加到控件集合之后。

Or you could simply create (and add to t.Controls ), the fill-docked textbox first, and the left-docked textbox second. 或者,您可以简单地创建(并添加到t.Controls ),首先创建填充文本框,然后创建左侧的文本框。

Either way works 无论哪种方式

By the way, try to name your variables correctly. 顺便说一句,尝试正确命名变量。 t , T , T2 are just not good names tTT2只是不好的名字

Here you go: 干得好:

t.Controls.Add(T2);
t.Controls.Add(T);

t.Controls.SetChildIndex(T, 1);
t.Controls.SetChildIndex(T2, 0);

t.PerformLayout();  // needed after SetChildIndex!

T2.Dock = DockStyle.Left;    
T.Dock = DockStyle.Left;

If you want your boxes to grow with the TabPage here is one way to do it: 如果您希望盒子随TabPage一起增长,这是一种实现方法:

private void t_Resize(object sender, EventArgs e)
{
   // assuming you want the two RTBs to fill the TabPage
   // if you want something else, best add an anchored container panel
   // and use its resize event instead
   T2.Width = t.Width / 4;
   T.Width = t.Width / 4;
}

And yes, t, T and T2 are really bad names! 是的, t, T and T2真是个坏名字!

There's a few things going on: 有几件事发生:

Fill does just that; 填充就是这样做的; it Fills the entirety of the PARENT control. 它填充了整个PARENT控件。 Whether anything else is there or not. 是否还有其他东西。

Your other text box will be hidden until it is set to Enabled. 您的其他文本框将被隐藏,直到将其设置为“已启用”。

What you Might? 你可能会怎样? be looking for is a way to have the text box size itself based on the size of the tab page being created: 寻找一种方法是根据创建的选项卡页面的大小来确定文本框的大小:

    {

        TabPage t = new TabPage("new " + 1);
        tabControl1.TabPages.Add(t);
        tabControl1.SelectedTab = t;
        RichTextBox T2 = new RichTextBox();
        t.Controls.Add(T2);
        T2.Dock = DockStyle.Left;
        T2.Enabled = true ;

        RichTextBox T = new RichTextBox();
        t.Controls.Add(T);
        T2.Dock = DockStyle.Right;
        var AdjustedSize = T2.Size;
        AdjustedSize.Width = t.Size.Width * 2 / 3;
        T2.Size = AdjustedSize;
        T.Font = new Font("Microsoft San Serif", 11);
        Random R = new Random();
        int RandomNumberHere = R.Next(1000, 100000);
        T.Text = "Welcome, type your text...";
        T.Select();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM