DataBinding:Format and Parse

 

単純に Binding することができないシチュエーションがあります。例えば、片方が値で、片方がテキスト、である場合などで表示の方法を変換したい場合などがあります。ここでは int と string の変換をデータバインディングに絡める方法を記載します。

 

[環境]

コンパイラ : Visual Studio 2013  
言語 : C#, Form  

 

スライダーの値をテキストボックスへ表示するデータバインディングを行うプログラムの作成を行います。
まずは Form を使ったプロジェクトを新規作成し、以下のような画面を作成。

部品 説明
Form タイトルを”Bindingテスト”とする。
TextBox TrackBarの値をテキスト表示。
TrackBar 0~100 を設定可能。初期値 0。
Button "Close"を表示。ボタンクリックでプログラム終了。

 

まずは部品を貼り付けます。

部品を張り付けた状態

 

各部品のプロパティを設定します。

プロパティ設定後

 

TextBox と TrackBar をデータバインディングします。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test_Binding_Format_Parse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Binding b = new Binding("Text", trackBar1, "Value" );
            textBox1.DataBindings.Add(b);
        }
    }
}

 

実行すると次のような画面になります。

Binding 設定後

 

次に、スライダーの値範囲を 0~100 のままに、テキストボックスの表示を 0.00~1.00 にすることを行います。
つまりスライダーの値を 1/100 してテキストボックスに表示する、または テキストボックスの値を ×100 してスライダーへ設定する、の2つの処理が必要です。
これを行うために、以下のように Format、Parse のイベントを加えます。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace test_Binding_Format_Parse
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Binding b = new Binding("Text", trackBar1, "Value" );

            b.Format += new ConvertEventHandler( IntToString );
            b.Parse  += new ConvertEventHandler( StringToInt );

            textBox1.DataBindings.Add(b);
        }

        private void IntToString(object sender, ConvertEventArgs cevent)
        {
            // The method converts only to string type. Test this using the DesiredType.
            if (cevent.DesiredType != typeof(string))
            {
                return;
            }

            decimal data = ((int)cevent.Value);
            data /= 100;
            cevent.Value = data.ToString("F2");
        }

        private void StringToInt(object sender, ConvertEventArgs cevent)
        {
            // The method converts only to string type. Test this using the DesiredType.
            if (cevent.DesiredType != typeof(int))
            {
                return;
            }

            cevent.Value = (int)(decimal.Parse((string)cevent.Value)*100);
        }
    }
}

 

Format, Parse 設定後

意図通りうまく動きました。

 

 

ライセンス

本ページの情報は、特記無い限り下記 MIT ライセンスで提供されます。

The MIT License (MIT)

  Copyright 2014 Kinoshita Hidetoshi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 


参考

 


 

変更履歴

2023-08-31 - ページデザイン更新
2014-03-15 - 新規作成

 

Programming Items トップページ

プライバシーポリシー