简体   繁体   English

计算机名称程序,可视C#

[英]Computer name program, visual C#

I'm very new to Visual C# I've already made a calculator, following a tutorial. 我是Visual C#的新手,已经按照教程制作了计算器。

Now, I'm trying to make a simple program, when run will bring up a box that shows what the computer name is. 现在,我正在尝试制作一个简单的程序,运行时会弹出一个框,显示计算机的名称。 I have an idea on how to do it, but I'm still learning the C# syntax. 我对如何实现有一个想法,但是我仍在学习C#语法。

I have made the box that pops up and it says, "This Computer's Name is:" then I made a blank label, and I want to be able to change the label to the computer name using System.Environment.MachineName 我制作了一个弹出框,上面写着“此计算机的名称为:”,然后我做了一个空白标签,并且希望能够使用System.Environment.MachineName将标签更改为计算机名称。

Here is what I have: 这是我所拥有的:

private void name_TextChanged(object sender, EventArgs e)
    {
        name.Text = System.Environment.MachineName;
        //name being the name of the label
    }

The labels text change event doesn't fire until something changes it's text. 标签文本更改事件在某些内容更改为文本之前不会触发。

Instead use the Forms Load event to do it, then use that line of code you have. 而是使用Forms Load事件来执行此操作,然后使用该行代码。

Firstly you want to create a form load event handler, you do this by double clicking on any blank space in the form, it will take you to the code window that is where you will want to write the following statement. 首先,您要创建一个表单加载事件处理程序,方法是双击表单中的任何空白,这将带您进入要在其中编写以下语句的代码窗口。

The form load event is executed before the form is displayed on screen, therefore your machine name will be displayed as soon as the window is visible. 窗体加载事件在窗体显示在屏幕上之前执行,因此,您的计算机名称将在窗口可见后立即显示。

Lets assume your form is called Form1 假设您的表单称为Form1

this is what your code will look like : 这是您的代码将如下所示:

namespace Machine_Name
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // This is the form load event handler
        private void Form1_Load(object sender, EventArgs e)
        {
            yourLabel.Text = Environment.MachineName;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM