WindowsFormアプリケーションを作っていたらたまにコンボボックスを使うことがありますが
そのままだと多分長さ固定なんですね
検索したらこの掲示板に助かることが書いてあったので
コンボボックスの長さを変えるのに使ってみた
コンボボックスの横幅を変えてみる
WindowsFormアプリケーションを作成して
ComboBoxをツールボックスから持ってきて貼り付ておきます
Form1.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> comboItems = new List<string>
{
"いちご",
"パイナップル",
"もも",
"りんご",
"デコポン"
};
comboBox1.Items.AddRange(comboItems.ToArray());
string longStr = "";
foreach (string str in comboItems)
{
if (longStr.Length < str.Length)
longStr = str;
}
Font font = comboBox1.Font;
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
SizeF size = graphics.MeasureString(longStr, font);
comboBox1.Size = new Size((int)Math.Round(size.Width), comboBox1.Size.Height);
}
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> comboItems = new List<string>
{
"いちご",
"パイナップル",
"もも",
"りんご",
"デコポン"
};
comboBox1.Items.AddRange(comboItems.ToArray());
string longStr = "";
foreach (string str in comboItems)
{
if (longStr.Length < str.Length)
longStr = str;
}
Font font = comboBox1.Font;
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
SizeF size = graphics.MeasureString(longStr, font);
comboBox1.Size = new Size((int)Math.Round(size.Width), comboBox1.Size.Height);
}
}
}
コンボボックスに設定した文字列から一番文字数の多いものの横幅を
コンボボックスの横幅に設定してみます
実行してみたところ、思ったより縮んでしまいました
アイテムを開くと「パイナップル」の横幅に合わせてくれてるのがわかります
なんかでもギチギチなので少し余裕を持たせたい
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> comboItems = new List<string>
{
"いちご",
"パイナップル",
"もも",
"りんご",
"デコポン"
};
comboBox1.Items.AddRange(comboItems.ToArray());
string longStr = "";
foreach (string str in comboItems)
{
if (longStr.Length < str.Length)
longStr = str;
}
Font font = comboBox1.Font;
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
SizeF size = graphics.MeasureString(longStr, font);
comboBox1.Size = new Size((int)Math.Round(size.Width) + 5, comboBox1.Size.Height);
}
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> comboItems = new List<string>
{
"いちご",
"パイナップル",
"もも",
"りんご",
"デコポン"
};
comboBox1.Items.AddRange(comboItems.ToArray());
string longStr = "";
foreach (string str in comboItems)
{
if (longStr.Length < str.Length)
longStr = str;
}
Font font = comboBox1.Font;
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
SizeF size = graphics.MeasureString(longStr, font);
comboBox1.Size = new Size((int)Math.Round(size.Width) + 5, comboBox1.Size.Height);
}
}
}
とりあえず横幅を+5してみました
余裕が出て見やすくはなりましたが、
実際コンボボックスの横幅が勝手に変わったら
他の部品を周りに置いていたときとかに困るのではって思ってきました
コンボボックスのアイテム部分の横幅を変えてみる
コンボボックス自体の横幅を変えるのは止めて
コンボボックスをクリックしたときに下に垂れ下がる、アイテム一覧の部分だけ
横幅増えてもらう方式でやってみます
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> comboItems = new List<string>
{
"いちご",
"パイナップル",
"もも",
"りんご",
"デコポン"
};
comboBox1.Items.AddRange(comboItems.ToArray());
string longStr = "";
foreach (string str in comboItems)
{
if (longStr.Length < str.Length)
longStr = str;
}
Font font = comboBox1.Font;
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
SizeF size = graphics.MeasureString(longStr, font);
if (comboBox1.DropDownWidth < (int)Math.Round(size.Width) + 5)
comboBox1.DropDownWidth = (int)Math.Round(size.Width) + 5;
}
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<string> comboItems = new List<string>
{
"いちご",
"パイナップル",
"もも",
"りんご",
"デコポン"
};
comboBox1.Items.AddRange(comboItems.ToArray());
string longStr = "";
foreach (string str in comboItems)
{
if (longStr.Length < str.Length)
longStr = str;
}
Font font = comboBox1.Font;
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
SizeF size = graphics.MeasureString(longStr, font);
if (comboBox1.DropDownWidth < (int)Math.Round(size.Width) + 5)
comboBox1.DropDownWidth = (int)Math.Round(size.Width) + 5;
}
}
}
さっきと一緒で文字列の横幅を取得したら
次はコンボボックスのDropDownWidthに横幅を設定してみます
アイテムの横幅が文字列の横幅を超えたときに
横幅を設定しなおします
実行してみました
画面が起動しても、コンボボックスは小さいままです
クリックすると選択アイテムが表示されました
コンボボックスの横幅に合わせず、文字の横幅に合うようになっています