using System;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using   System.IO;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace mlearning
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // 向上导航
                // 向上键
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // 向下导航
                // 向下键
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // 向左键
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // 向右键
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }        }        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            
        }        private void menuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }        private void label1_ParentChanged(object sender, EventArgs e)
        {
            label1.Text = ListLines[0, 0];
        }        private void Form2_Load(object sender, EventArgs e)
        {
            
           
            }                class Init
     {
                TextReader tr = new StreamReader("G:\text.txt", Encoding.GetEncoding("gb2312"));         public int NumberOfLines = 5;         public string[,] ListLines;
         public string[] aryTemp;         public void init()
         {             ListLines = new string[NumberOfLines,9];             for (int i = 0; i < NumberOfLines; i++)
             {
                 aryTemp = tr.ReadLine().Split('|');                 //Console.WriteLine(aryTemp[0]);                    ListLines[i, 0] = aryTemp[0];
                 ListLines[i, 1] = aryTemp[1];
                 ListLines[i, 2] = aryTemp[2];
                 ListLines[i, 3] = aryTemp[3];
                 ListLines[i, 4] = aryTemp[4];
                 ListLines[i, 5] = aryTemp[5];
                 ListLines[i, 6] = aryTemp[6];
                 ListLines[i, 7] = aryTemp[7];
                 ListLines[i, 8] = aryTemp[8];        
                              tr.Close();             }
         }
              }
 }
    }
错误是 label1.Text = ListLines[0, 0];  提示当前上下文中不存在名称“ListLines”,请问怎么改啊?我主要是想在label1中显示txt中用“|”分割的一小部分内容

解决方案 »

  1.   

    你类没有实例化·
    ListLines 在类init里面·
     init myinit = new init();
    label1.Text = myinit.ListLines[0, 0];  
      

  2.   

    你的ListLines是类 Init里面,你实例化一个Init 类,然后在访问,如下: 
    private void label1_ParentChanged(object sender, EventArgs e) 
            { 
                Init it=new Init();
                label1.Text = it.ListLines[0, 0]; 
            } 
      

  3.   

    按照3楼的做了之后又说 Init 类中TextReader tr = new StreamReader("G:\text.txt", Encoding.GetEncoding("gb2312")); 这一行“值不在预期的范围内”呢??
      

  4.   

    按照您的做了之后又说 Init 类中TextReader tr = new StreamReader("G:\text.txt", Encoding.GetEncoding("gb2312")); 这一行“值不在预期的范围内”呢??