从字符串重命名按钮
我正在尝试从字符串重命名按钮。听起来很简单吗?好吧,我浏览了互联网并尝试了很多方法,但我不断出现同样的错误。
我有 2 份表格和一份班级文件。我正在使用面向对象将字符串从文本框传递到 form1,其中可以通过我的“参考类”传递它来更改“button1.Text”(我不认为可以通过任何其他方式完成)
private void button1_Click_1(object sender, EventArgs e)
{
Refclass Ref = new Refclass();
String but1 = Ref.but1;
String btn = "button1"; this.Controls[btn].Text = but1;
}
我确信这可能是错误的,但我希望借此能够理解我想要做什么。我正在从“Ref”类调用一个字符串,并调用该字符串“hell”,
不用说,我要么遇到调试错误并完全崩溃 Visual Studio,要么收到一条错误消息“对象引用未设置为对象的实例” ”。
我知道我在某个地方出错了,有人知道哪里吗?谢谢。
I am trying to rename a button from a string. Sounds simple enough? Well I have scowered the internet and tried many things however I keep coming up with the same errors.
I have 2 forms and one class file. I am using object orientation to pass a string from a textbox to form1 where upon the "button1.Text" can be change passing it through my "Reference class" (I don't think it can be done any other way)
private void button1_Click_1(object sender, EventArgs e)
{
Refclass Ref = new Refclass();
String but1 = Ref.but1;
String btn = "button1"; this.Controls[btn].Text = but1;
}
I am sure this is probably wrong but I hope by this might be able to understand what I am trying to do. I am calling a string from the "Ref" class and calling the string "hell"
Needless to say I am either getting a debugging error and totally crashing visual studio or I get an error saying "Object reference not set to an instance of an object."
I know I am going wrong somewhere does anyone know where? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要为类创建对象。如果您的类在同一个程序集中。只需像这样调用即可。
其中 but1 是该类中的 const 字符串。
there is no need of create the object for class.if your class in same assembly.just call like this.
where but1 is a const string in that class.