主題の要件:
図のようにフォームをデザインします。
フォームには2つのテキストボックスがあります。テキストボックスには最大6文字を入力できます。テキストボックスに入力したコンテンツには、*が表示されます。もう1つのボタンと2つのラジオボタンを追加します。ボタンをクリックすると、対応するテキストボックスの内容がラジオボタンに従ってラベルに表示されます。
参照コード:
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_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "单选按钮实例";
this.textBox1.MaxLength = 6;
this.textBox2.PasswordChar = '*';
this.radioButton1.Text = "取第一个文本框内容";
this.radioButton2.Text = "取第二个文本框内容";
this.button1.Text = "根据单选按钮,将对应文本框中的内容显示在标签";
}
private void button1_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
label1.Text = textBox1.Text;
}
else if (radioButton2.Checked == true)
{
label1.Text = textBox2.Text;
}
}
}
}
自動生成されたDesignerファイル:
namespace WindowsFormsApplication5_6
{
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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(47, 49);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(279, 28);
this.textBox1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(47, 116);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(279, 28);
this.textBox2.TabIndex = 1;
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(364, 50);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(141, 22);
this.radioButton1.TabIndex = 2;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(364, 117);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(141, 22);
this.radioButton2.TabIndex = 3;
this.radioButton2.TabStop = true;
this.radioButton2.Text = "radioButton2";
this.radioButton2.UseVisualStyleBackColor = true;
//
// button1
//
this.button1.Location = new System.Drawing.Point(47, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(501, 41);
this.button1.TabIndex = 4;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(56, 261);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(62, 18);
this.label1.TabIndex = 5;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(613, 320);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.radioButton2);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
}
}