Windows application programming(1)_2

Subject requirements:

Design a form as shown on the left. There are 2 buttons on the form, one for displaying text and one for displaying pictures.

(1) Click the button above or press Alt+B, the message box shown on the right will pop up.

(2) Click the button below to also pop up the message box shown on the right (refer to section 6.5.1 of the textbook for the message dialog box).

 

Reference Code:

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.button1.Text = "第一个按钮(&B)";
            this.button2.BackgroundImage = Image.FromFile("picture1.jpg");
            this.button2.BackgroundImageLayout = ImageLayout.Stretch;
            this.button2.Text = "";

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.B && e.Alt)
            {
                MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
        }
    }
}

Automatically generated Designer file:

namespace WindowsFormsApplication5_2
{
    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.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(73, 39);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(360, 87);
            this.button1.TabIndex = 0;
            this.button1.Text = "第一个按钮(&B)";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(73, 152);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(360, 91);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(522, 314);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.KeyPreview = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}

 

Guess you like

Origin blog.csdn.net/wangws_sb/article/details/105626738