SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

ComponentZine(ComponentOne)

強化したリボンインターフェースでテキスト操作ができる.NETアプリケーションの作成

Ribbon for Windows Forms 2.0JのC1Ribbonコントロールを使ったアプリケーションの作成 その2

  • このエントリーをはてなブックマークに追加

各アイテムのイベント処理

 リボンに配置したアイテムのイベントハンドラに、それぞれの処理を組み込みます。

 作成するアイテムとイベントハンドラは以下の通りです。それぞれ、デフォルトのイベントハンドラを使用します。また、フォームのLoadイベントハンドラに、コンボボックスのリストに文字サイズを入れる処理を追加します。

Visual Basic
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
C#
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つテキストボックスで表示するようにします。

Visual Basic
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
C#
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」のフォントを選択されたフォントとサイズに変更します。

Visual Basic
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
C#
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」のフォントサイズを変更する処理を行います。

Visual Basic
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
C#
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」プロパティにセットして文字色を変更します。

Visual Basic
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
C#
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コントロールで表示します。表示したら、ステータスバーのラベルでファイル名をフルパスで表示します。

Visual Basic
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
C#
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」を設定します。

Visual Basic
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
C#
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;
}

まとめ

 リボンインターフェイスは、アプリケーションの操作系を視覚的にまとめることができるため、アプリケーションの操作に不慣れな人に対しても、使いやすいユーザーインターフェイスを提供できると思います。リボンには、他にもクイック起動やアプリケーションメニューの設定などが行えるので、使ってみてはいかがでしょうか。

修正履歴

この記事は参考になりましたか?

  • このエントリーをはてなブックマークに追加
ComponentZine(ComponentOne)連載記事一覧

もっと読む

この記事の著者

瀬戸 遥(セト ハルカ)

8ビットコンピュータの時代からBASICを使い、C言語を独習で学びWindows 3.1のフリーソフトを作成、NiftyServeのフォーラムなどで配布。Excel VBAとVisual Basic関連の解説書を中心に現在まで40冊以上の書籍を出版。近著に、「ExcelユーザーのためのAccess再...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

  • このエントリーをはてなブックマークに追加
CodeZine(コードジン)
https://codezine.jp/article/detail/6547 2012/04/26 18:00

おすすめ

アクセスランキング

アクセスランキング

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー

アクセスランキング

アクセスランキング