0

when i try to open a textfile with my text editor, drag the textfile onto my program.exe, the editor opens. But there is not text in the richtextbox. How do i get the text in there when i open it with my text editor?

I was thinking like this:

if (When u drag file on top of the .exe)
{
   richTextBox.Text = That file u dragged on top of the .exe;
}

Thank you in advance!

1 Answers1

0

you can do it like this:

1) go to your form class and add this constructor:

public Form1(string filename)
        {
            InitializeComponent();

            //get all the text of text file
            string[] readText = System.IO.File.ReadAllLines(filename);
            //insert the string array in your richtextbox
            foreach (string s in readText)
            {
                richTextBox1.Text += s + Environment.NewLine;
            }
        }

2) go to your Program class and change the code from your Main() method to this :

static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args[0] != null)
            {
                //args[0] exsists, so a file gets opened
                //args[0] is the file path
                Application.Run(new Form1(args[0]));   
            }
            else
            {
                //args[0] doesn't exsist
                Application.Run(new Form1());
            }
        }

make sure your Main method has the parameter 'string[] args'! if your method doesn't, just add it here

static void Main(string[] args)

3) build your project, go to your .exe file in the bin folder and test it by dropping a text file on top of the .exe (if i understand your question)

Zong
  • 5,701
  • 5
  • 27
  • 46
JoJo
  • 786
  • 1
  • 10
  • 25