C#在for循环中将richtextbox数组添加到TabPages数组中
我的代码是:C#在for循环中将richtextbox数组添加到TabPages数组中,c#,arrays,tabcontrol,C#,Arrays,Tabcontrol,我的代码是: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.For
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace inform
{
public partial class Form1 : Form
{
public static TabPage[] TabPages = new TabPage[20];
public static RichTextBox[] TextBoxes = new RichTextBox[20];
public Form1()
{
InitializeComponent();
TabControl.TabPages.Clear();
for (int x = 0; x < 19; x++)
{
TabPages[x].Controls.Add(TextBoxes[x]); //ERROR HERE
//Object reference not set to an instance of an object.
TabControl.TabPages.Add(TabPages[x]);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
而不是我的:
TabPages[x].Controls.Add(TextBoxes[x]);
但即使这样它也不起作用,我以前做过,但并没有数组,最后一次我做了6个标签,我想做更多。
我试着在互联网上阅读一些网页,但似乎没有任何效果,如果有任何帮助,我将不胜感激。你必须这样写
public partial class Form1 : Form
{
public static TabPage[] TabPages = new TabPage[20];
public static RichTextBox[] TextBoxes = new RichTextBox[20];
public Form1()
{
InitializeComponent();
tabControl1.TabPages.Clear();
for (int x = 0; x < 19; x++)
{
TabPages[x] = new TabPage();
TabPages[x].Controls.Add(TextBoxes[x]); //ERROR HERE
//Object reference not set to an instance of an object.
tabControl1.TabPages.Add(TabPages[x]);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
公共部分类表单1:表单
{
公共静态选项卡页[]选项卡页=新选项卡页[20];
公共静态RichTextBox[]文本框=新的RichTextBox[20];
公共表格1()
{
初始化组件();
tabControl1.TabPages.Clear();
对于(int x=0;x<19;x++)
{
TabPages[x]=新TabPage();
TabPages[x].Controls.Add(文本框[x]);//此处出错
//对象引用未设置为对象的实例。
tabControl1.TabPages.Add(TabPages[x]);
}
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
}
试试这个
for (int a = 0; a < 20;a++ )
{
RichTextBox textBox = new RichTextBox();
TextBoxes[a] = textBox;
TabPage tabPage = new TabPage();
TabPages[a] = tabPage;
}
for (int x = 0; x < 19; x++)
{
TabPages[x].Controls.Add(t);
TabControl.TabPages.Add(TabPages[x]);
}
for(int a=0;a<20;a++)
{
RichTextBox textBox=新的RichTextBox();
文本框[a]=文本框;
TabPage TabPage=新TabPage();
TabPages[a]=TabPages;
}
对于(int x=0;x<19;x++)
{
TabPages[x].Controls.Add(t);
TabControl.TabPages.Add(TabPages[x]);
}
for (int a = 0; a < 20;a++ )
{
RichTextBox textBox = new RichTextBox();
TextBoxes[a] = textBox;
TabPage tabPage = new TabPage();
TabPages[a] = tabPage;
}
for (int x = 0; x < 19; x++)
{
TabPages[x].Controls.Add(t);
TabControl.TabPages.Add(TabPages[x]);
}