且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

文本编辑器问题

更新时间:2022-04-28 08:52:05

在TXTEditor类中定义两个构造函数



例如:
Define two constructor in your TXTEditor class

eg:
//which is generated by default
public TXTEditor()
{
InitializeComponent();
}

//new Contructor
public TXTEditor(string fileName)
{
//your code goes here
}


谢谢你帮助解决了问题Jibesh



Thank you helped solve the problem Jibesh

public Form1()
       {
           InitializeComponent();
       }
       public Form1(string filename)
       {
           InitializeComponent();

           if (filename != null)
           {
               try
               {
                   //textBox1.LoadFile(filename); remove this line
                   StreamReader reader = new StreamReader(filename);
                   char[] data = new char[filename.Length];
                   reader.ReadBlock(data, 0, (int)filename.Length);
                   textBox1.Text = new string(data);
                   reader.Close();// and add these lines
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               }
           }
}