用 RichTextBox 替换 WinForms TextBox [英] Replacing a WinForms TextBox with a RichTextBox

查看:20
本文介绍了用 RichTextBox 替换 WinForms TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文本框的 Windows 窗体应用程序.我想用新的 RichTextBox 对特定的 TextBox 进行一揽子替换.如果我从设计器中删除有问题的 TextBox,Visual Studio 2012 不会自动删除任何引用该文本框的代码吗?

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?

我认为一种方法是在 cs 文件中进行字符串替换,甚至包括 Designer.cs 文件,并找到包含控件名称 (txtQuestion) 的任何字符串并将其替换为新的名称RichTextBox 控件.

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.

我希望 Visual Studio 有更简单的方法来做到这一点.

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

推荐答案

我不知道一个真正简单的方法来做到这一点.也就是说,手动操作不应该很复杂:

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

  • 在 *.Designer.cs 文件中,应该只有两个地方可以更改类型:字段声明和创建新实例的位置.只需将类型名称从 TextBox 更改为 RichTextBox 即可.
  • 实际上没有任何需要更改控件的名称(即其字段的名称),但如果您真的想要,您可以更改控件的属性中的 Name 属性窗口,VS 将自动重构引用该字段的代码.任何明确引用该控件字段的代码都将自动更新.
  • 如果你真的很不走运,你会在代码中的一些地方将控件的对象引用转换为它的实际类型,例如在您使用 sender 参数而不是仅引用字段的事件处理程序中(例如,当同一个事件处理程序与多个控件一起使用时会发生这种情况).在这些地方,您将不得不自己跟踪并更改分配给正确类型的强制转换和变量.请注意,无论如何,IDE 都没有可靠的实用方法来可靠地完成这项工作.
  • 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.

TextBox 的成员很少(如果有的话)不只是继承或覆盖 TextBoxBase 的成员,TextBox 是在 TextBoxcode> 和 RichTextBox,所以所有实际使用字段的代码现在是一个 RichTextBox 应该正常工作".我想不出任何可能的异常,但如果它们存在,它们应该很少,并且只需编译代码即可轻松找到.

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.

可能像 Resharper 这样的工具可以使上述一些事情变得更容易.我不确定…我自己不使用它.但是,如果上述内容看起来过于繁琐,您可以查看他们是否有试用版,看看它在这种情况下是否有帮助.

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.

这篇关于用 RichTextBox 替换 WinForms TextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆