第一个类是这样的 public partial class frmFeedback : Form
{
private Color systolicColor;
public Color setSystolicColor
{
set
{
systolicColor = value;
}
}
//向RichTextBox添加带颜色的字体
private void addText(string txt, Color color)
{
rtboxResult.AppendText("\n");
//color the text
rtboxResult.SelectionColor = color;
rtboxResult.AppendText(txt);
rtboxResult.SelectionAlignment = HorizontalAlignment.Left;
} private void frmFeedback_Shown(object sender, EventArgs e)
{
addText("systolic", systolicColor);
}
第二个类向第一个类传递颜色,红色 class Evaluation
{
frmFeedback feedback = new frmFeedback();
public string systolic(string reading)
{
switch (reading)
{
case "210+":
feedback.setSystolicColor = Color.Red;
return "High Blood Pressure: Very severe";
default:
return "Unknown";
}
}
按常理应该是第一个类的systolicColor可以被设置为红色啊!!!但是为什么什么也没有接收到??我再第一个类的addText("systolic", systolicColor);处设置了断点,发现systolicColor收到的是systolicColor = "{Name=0, ARGB=(0, 0, 0, 0)}"!!这就非常诡异了我哪里做的不对?为什么文字可以传,颜色不行??属性还不能接受颜色难不成???
{
private Color systolicColor;
public Color setSystolicColor
{
set
{
systolicColor = value;
}
}
//向RichTextBox添加带颜色的字体
private void addText(string txt, Color color)
{
rtboxResult.AppendText("\n");
//color the text
rtboxResult.SelectionColor = color;
rtboxResult.AppendText(txt);
rtboxResult.SelectionAlignment = HorizontalAlignment.Left;
} private void frmFeedback_Shown(object sender, EventArgs e)
{
addText("systolic", systolicColor);
}
第二个类向第一个类传递颜色,红色 class Evaluation
{
frmFeedback feedback = new frmFeedback();
public string systolic(string reading)
{
switch (reading)
{
case "210+":
feedback.setSystolicColor = Color.Red;
return "High Blood Pressure: Very severe";
default:
return "Unknown";
}
}
按常理应该是第一个类的systolicColor可以被设置为红色啊!!!但是为什么什么也没有接收到??我再第一个类的addText("systolic", systolicColor);处设置了断点,发现systolicColor收到的是systolicColor = "{Name=0, ARGB=(0, 0, 0, 0)}"!!这就非常诡异了我哪里做的不对?为什么文字可以传,颜色不行??属性还不能接受颜色难不成???
在父窗体中创建一个自身的公共静态变量
class MainForm
{
public static MainForm mainForm=null;
MainForm()
{
mainForm=this;
}
}
class childForm
{pbulci void SelectText()
{
MainForm.mainForm.richTextBox.SelectedStart=100;
}
}
再者,给你的属性写个 get 方法,对比下试一试。
也许你的类在其它地方 没有使用,已经被 优化了。
可以在后边 访问下这个类的成员。 也许结果 就不同了。
打个比方:
return 前加上 Messagebox.show(class.Color.tostring());
但是用在switch里还是不行!!
把
private Color systolicColor;
public Color setSystolicColor
{
set
{
systolicColor = value;
}
}改成 private static Color systolicColor;
public static Color setSystolicColor
{
set
{
systolicColor = value;
}
}不知道为什么换成静态的就行??