5

I have a windows forms application that contains a TextBox. I would like to do a blanket replace of a particular TextBox with a new RichTextBox. If I delete the TextBox in question from the designer, won't Visual Studio 2012 automatically delete any code that references the textbox?

Is there a way to do this gracefully without jeopardizing the code that is already there?

I'm supposing that one way is to do a string replace in the cs files, including even, the Designer.cs files and find any strings that contain the control's name (txtQuestion) and replace it with the name of the new RichTextBox control.

I was hoping Visual Studio would have a simpler way to do this.

3
  • only the codes related to design will be deleted. nothing else. not the events and even the part of the code uses the textbox. you just have to make new richtextbox name the same as textbox name Commented Jul 10, 2015 at 0:00
  • 1
    You won't lose the code, but you will lose the event wiring. VB.Net makes this easier, but in c#, I would just go into the Designer file and replace the two declarations that are there for the TextBox.
    – LarsTech
    Commented Jul 10, 2015 at 0:08
  • You may refactor the TextBox name to a new Name (choose a character string that cannot be found elsewhere in your project, e.g "txtQuestionOldVersion"). Create the RichTextBox with the name "txtQuestion". Then operate a replace all on the form.cs file(not on all project files) to change "txtQuestionOldVersion" to "txtQuestion". You may the safely remove the "txtQuestion" control.
    – Graffito
    Commented Jul 10, 2015 at 0:16

1 Answer 1

4

I'm not aware of a truly simple way to do this. That said, doing it manually should not be very complicated:

  • In the *.Designer.cs file, there should be just two places to change the type: the field declaration, and where the new instance is created. Just change the type name from TextBox to RichTextBox there.
  • There is not actually any need to change the name of the control (i.e. the name of its field), but if you really want to, you can just change the Name property in the control's Properties window, and VS will automatically refactor the code referring to that field. Any code referring explicitly to that control's field will automatically be updated.
  • If you're really unlucky, you'll have some places in the code where you cast an object reference to the control to its actual type, e.g. in an event handler where you use the sender parameter instead of just referring to the field (something that happens when e.g. the same event handler is used with more than one control). Those places, you will have to track down yourself and change the cast and variable to which it's assigned to the correct type. Note that it's not like the IDE would have a practical way of reliably doing that work anyway.

There are few if any members of TextBox that aren't just inherited from or overrides of members of TextBoxBase, the base class shared between TextBox and RichTextBox, so all of the code actually using the field which is now a RichTextBox ought to "just work". I can't think of any possible exceptions off the top of my head, but if they exist they should be very few, and will be easy to find simply by compiling the code.

It's possible a tool like Resharper could make some of the above easier. I'm not sure…I don't use it myself. But if the above seems too onerous, you might look to see if they have a trial version and see if it helps in this scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.