使用代码制作新按钮 [英] Making A New Button Using Code

查看:77
本文介绍了使用代码制作新按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我一直在开发自己的编程语言,并感谢一些非常乐于助人的人,现在我可以使用我的编程语言创建Windows窗体,仅此而已.我需要能够向其中添加按钮和文本框之类的东西.首先是按钮,这是我当前的代码:

hey guys,

i have been making my programming language and thanks to some very helpful people, i am now able to create windows forms using my programming language, but that is all. i need to be able to add things to it like buttons and text boxes. the first thing is buttons and this is my current code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace programming_language_test_1
{
    class Program : System.Windows.Forms.Form
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(args[0]);
            string code = sr.ReadToEnd();
            Run(code);
        }

        static void Run(string code)
        {
            code = code.Replace(((char)13).ToString(), "");

            foreach (string a in code.Split('\n'))
            {
                if (a == "new form")
                {
                    Form form = new Form();
                    form.Activate();
                    form.Enabled = true;
                    form.Focus();
                    Application.EnableVisualStyles();
                    Application.Run(form);
                }

                if (a == "new button")
                {
                    Button button = new Button();
                    button.Visible = true;
                    button.Enabled = true;
                    button.Focus();
                    button.Text = "i am a button";
                    button.Location = new Point(50, 50);
                }
            }
        }
    }
}



1.制作一个新的控制台应用程序
2.添加Windows窗体
3.将此代码添加到


如果您打算使用以下方法来测试此代码:



1. make a new console application
2. add a windows form
3. add this code in


if you were going to test this code out by using:

new form
new button



您很快就会看到创建了一个新表单,但是该按钮不在表单上.我需要制作按钮以及表单上所有内容的帮助.



you would soon see that a new form is created, but the button isn''t on the form. I need help on making buttons and everything on the form.

推荐答案

您对此有错误的做法.
按钮类必须先分配给表单,然后才能出现.
喜欢

You have the wrong approach for this.
A Button class has to be assigned to a Form before it appears.
Like

Form f = new Form();
Button b = new Button();

f.Controls.Add(b);



创建您的Form,然后在From的构造函数中创建Button.
您可以为自己做的最好的帮助就是在Visual Studio中使用Form项目启动一个新项目.
从那里可以看到Visual Studio Form设计器正在制作的所有代码.

祝您好运:-)



Create you Form, and in the constructor of the From you then create the Button.
The best help you can do for your self, is to start a new project in Visual Studio with a Form project.
From there you can see all the code that Visual Studio Form designer is making.

Good luck :-)


嘿,您必须将按钮添加到表单中..
Hey you must add your button to the form ..
 Button button = new Button();
                    button.Visible = true;
                    button.Enabled = true;
                    button.Focus();
                    button.Text = "i am a button";
                    button.Location = new Point(50, 50);
this.Controls.Add(button);


这听起来像是我的作业.
但是,如何在C#中将控制台应用程序转换为Windows应用程序 [
This sounds like homework to me.
However, How to Convert a Console App into a Windows App in C#[^] should help you out.

This describes the various assemblies / steps to use a form with a console application.


这篇关于使用代码制作新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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