如果声明/检查值?

时间:2011-05-26 09:08:21

标签: c# if-statement email-integration

我用手指搜索了我的手指,我知道这里的代码不是很容易接受,但我真的很感激一些帮助:

此复选框必须检查选项按钮的值(布尔值)。

根据该值显示不同的消息,如果True在邮件中包含额外的行,但这是另一个问题。

非常感谢任何帮助。我真的需要掌握这个C#。

public void field42_Changed(object sender, XmlEventArgs e)
{
    // checking Gsm checkbox(IOgsm@parzs.be)
    if (e.NewValue.Equals("true"))
    {
        XPathNavigator xnMyForm = this.CreateNavigator();
        XmlNamespaceManager ns = this.NamespaceManager;
        xnMyForm.SelectSingleNode("/my:myFields/my:txtGSM", ns).SetValue("P.Bab@gmail.Com");
        //string MobielInternet = xnMyForm.SelectSingleNode("/my:myFields/my:GsmMobileInternet", ns).Value;
        if MobielInternet e.NewValue.Equals("true")
            MessageBox.Show("An e-mail for GSM with Mobile internet will be sent");
        if (e.NewValue.Equals("false"))
            MessageBox.Show("An email for the GSM will be sent");
        if (e.NewValue.Equals(""))
            MessageBox.Show (" The Mobile Internet option has to been filled out ");
    }
    else if (e.NewValue.Equals("false"))
    {
        XPathNavigator xnMyForm = this.CreateNavigator();
        XmlNamespaceManager ns = this.NamespaceManager;
        xnMyForm.SelectSingleNode("/my:myFields/my:txtGSM", ns).SetValue("");
    }
}

1 个答案:

答案 0 :(得分:0)

这是我对更清洁代码的建议。我希望我不会错过这里的观点,但你实际上并没有比较mobielinternet值,而是再次比较EventArgs值。

试试这个。

XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
var emailAddress = "";
var message = "";

if (e.NewValue.Equals("true"))
{
    emailAddress = "P.Bab@gmail.Com";
    string MobielInternet = xnMyForm.SelectSingleNode("/my:myFields/my:GsmMobileInternet", ns).Value;

    if (string.IsNullorEmpty(MobielInternet))
        message = "The Mobile Internet option has to been filled out";

    if (bool.Parse(MobielInternet))
        message = "An e-mail for GSM with Mobile internet will be sent";
    else
        message = "An email for the GSM will be sent";

    MessageBox.Show(message);
}
else if (e.NewValue.Equals("false"))
{
    XPathNavigator xnMyForm = this.CreateNavigator();
    XmlNamespaceManager ns = this.NamespaceManager;
    xnMyForm.SelectSingleNode("/my:myFields/my:txtGSM", ns).SetValue("");
}


xnMyForm.SelectSingleNode("/my:myFields/my:txtGSM", ns).SetValue(emailAddress);