I recently started learning C #, and so far, unfortunately, I don’t know all the subtleties and possibilities. I study in principle through trial and error, and sometimes I look in Google for searching for an answer. So, I come to the question: is it possible to set child variables in C # of controls? I make a simple tabbed text editor, and I need to assign a mutable isSaved flag to each tab item RichTextBox for manipulations. I will try to give an example of what I want:

RichTextBox TE = new RichTextBox() { Dock = DockStyle.Fill, Name = "text_edit", Location = new Point(3, 3), Text = content, Font = Properties.Settings.Default.GlobFont, Margin = new Padding(3, 3, 3, 3), /* а вот и то, что мне нужно */ bool isSaved; /* собственное значение */ }; 
  • 3
    You need to inherit from RichTextBox and create your own class, which will contain the bool isSaved field - Leonid
  • 3
    You approach the task incorrectly. Being saved or not is not a property of the control. You must have a data structure describing the document , and in it isSaved field, as well as the contents of the document (and, possibly, more fields). - VladD
  • 2
    UI element should not contain business logic - Vadim Prokopchuk

1 answer 1

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); RichTextBoxMy TE = new RichTextBoxMy(); TE.isSaved = true; } } public class RichTextBoxMy : RichTextBox { public bool isSaved; } 
  • Yes, and the truth is not the right approach. What I did was just sketching, since I adapted to the syntax of C # after Lua. Perhaps, I will rewrite the text editor again, having already correctly thought through the entire organization of the program in terms of code. Thanks for answers! - InternalPower