There is a project in which there are about 30 .cs files, each of which has the same code, be it font initialization, copy function, etc. How can this be taken out in one place and called in one line? Not to create .dll
- And what prevents to allocate the same code in a separate class / method? - Alexey Sarovsky
- @ AlekseySarovsky in some forms has a richtextbox, and in class he does not see him - anton
- Communication with richtextbox is left in the form, the general code is to take classes and their methods. Methods are already called in forms, thus reducing duplication. - Monk
- Create a class in which you implement the common code (for the same RichTextBox), and from it inherit those in which this code is needed. - Alexey Sarovsky
|
1 answer
I can advise you to convert the data into a "universal view", which is then easily superimposed on API calls. For example, copying to the buffer can be implemented as:
public static void CopyToClipboard(ClipboardObject copiedText) { var type = copiedText.ObjectType; //в буфер обмена можно помещать как форматируемый, так и не форматируемый текст, у них будут разные типы var data = copiedText.Data; /*Win API calls, etc.*/ } public static ClipboardObject ExtractText(TextBox textBox) { return new ClipboardObject(textBox.Text, ClipboardObjectType.Simple); // ClipboardObjectType - это отдельный наш enum. } public static ClipboardObject ExtractText(RichTextBox textBox) { return new ClipboardObject(???, ClipboardObjectType.Html); // ClipboardObjectType - это отдельный наш enum. } Further in the event of the button there will be something like:
public void OnCopyButtonClick(object sender, EventArgs a) { var obj = ClipboardManager.ExtractText(this.textArea); ClipboardManager.CopyToClipboard(obj); } I strongly reduced the code to leave the idea.
|