ラベルの設定をする
これまでにも使ったラベル(Label)についてみていくことにします。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace lesson7
{
class sample3 : Form
{
private Label[] lb = new Label[3];
private TableLayoutPanel tlp;
static void Main(string[] args)
{
Application.Run(new sample3());
}
public sample3()
{
this.Text = "lesson7";
this.Width = 300; this.Height = 200;
tlp = new TableLayoutPanel(); //テーブルレイアウトパネルを作成します
tlp.Dock = DockStyle.Fill;
tlp.ColumnCount = 1; //列を1とします
tlp.RowCount = 3; //行を3とします
for (int i = 0; i < lb.Length; i++) //ラベルを3つ作成します
{
lb[i] = new Label(); //ラベルを追加します
lb[i].Text = i + "枚目です";
lb[i].ForeColor = Color.Black; //前景色を設定します
switch ( i )
{
case 0:
lb[i].BackColor = Color.White ; //背景色を設定します
lb[i].TextAlign = ContentAlignment.TopLeft; //位置揃えを設定します
lb[i].BorderStyle = BorderStyle.None; //境界線を設定します。
break;
case 1:
lb[i].BackColor = Color.Gray;
lb[i].TextAlign = ContentAlignment.MiddleCenter;
lb[i].BorderStyle = BorderStyle.FixedSingle;
break;
case 2:
lb[i].BackColor = Color.White;
lb[i].TextAlign = ContentAlignment.BottomRight;
lb[i].BorderStyle = BorderStyle.Fixed3D;
break;
}
lb[i].Parent = tlp;
}
tlp.Parent = this;
}
}
}
構造体の仕組みを知る
なお、ここで色を表すために使っているColorは構造体(structure)と呼ばれ、structキーワードを使って定義されているものです。
構文:構造体の定義
struct 構造体名
{
フィールドの宣言;
メソッド(引数のリスト)の定義
プロパティの定義
}
構文:構造体の利用
構造体名.メンバ
種類 | 説明 |
---|---|
White | 白 |
Black | 黒 |
Gray | グレー |
Red | 赤 |
Green | 緑 |
Blue | 青 |
Cyan | シアン |
Yellow | 黄色 |
Magenta | マゼンタ |
コントロールにフォントを設定する
さて、ラベルはテキスト表示するものですから、表示されてる文字のフォントを変えることができれば便利です。そこで今度はラベルのフォントを変更してみることにしましょう。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace lesson7
{
class sample4 : Form
{
private Label[] lb = new Label[3];
private TableLayoutPanel tlp;
static void Main(string[] args)
{
Application.Run(new sample4());
}
public sample4()
{
this.Text = "lesson7";
this.Width = 300; this.Height = 200;
tlp = new TableLayoutPanel(); //テーブルレイアウトパネルを作成します
tlp.Dock = DockStyle.Fill;
tlp.ColumnCount = 1; //列を1とします
tlp.RowCount = 3; //行を3とします
for (int i = 0; i < lb.Length; i++) //ラベルを3つ作成します
{
lb[i] = new Label(); //ラベルを追加します
lb[i].Text = i + "枚目です";
lb[i].ForeColor = Color.Black; //前景色を設定します
lb[i].Width = 200;lb[i].Height = 40;
switch (i)
{
case 0:
lb[i].BackColor = Color.White; //背景色を設定します
lb[i].TextAlign = ContentAlignment.TopLeft; //位置揃えを設定します
lb[i].BorderStyle = BorderStyle.None; //境界線を設定します。
lb[i].Font = new Font("SansSerif", 12, FontStyle.Bold); //フォントを設定します
break;
case 1:
lb[i].BackColor = Color.Gray;
lb[i].TextAlign = ContentAlignment.MiddleCenter;
lb[i].BorderStyle = BorderStyle.FixedSingle;
lb[i].Font = new Font("Helvetica", 16, FontStyle.Bold);
break;
case 2:
lb[i].BackColor = Color.White;
lb[i].TextAlign = ContentAlignment.BottomRight;
lb[i].BorderStyle = BorderStyle.Fixed3D;
lb[i].Font = new Font("Century", 20, FontStyle.Bold);
break;
}
lb[i].Parent = tlp;
}
tlp.Parent = this;
}
}
}
lb[i].Font = new Font(“SansSerif”, 12, FontStyle.Bold); //フォントを設定します
フォント名 サイズ スタイル
種類 | 説明 |
---|---|
Regular | レギュラー |
Bold | 太字 |
Italic | イタリック |
Underline | 下線 |
Strikeout | 取り消し線 |
コメント