If statement/checkvalue?

本文关键字:checkvalue statement If | 更新日期: 2023-09-27 17:57:39

我已经在谷歌上搜索了这个代码,我知道这里的代码不太可接受,但我真的很感激你的帮助:

这个复选框必须选中一个选项按钮的值(布尔值)。

根据该值显示不同的消息,如果为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("");
    }
}

If statement/checkvalue?

这将是我对更干净代码的建议。我希望我没有错过这里的重点,但你实际上并没有比较mobielnternet的值,你又在比较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);