假设我有一个这样的类:
class MyClass
{
... (some more properties here)
public int Min {get;set;}
public int Max {get;set;}
... (some more properties here)
}
现在,我在设计器中放置了一个文本框,希望它将Min和Max显示为用破折号分隔的文本。例如,如果是Min=3和Max=10,则文本框应显示"3-10“。当文本更改/绑定更新时,它应该像这样解析字符串"3-10“:
用'-‘分割字符串,并用int.Parse(...)解析这两个字符串如果这不起作用(异常发生),我想以某种方式对此作出反应。例如,显示一条错误消息就可以了。
我该怎么做呢?VisualStudio设计器只允许我将文本绑定到对象的一个属性。
发布于 2013-03-31 18:15:19
为了显示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是返回Min + "-“+Max的某个属性;
https://stackoverflow.com/questions/15728306
复制相似问题