0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Prototype
{
    public partial class Form1 : Form
    {
        object oDocument;
        int thmbNailCnt = 0;
        GroupBox[] thmbNail = new GroupBox[100];
        PictureBox[] picBox = new PictureBox[100];
        TextBox[] texBox = new TextBox[100];

        public Form1()
        {
            InitializeComponent();            
        }

        private void addWordToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void scheduleToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button9_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx)|*.doc;*.docx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;
                newThumbNail(1, sFileName);
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            oDocument = webBrowser1.Document;

        }

        private void button8_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.xls, *.xlsx)|*.xls;*.xlsx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.ppt, *.pptx)|*.ppt;*.pptx";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Office Documents " + " " + "(*.pdf)|*.pdf";
            openFileDialog1.FilterIndex = 1;
            System.Windows.Forms.HtmlDocument document;
            string sFileName;
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            sFileName = openFileDialog1.FileName;

            if (sFileName.Length != 0)
            {
                oDocument = null;
                webBrowser1.Navigate(sFileName);
                document = webBrowser1.Document;

            }
        }

        private void newThumbNail(int docType, string fileName)
        {




            thmbNail[thmbNailCnt].Visible = true;            
            thmbNail[thmbNailCnt].Location = new Point(2, 5);
            thmbNail[thmbNailCnt].Size = new Size(222, 50);


            picBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
            picBox[thmbNailCnt].Visible = true;
            picBox[thmbNailCnt].Location = new Point(6, 13);
            picBox[thmbNailCnt].Size = new Size(31, 31);
            picBox[thmbNailCnt].Image = new Bitmap("images/excel.png");

            texBox[thmbNailCnt].Parent = thmbNail[thmbNailCnt];
            texBox[thmbNailCnt].Visible = true;
            texBox[thmbNailCnt].Location = new Point(53, 24);
            texBox[thmbNailCnt].Size = new Size(163, 20);
            texBox[thmbNailCnt].Text = fileName;
            texBox[thmbNailCnt].Enabled = false;

            this.Controls.Add(thmbNail[thmbNailCnt]);
            this.Controls.Add(picBox[thmbNailCnt]);
            this.Controls.Add(texBox[thmbNailCnt]);


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }



    }
}

我的程序在进入函数 private void newThumbNail(int docType, string fileName) 时会抛出空引用错误。请帮忙。我相信我已经正确地声明了 groupBox、textBox 和 picture Box。但我不确定我是否声明了一个有效的 groupBox 数组。

4

3 回答 3

3

您已经声明并创建了数组,因此您的数组确实不是 NRE 的原因。但是,数组填充了什么?您在哪里指定数组的内容具有非空值?例如,您需要以下内容:

thmbNail[0] = new GroupBox(...)

可能您希望数组在添加到末尾时自动扩展。C# 不支持数组,所以你最好使用 aList<GroupBox>而不是 a GroupBox[]

于 2012-03-20T22:12:11.460 回答
2

您声明 thmbNail 并给它一个长度,但您没有填充它的任何元素。所以thmbNail[thmbNailCnt]返回一个空值,然后当您尝试访问它的Visible属性时抛出 NullReferenceException。也许您应该首先为其分配一个新值:

if (thmbNail[thmbNailCnt] == null)
    thmbNail[thmbNailCnt] = new GroupBox();
thmbNail[thmbNailCnt].Visible = true;

picBox 和 texBox 数组也会有同样的问题。还记得在创建时将它们添加到您的表单中。

于 2012-03-20T22:12:36.950 回答
1

您所做的就是声明一个控件数组。

GroupBox[] thmbNail = new GroupBox[100];

要使用一个,您需要实例化它,例如

GroupBox[thmbnailcount] = new GroupBox();

picbox 和文本框也是如此。

和做没有区别

GroupBox myGroupBox;

你所做的就是告诉编译器变量的类型。它不是一个值类型,所以在你尝试使用它之前,你必须在那里获得一个新的。

于 2012-03-20T22:17:27.947 回答