2013-03-31 126 views
0

假设我有一类这样的:绑定两个属性到一个文本框的文本

class MyClass 
{ 
    ... (some more properties here) 

    public int Min {get;set;} 
    public int Max {get;set;} 

    ... (some more properties here) 
} 

现在我把在设计一个文本框,我希望它显示最小值和最大值与破折号分隔文本。 例如,如果最小值= 3,最大值= 10,则文本框应显示“3-10”。 当文本更改/绑定更新应该解析字符串“3-10”是这样的:

分割字符串用“ - ”和与int.Parse解析两个字符串(...) 如果这不起作用(发生异常),我想对此作出反应。例如,显示错误消息将工作。

我该怎么做? VisualStudio设计器只允许我将文本绑定到对象的一个​​属性。

回答

0

为了显示3-10,你可以写

TextBoxName.Text=Min + "-" + Max; 

而且,你可能会引发异常,并显示在MessageBox为:

try{ 
    int.Parse(Min); 
    int.Parse(Max); 
} 
catch(Exception ae){ 
    MessageBox.Show("Some error message"); 
} 

编辑: 绑定,

textBoxName.DataBindings.Add("Text",this,"StringVariable"); 
        //Text property,this form, name of the variable. 

其中StringVariable是一些专业版perty返回Min +“ - ”+ Max;

+0

是的但我不想将值直接分配给Text属性。我需要用数据绑定来解决它。 – riki

+0

编辑答案;这应该工作。 –

相关问题