Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C# - TextBoxのサンプル コードべた書き(Visual Studio不使用)

Last updated at Posted at 2020-10-26

目次: C# - Windows Formsでよく使うコントロールたち (Visual Studioなし環境向け) - Qiita

単一行テキストボックス

単一行テキストボックスの画面キャプチャ

image.png

単一行テキストボックスのテンプレ(サンプルコード)


using System;
using System.Drawing;
using System.Windows.Forms;

class TextBoxSample : Form
{
    TextBox txt;

    TextBoxSample()
    {
        ClientSize = new Size(300, 100);
        
        Controls.Add(txt = new TextBox(){
            Location = new Point(20, 30),
            Width = 250,
        });

        txt.TextChanged += Txt_TextChanged;
    }

    void Txt_TextChanged(object sender, EventArgs e)
    {
        Text = txt.Text;
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new TextBoxSample());
    }
}

複数行テキストボックス

複数行テキストボックスの画面キャプチャ

image.png

複数行テキストボックスのテンプレ(サンプルコード)


using System;
using System.Drawing;
using System.Windows.Forms;

class TextBoxSample : Form
{
    TextBox txt;

    TextBoxSample()
    {
        ClientSize = new Size(300, 250);
        
        Controls.Add(txt = new TextBox(){
            Location = new Point(0, 0),
            Size = new Size(300, 250),
            Multiline = true,
            WordWrap = false, // 折り返し表示をしない
            ScrollBars = ScrollBars.Both,
        });
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new TextBoxSample());
    }
}

文字数制限

デフォルトだと32767文字までしか入力できないっぽい。
MaxLength = 0;とすると、この制限を解除できる。(いちど0以外を設定する必要あるかも(?))

TextBox.MaxLength Property (System.Windows.Controls) | Microsoft Docs

defaultが0と書かれているが、謎。。

参考サイト

全般

個別ノウハウ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?