[英]C# WinForms button doesn't work
無論如何,這是我第一次嘗試使用這種語言或應用程序進行任何操作。 我剛剛安裝了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為我完成了所有代碼。 :)
InitializeComponent()
方法中缺少的是這樣的:
this.button1.Click += new System.EventHandler(this.button1_Click);
為什么沒有為您創建這個,這很難說。 通常,您打開表單設計器並雙擊按鈕,然后創建代碼,然后進入帶button1_Click
方法的存根。
嘗試刪除當前的button1_Click
方法代碼,保存,打開設計器,然后雙擊按鈕以查看是否有效。
您可以自己添加類似的代碼,方法是:
button1.Click += button1_Click;
但是要注意,它不在InitializeComponent()
方法中,這將導致它被調用兩次。
加
button1.Click += button1_Click;
某個地方(Form1的構造函數)。
您需要將button1_Click
處理程序添加到Click
事件。
與VB6不同,這不是隱式完成的。
您可以在設計器中執行此操作,方法是選擇按鈕並轉到事件選項卡。
為了回答您的問題,VS WinForms設計器會生成C#源代碼,以創建您設計的布局。
如果您不喜歡該范例,則可能要使用WPF。
當然可以完全手動構建WinForms UI,但這將非常痛苦。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.