简体   繁体   中英

Context Menu and Adding items to textbox c#

So, what i'm trying to do is this:


Have a textbox or item that will add clickable buttons to it. I don't know how to explain it fully, but basically for each file in a folder, add a button that when you click it will print the text inside that textfile.

To attempt to explain it better: One item on the form, and for every text file in a directory, it will add a contextmenu that has custom options to print it to the textbox.

Sorry if I didn't explain it fully, but I plan to rewrite this in a more reasonable way if needed.

My current code is

String path = AppDomain.CurrentDomain.BaseDirectory;
string[] files = Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);

foreach (string s in files)
{
      string file = s.Replace(path + "\\", "").Replace(path + "\\", "").Replace(path + "\\", "");
      richTextBox1.AppendText(file + "\n");
      ContextMenu cm = new ContextMenu();
      cm.MenuItems.Add(file)
}

http://i.imgur.com/gKaZqOY.png

it prints the text files in the directory fine, but I want to be able to right click them and add the text inside of them to go to the textbox.

I don't understand exactly you but you can load datas in a txtfile to maybe richTextBox like that:

 DialogResult result1 = openFileDialog1.ShowDialog(); // Show the dialog.
            if (result1 == DialogResult.OK) // Test result.
            {
                string file = openFileDialog1.FileName;
                try
                {
                    string line;
                    // Read the file and display it line by line.
                    System.IO.StreamReader file1 =
                       new System.IO.StreamReader(file, true);
                    while ((line = file1.ReadLine()) != null)
                    {
                        richTextBox1.LoadFile(file,RichTextBoxStreamType.PlainText);
                    }
                    richTextBox1.Clear();
                    file1.Close();
                }
                catch (System.IO.IOException)
                {
                    MessageBox.Show("The file could not be read:");

                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM