準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
- フォーム (Form1) にボタン (button2) を配置します。
サンプルコード (C#)
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
button1.AutoSize = true;
button1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
button1.Text += "ABCDE";
}
private void button2_Click(object sender, EventArgs e)
{
button1.Text = button1.Text.Substring(0, button1.Text.Length - 1);
}
}
}
解説
button1 をクリックするとボタンのテキストが長くなり、ボタンの横サイズも広がります。button2 をクリックすると、ボタンのテキストが短くなり、ボタンのサイズも狭くなります。両者とも縦サイズも自動調整されます。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.05.04 Visual Studio 2015 Professional (C# 6.0)
