Hello! In short, I make a text editor with tabs (so that on one tab you can work with one file, on the other - with another). There is a code to add new tabs and generate new TextBox on them.

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

But there was a problem - I do not know how to access the textbox in the active tab. For example, this is how I access 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?

    1 answer 1

     TextBox textbox = null; TabPage p = tabControl1.SelectedTab; foreach(Control c in p.Controls) { if (c is TextBox) { textbox = (TextBox)c; break; } } if (textbox != null) { // found } 
    • If not difficult, could you explain the meaning of this line: textbox = (TextBox) c; - StepanKo
    • This type conversion (conversion, casting) is from Control to TextBoxIgor
    • It is better to replace the entire cycle with TextBox textbox = p.Controls.OfType<TextBox>().FirstOrDefault(); - andreycha
    • @andreycha, I'm a beginner and I don’t really understand your version of TextBox textbox = p.Controls.OfType<TextBox>().FirstOrDefault(); code TextBox textbox = p.Controls.OfType<TextBox>().FirstOrDefault(); Could you explain to me how this code will be useful to me? Thank. - StepanKo
    • @StepanKo if you are a beginner, you can still not pay attention to it. And this is a more compact and, in my opinion, more readable version of the same cycle. - andreycha