C# WinForms 按钮不起作用

发布于 2024-12-02 09:37:49 字数 3422 浏览 0 评论 0原文

无论如何,这是我第一次尝试使用这种语言或应用程序进行任何操作。我刚刚安装了 c#,但无法让此代码进行简单的值更改。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 22;
            MessageBox.Show("completed!");

        }
    }
}

    namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // progressBar1
            // 
            this.progressBar1.Location = new System.Drawing.Point(42, 309);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(428, 54);
            this.progressBar1.TabIndex = 0;
            this.progressBar1.Value = 55;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(42, 48);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(428, 45);
            this.button1.TabIndex = 1;
            this.button1.Text = "start";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.ClientSize = new System.Drawing.Size(525, 409);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.progressBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.Button button1;
    }
}

我所做的只是添加

       progressBar1.Value = 22;
       MessageBox.Show("completed!");

,但是当我单击“button1”时什么也没有发生。

顺便说一句,所有这些代码都是由软件创建的,人们就是这样开发的吗?我以为这都是没有“帮助”的代码,这看起来就像 10 年前我第一次开始使用 HTML 时,Frontpage 为我完成了所有代码。 :)

this is the very first time I try to anything in this language or applications anyway. I just installed c# and I can't get this code to do a simple value change.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            progressBar1.Value = 22;
            MessageBox.Show("completed!");

        }
    }
}

    namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // progressBar1
            // 
            this.progressBar1.Location = new System.Drawing.Point(42, 309);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(428, 54);
            this.progressBar1.TabIndex = 0;
            this.progressBar1.Value = 55;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(42, 48);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(428, 45);
            this.button1.TabIndex = 1;
            this.button1.Text = "start";
            this.button1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.ClientSize = new System.Drawing.Size(525, 409);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.progressBar1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.Button button1;
    }
}

All I did is add

       progressBar1.Value = 22;
       MessageBox.Show("completed!");

but when I click on button1 nothing happens.

By the way all this code was created by the software, is this how people develop? I thought it was all code with no "help", this looks like me 10y ago when I first started with HTML and frontpage did all the code for me.
:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨的味道风的声音 2024-12-09 09:37:49

InitializeComponent() 方法中缺少的是这样的内容:

this.button1.Click += new System.EventHandler(this.button1_Click);

很难说为什么没有为您创建它。通常,您打开表单设计器并双击按钮,就会创建该代码,然后您将进入 button1_Click 方法存根。

尝试删除当前的 button1_Click 方法代码,保存,打开设计器并双击按钮以查看是否有效。

您可以通过以下方式自己添加类似的代码:

button1.Click += button1_Click;

但请注意,它不在 InitializeComponent() 方法中,这会导致它被调用两次。

What is missing, in the InitializeComponent() method, is something like this:

this.button1.Click += new System.EventHandler(this.button1_Click);

Why this didn't get created for you is hard to tell. Normally you open the form desginer and double click on the button and that code is created and you are taken to a button1_Click method stub.

Try deleting your current button1_Click method code, save, open the designer and double click on the button to see if that works.

You can add similar code yourself by doing:

button1.Click += button1_Click;

But be careful that it is not also in the InitializeComponent() method, which would cause it to be called twice.

清君侧 2024-12-09 09:37:49

添加

button1.Click += button1_Click;

某处(Form1 的构造函数)。

Add

button1.Click += button1_Click;

somewhere (constructor of Form1).

枯叶蝶 2024-12-09 09:37:49

您需要将 button1_Click 处理程序添加到 Click 事件中。
与 VB6 不同,这不是隐式完成的。

您可以在设计器中通过选择按钮并转到事件选项卡来执行此操作。


为了回答您的问题,VS WinForms 设计器会生成 C# 源代码来创建您设计的布局。
如果您不喜欢这种范例,您可能需要使用 WPF。

完全手工构建 WinForms UI 当然是可能的,但这将是极其痛苦的。

You need to add the button1_Click handler to the Click event.
Unlike VB6, that's not done implicitly.

You can do that in the designer by selecting the button and going to the events tab.


To answer your question, the VS WinForms designer generates C# source code that creates the layout you designed.
If you don't like that paradigm, you may want to use WPF.

It is certainly possible to build WinForms UIs entirely by hand, but it will be extremely painful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文