I need to implement a tabbed text editor. Well, the type so that you can work with several files (one file is open in one tab, the other in another), for example, in AkelPad. I wrote a rather clumsy (and, as it turned out, not working) code. The fact is that the TextBox in a new tab does not receive the text from the open file, i.e. it remains empty. Can you tell me who knows what's the matter, what can be done here? You are welcome ;)

One more thing! If it is not difficult, how to stretch the generated text field to the full screen programmatically? Well, that the Dock property was Fill?)) Thank you.

Here is my code:

private void открытьВНовойВкладкеToolStripMenuItem_Click(object sender, EventArgs e) { // Выбор файла (для новой вкладки): openFileDialog1.ShowDialog(); if (openFileDialog1.FileName == null) return; // Ничего не делаем, если файл не выбран // Читаем... try { using (StreamReader sr = new StreamReader(openFileDialog1.FileName)) { TabPage p = new TabPage(openFileDialog1.FileName); // Присваиваем названию новой вкладки название открытого файла tabControl1.TabPages.Add(p); p.Controls.Add(new TextBox()); TextBox textbox = new TextBox(); textbox.Location = new Point(100, 100); // Местоположение нового TextBox textbox.Visible = true; // Видимость textbox.Multiline = true; // Многострочность textbox.Text = sr.ReadToEnd(); // Текст нового TextBox присваивает текст открытого файла sr.Close(); // Всем спасибо, все свободны } } catch (Exception Unknow_error) // Если ошибка (неизвестная) { MessageBox.Show(Unknow_error.Message, "Неизвестная ошибка", MessageBoxButtons.OK); // Показываем сообщение } } 
  • Do not forget to specify in tags what technology of working with windows you use, WinForms or WPF. They are very different. - rdorn
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

  1. Why does the textbox remain empty?

p.Controls.Add (new TextBox ());
TextBox textbox = new TextBox ();

Everything is simple, you put one textbox in a tab, and after that you create another one and set it up already, and put the text in it. The fix is ​​simple:

 ... TextBox textbox = new TextBox(); p.Controls.Add(textbox); ... 
  1. You can set the Dock property like any other by simply assigning the desired value to textbox.Dock = DockStyle.Fill;
  • Thanks for your reply! Everything is working. But I would like to clarify how I can access the textbox in the active tab? Here, for example, so I get access to the active tab 'tabControl1.SelectedTab', and how can I get to the textbox to save / print / clear text from the textbox on the active tab? - StepanKo
  • @StepanKo and this is better as a separate issue. In a nutshell it will not work, there are options. - rdorn