ロゴ

フォームの変更・編集

今回はとうとうアプリケーションの本質であるフォームの編集です。

メモを追加できるアプリを作ります。Form1.csを開き、以下のコードに直してください。

Form1.cs


namespace Sample1;
public partial class Form1 : Form
{
        TextBox txt1;
        Button button1 = new();
        Label[]? label = new Label[100];
        private int i = 0;
        private int topnum = 80;
        public Form1()
        {
            InitializeComponent();
            Text = "メモ";
            txt1 = new()
            {
                PlaceholderText = "ここにメモを入力してください。",
                Parent = this
            }
            button1 = new()
            {
                Top = 50;
                Text = "追加";
                Width = ClientSize.Width;
                Parent = this;
            }
                button1.Click += Note_Added;
        }
        void Note_Added(object sender, EventArgs e)
        {
            try
            {
                DateTime date = DateTime.Now;
                i++;
                label[i] = new()
                {
                    AutoSize = true,
                    Text = $"{txt1.Text}-date:{date}",
                    Parent = this,
                    Top = topnum
                };
                topnum += 20;
            }
            catch
            {
                MessageBox.Show("Error", "予期しないエラーが発生しました。", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
}

ビルドしてみましょう。F5キーを押すかSample1のボタンをクリックしてビルドしてみましょう。

簡単なメモアプリができました。

次回はインストーラーを作成します。

次の記事へ