简体   繁体   English

以编程方式将控件添加到TabPage

[英]Programmatically Add a Control to a TabPage

All, I want to add a custom RichTextBox to a WinForm TabPage . 全部,我想将自定义RichTextBox添加到WinForm TabPage I have tried various things illustrated by the code below 我尝试过以下代码说明的各种事情

TabPage tmpTabPage = new TabPage("Test");
tabControl1.TabPages.Add(tmpTabPage);

CustomRichTextBox tmpRichTextBox = new CustomRichTextBox();
tmpRichTextBox.LoadFile(@"F:\aaData\IPACostData\R14TData\ACT0\1CALAEOSAudit_log.rtxt");

// Attempted FIX.
tabControl1.SuspendLayout();
tabControl1.TabPages["Test"].Controls.Add(tmpRichTextBox); // This throws a NullReferenceException??
tabControl1.ResumeLayout();

tmpRichTextBox.Parent = this.tabControl1.TabPages["test"];

tmpRichTextBox.WordWrap = tmpRichTextBox.DetectUrls = false;
tmpRichTextBox.Font = new Font("Consolas", 7.8f); 

tmpRichTextBox.Dock = DockStyle.Fill;
tmpRichTextBox.BringToFront();

Before I added the "aAttempted FIX", the code would run without exception but the CustomRichTextBox would not appear. 在我添加“aAttempted FIX”之前,代码将毫无例外地运行,但不会出现CustomRichTextBox Now I get the NullReferenceException and I am confused over both situations. 现在我得到了NullReferenceException ,我对这两种情况感到困惑。 What am I doing wrong here? 我在这做错了什么?

What you're really missing is setting the "Name" property for your TabPage variable. 您真正缺少的是为TabPage变量设置“Name”属性。 The string you're passing to TabPage's constructor is only setting the TabPage.Text property. 您传递给TabPage的构造函数的字符串仅设置TabPage.Text属性。

Just add the following code after instantiating your TabPage and you should be fine: 只需在实例化TabPage后添加以下代码,你应该没问题:

TabPage tmpTabPage = new TabPage("Test");
tmpTabPage.Name = "Test"
// Rest of your code here

The reason you are getting the NullReferenceException is because the following code: 您获得NullReferenceException的原因是因为以下代码:

tabControl1.TabPages["Test"]

is not returning a reference to the TabPage, because the TabPage's "Name" property was not set. 没有返回对TabPage的引用,因为没有设置TabPage的“Name”属性。

Try it like this by adding the tmpRichTextBox to tmpTabPage and then adding tmpTabPage to tabControl1 通过将tmpRichTextBox添加到tmpTabPage然后将tmpTabPage添加到tabControl1来尝试这样

        TabPage tmpTabPage = new TabPage("Test");
        CustomRichTextBox tmpRichTextBox = new CustomRichTextBox();
        tmpRichTextBox.LoadFile(@"F:\aaData\IPACostData\R14TData\ACT0\1CALAEOSAudit_log.rtxt");

        // Attempted FIX. 
        tmpTabPage.SuspendLayout();
        tmpTabPage.Controls.Add(tmpRichTextBox); // This throws a NullReferenceException?? 
        tmpTabPage.ResumeLayout();

        tmpRichTextBox.Parent = tmpTabPage;

        tmpRichTextBox.WordWrap = tmpRichTextBox.DetectUrls = false;
        tmpRichTextBox.Font = new Font("Consolas", 7.8f);

        tmpRichTextBox.Dock = DockStyle.Fill;
        tmpRichTextBox.BringToFront();

        //tmpTabPage.Controls.Add(tmpRichTextBox);
        tabControl1.TabPages.Add(tmpTabPage); 

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

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