如何在c#.net中对UI进行硬编码 [英] How to hardcode a UI in c#.net

查看:104
本文介绍了如何在c#.net中对UI进行硬编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C#.net中对ui进行硬编码,所以我创建了一个Windows窗体项目并保持默认窗体并创建了一个新类。在那个课程中,我试图硬编码一个按钮

ex:

button b1 = new button()

b1.text =click me ;



但是在这里当我尝试使用b1.text它会出错。

我无法随时查阅按钮。我必须在哪里使用主要方法?

在主.cs文件中?还是在我创建的课程中?



因为我是C#.net的新手,我在编码时遇到了一些麻烦。

可以你对我有所帮助吗?



我尝试过:



我试图在其中创建一个新类和一个方法,并尝试从默认的.cs文件中调用该方法。

解决方案

你无法学到像通过在论坛上提问来从头开始形成发展。最好的办法是获得一本关于使用c#开发Windows窗体的书籍,并继续学习基础知识。一旦你了解了基础知识,你可以问一下你是否遇到了一个特定问题。


OK ...停止你想要做的事。

你不要在大多数WinForms应用程序中都需要触摸main方法 - 它只包含运行应用程序所需的内容,并启动代码,为您幕后处理UI。



您遇到的问题可能有两个:

1)C#区分大小写:按钮与基于类的按钮不同在控制上,可以显示在表格上。

2)你的新课程可能只是声明为:

  public   class  MyClass 
{
...
}

这意味着它不是可以显示控件的类,也不能显示它本身。它也不会包含必要的使用语句来引用类中的控件(所以当你尝试使用Button时会遇到问题因为编译器不知道是什么虽然可以通过添加以下内容轻松修复:

 使用 System.Windows.Forms ; 

到您的类文件的顶部,它仍然无法显示它们,因此用户无法与它们进行交互。



相反,转储您的类,然后返回默认表单,并在设计模式下查看它(双击解决方案资源管理器窗格中的表单名称)

如果您还没有完成然后,在菜单栏中显示工具箱:查看,然后单击工具箱。将其拖到屏幕左侧,使其不在路上但仍然可见。

在工具箱中打开所有Windows窗体分支,然后将Button拖到窗体上。把它放在那里。

使用属性窗格更改它的名称butMine并且不要发短信给Click Me!

然后双击按钮,它会添加一个点击处理程序。向处理程序添加一行代码:

  private   void  butMine_Click( object  sender,EventArgs e)
{
MessageBox.Show( 感谢您点击此按钮。);
}

现在当你运行你的应用程序时,你会看到按钮,你点击它就会响应你。



如果你想知道它是如何创建和添加的,那么在解决方案资源管理器中查看默认类并打开表单分支 - 它下面会有三个项目:

 xxx.Designer.cs 
xxx.resx
xxx

其中xxx是表格的名称。

双击xxx.Designer.cs行,它将为您打开源代码,您可以看到您的按钮是如何创建和放置的。请勿更改此文件!这很容易弄错,然后你的表单不会加载到设计器中!看 - 但不要触摸,直到你知道你在做什么! :笑:


I want to hardcode a ui in C#.net, So i created a windows forms project and kept the default form as it is and created a new class. In that class i tried to hardcode a button
ex:
button b1 = new button()
b1.text = "click me";

but here when i try to use b1.text it gives an error.
And i cannot refer to the button at any time. And where do i have to have the main method?
in the main .cs file? or in the class i created?

Since i'm new to C#.net i'm struggling a bit with coding.
Can you help me with this?

What I have tried:

I have tried to create a new class and a method inside it and tried to call the method from the default .cs file.

解决方案

You can't learn something like forms development from scratch by asking questions on a forum. Your best bet is to get a book on developing Windows Forms with c# and go through to learn the basics. Once you know the basics you can ask if you are stuck on a particular issue.


OK...stop what you are trying to do.
You don't need to touch the main method in most WinForms applications - it contains just the stuff you need to get your application running, and start the code which handles the UI "behind the scenes" for you.

The problem you are meeting is probably two fold:
1) C# is case sensitive: "button" is not the same thing as "Button" which is a class based on Control and can be displayed on a Form.
2) Your new class is probably just declared as:

public class MyClass
   {
   ...
   }

Which means that it isn't a class that can display controls, nor can it itself be displayed. It also won't include the necessary using statements to let you reference Controls within your class (so you get problems when you try to use Button because the compiler doesn't know what it is), and while that can be fixed easily by adding:

using System.Windows.Forms;

to the top of your class file, it still won't be able to display them so the user can't interact with them.

Instead, dump your class, and go back to the "default" Form, and view it in Design mode (double click the form name in the Solution Explorer pane)
If you haven't done it yet, display the Toolbox: "View" from the Menu bar, then click on "ToolBox". Drag it over to the left of the screen so it's out of the way but still visible.
Open the "All Windows Forms" branch in the toolbox, and drag a Button onto your form. Drop it there.
Use the Properties Pane to change it's Name "butMine" and it't Text to "Click Me!"
Then double click on the button, and it will add a click handler. Add a line of code to the handler:

private void butMine_Click(object sender, EventArgs e)
    {
    MessageBox.Show("Thank you for clicking this button.");
    }

Now when you run you application, you will see the button, and it you click on it it will respond to you.

If you want to know how it was created and added, then look at the default class in the Solution Explorer and open the form branch - there will be three items under it:

xxx.Designer.cs
xxx.resx
xxx

where "xxx" is the name of your form.
Double click the "xxx.Designer.cs" line and it will open the source code for you and you can see how your button was created and placed for you. DO NOT CHANGE THIS FILE! It is really easy to get it wrong, and then your form won't load in the designer! Look - but don;t touch until you know what you are doing! :laugh:


这篇关于如何在c#.net中对UI进行硬编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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