各アイテムのイベント処理
リボンに配置したアイテムのイベントハンドラに、それぞれの処理を組み込みます。
作成するアイテムとイベントハンドラは以下の通りです。それぞれ、デフォルトのイベントハンドラを使用します。また、フォームのLoadイベントハンドラに、コンボボックスのリストに文字サイズを入れる処理を追加します。
Private Sub RibbonFontComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles RibbonFontComboBox1.SelectedIndexChanged Private Sub RibbonComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles RibbonComboBox1.SelectedIndexChanged Private Sub RibbonColorPicker1_Click(sender As System.Object, e As System.EventArgs) Handles RibbonColorPicker1.Click Private Sub RibbonColorPicker1_SelectedColorChanged(sender As Object, e As System.EventArgs) Handles RibbonColorPicker1.SelectedColorChanged</pre> Private Sub RibbonButton6_Click(sender As System.Object, e As System.EventArgs) Handles RibbonButton6.Click Private Sub RibbonToggleButton1_Click(sender As System.Object, e As System.EventArgs) Handles RibbonToggleButton1.Click Private Sub RibbonToggleButton2_Click(sender As System.Object, e As System.EventArgs) Handles RibbonToggleButton2.Click Private Sub RibbonToggleButton3_Click(sender As System.Object, e As System.EventArgs) Handles RibbonToggleButton3.Click
private void ribbonFontComboBox1_SelectedIndexChanged(object sender, EventArgs e) private void ribbonComboBox1_SelectedIndexChanged(object sender, EventArgs e) private void ribbonColorPicker1_Click(object sender, EventArgs e) private void ribbonColorPicker1_SelectedColorChanged(object sender, EventArgs e) private void ribbonButton6_Click(object sender, EventArgs e) private void ribbonToggleButton1_Click(object sender, EventArgs e) private void ribbonToggleButton2_Click(object sender, EventArgs e) private void ribbonToggleButton3_Click(object sender, EventArgs e)
フォント関連の設定
フォーム起動時にフォントサイズを設定するコンボボックス「RibbonComboBox1」のリスト作成を行います。これは、単純に偶数の数字を2から100までリストにするだけですが、Addメソッドの引数がStringなので、数値を文字列に変換して設定します。
設定したら、「12」がテキストボックスに入るように「SelectedIndex」プロパティを設定します。フォントコンボボックスも同様に、リストのフォント名を1つテキストボックスで表示するようにします。
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(DataSet11)
' 今回追加したコード
Dim i As Integer
For i = 2 To 100 Step 2
RibbonComboBox1.Items.Add(i)
Next
RibbonComboBox1.SelectedIndex = 5
RibbonFontComboBox1.SelectedIndex = 1
End Sub
private void Form1_Load(object sender, EventArgs e)
{
oleDbDataAdapter1.Fill(dataSet11);
// 今回追加したコード
int i = 0;
for(i=2;i<=100;i+=2)
{
String s = Convert.ToString(i);
ribbonComboBox1.Items.Add(s);
}
ribbonComboBox1.SelectedIndex = 5;
ribbonFontComboBox1.SelectedIndex = 1;
}
フォントコンボボックスでフォントが選択された場合の処理を作成します。フォントがリストから選択されると、C1RibbonFontComboBoxコントロールには「SelectedIndexChanged」イベントが発生します。
ここで、RichTextBoxコントロール「RichTextBox1」のフォントを選択されたフォントとサイズに変更します。
Private Sub RibbonFontComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles RibbonFontComboBox1.SelectedIndexChanged Dim fntsize As Single = Convert.ToSingle(RibbonComboBox1.Text Dim fntname As String = RibbonFontComboBox1.Text RichTextBox1.SelectionFont = New Font(fntname, fntsize) End Sub
private void ribbonFontComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Single fntsize = Convert.ToSingle(ribbonComboBox1.Text);
String fntname = ribbonFontComboBox1.Text;
richTextBox1.SelectionFont = new Font(fntname, fntsize);
}
フォントサイズ用のコンボボックスでリストからサイズが選択された場合は、C1RibbonComboBoxコントロールには「SelectedIndexChanged」イベントが発生するため、ここでRichTextBoxコントロール「RichTextBox1」のフォントサイズを変更する処理を行います。
Private Sub RibbonComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles RibbonComboBox1.SelectedIndexChanged
Dim fontname As FontFamily = RichTextBox1.SelectionFont.FontFamily
Dim fntsize As Single = Convert.ToSingle(RibbonComboBox1.Text)
RichTextBox1.SelectionFont = New Font(fontname, fntsize)
End Sub
private void ribbonComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FontFamily fontname = richTextBox1.SelectionFont.FontFamily;
Single fntsize = Convert.ToSingle(ribbonComboBox1.Text);
richTextBox1.SelectionFont = new Font(fontname, fntsize);
}
カラーピッカーの設定
カラーピッカー「RibbonColorPicker1」では、表示されるパレットで色を変更したときはSelectedColorChangedイベントが、カラーピッカーに最初から表示されている色のボタンをクリックするとClickイベントが、それぞれ発生するようになっています。
そこで、この2つのイベントハンドラにRichTextBoxコントロール「RichTextBox1」で選択された文字の色を変更する処理を作成します。
カラーピッカー「RibbonColorPicker1」で選択された色は「Color」プロパティに格納されるので、これをRichTextBoxコントロール「RichTextBox1」の「SelectionColor」プロパティにセットして文字色を変更します。
Private Sub RibbonColorPicker1_Click(sender As System.Object, e As System.EventArgs) Handles RibbonColorPicker1.Click
RichTextBox1.SelectionColor = RibbonColorPicker1.Color
End Sub
Private Sub RibbonColorPicker1_SelectedColorChanged(sender As Object, e As System.EventArgs) Handles RibbonColorPicker1.SelectedColorChanged
RichTextBox1.SelectionColor = RibbonColorPicker1.Color
End Sub
private void ribbonColorPicker1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = ribbonColorPicker1.Color;
}
private void ribbonColorPicker1_SelectedColorChanged(object sender, EventArgs e)
{
richTextBox1.SelectionColor = ribbonColorPicker1.Color;
}
「開く」ボタン「RibbonButton6」がクリックされると、Clickイベントが発生するため、このイベントハンドラでファイルを開く処理を行い、もう1つのRichTextBoxコントロール「RichTextBox2」でファイルの中身を表示します。
ファイルを開くダイアログ「OpenFileDialog1」コントロールでは、テキストファイルとリッチテキストファイルの2種類のファイルを開けるようにFilterプロパティを設定します。そして、選択されたファイル名の末尾にあるファイルの拡張子の最後の文字を取得して、その文字が「t」であればプレーンテキストモードで、「f」であればリッチテキストモードでファイルを開き、RichTextBoxコントロールで表示します。表示したら、ステータスバーのラベルでファイル名をフルパスで表示します。
Private Sub RibbonButton6_Click(sender As System.Object, e As System.EventArgs) Handles RibbonButton6.Click
Dim fname As String = ""
OpenFileDialog1.Filter = "テキストファイル(*.txt) | *.txt|リッチテキスト(*.rtf)|*.rtf"
OpenFileDialog1.FileName = ""
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
fname = OpenFileDialog1.FileName
End If
If fname.EndsWith("t") Then
RichTextBox2.LoadFile(fname, RichTextBoxStreamType.PlainText)
ElseIf fname.EndsWith("f") Then
RichTextBox2.LoadFile(fname, RichTextBoxStreamType.RichText)
End If
RibbonLabel1.Text = fname
End Sub
private void ribbonButton6_Click(object sender, EventArgs e)
{
String fname = "";
openFileDialog1.Filter = "テキストファイル(*.txt) | *.txt|リッチテキスト(*.rtf)|*.rtf";
openFileDialog1.FileName = "";
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
fname = openFileDialog1.FileName;
}
if(fname.EndsWith("t") )
{
richTextBox2.LoadFile(fname, RichTextBoxStreamType.PlainText);
}else if(fname.EndsWith("f"))
{
richTextBox2.LoadFile(fname, RichTextBoxStreamType.RichText);
}
ribbonLabel1.Text = fname;
}
トグルボタンの設定
最後にトグルボタンの処理を作成します。トグルボタン「RibbonToggleButton」がクリックされるとClickイベントが発生するので、ここでRichTextBoxコントロール「RichTextBox1」の文字位置を変更する処理を実行します。
文字位置の変更は、RichTextBoxクラスのSelectionAlignmentプロパティを使用し、それぞれのトグルボタンのイベントハンドラで、「HorizontalAlignment」列挙体のメンバ「Center」「Left」「Right」を設定します。
Private Sub RibbonToggleButton1_Click(sender As System.Object, e As System.EventArgs) Handles RibbonToggleButton1.Click
RichTextBox1.SelectionAlignment = HorizontalAlignment.Left
End Sub
Private Sub RibbonToggleButton2_Click(sender As System.Object, e As System.EventArgs) Handles RibbonToggleButton2.Click
RichTextBox1.SelectionAlignment = HorizontalAlignment.Center
End Sub
Private Sub RibbonToggleButton3_Click(sender As System.Object, e As System.EventArgs) Handles RibbonToggleButton3.Click
RichTextBox1.SelectionAlignment = HorizontalAlignment.Right
End Sub
private void ribbonToggleButton1_Click(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
}
private void ribbonToggleButton2_Click(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
}
private void ribbonToggleButton3_Click(object sender, EventArgs e)
{
richTextBox1.SelectionAlignment = HorizontalAlignment.Right;
}
まとめ
リボンインターフェイスは、アプリケーションの操作系を視覚的にまとめることができるため、アプリケーションの操作に不慣れな人に対しても、使いやすいユーザーインターフェイスを提供できると思います。リボンには、他にもクイック起動やアプリケーションメニューの設定などが行えるので、使ってみてはいかがでしょうか。

