强制Infopath每行单选框单选(原创)

using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;

namespace 模板2
{
    public partial class FormCode
    {
        // 启用浏览器功能的表单不支持成员变量。
        // 请使用代码从 FormState 词典
        // 写入和读取这些值,如下所示:
        //
       

        // 注意: 以下是 Microsoft Office InfoPath 所需的过程。
        // 可以使用 Microsoft Office InfoPath 对其进行修改。
        public void InternalStartup()
        {
            EventManager.XmlEvents["/my:myFields/my:group1/my:group2/my:field1"].Changed += new XmlChangedEventHandler(field1_Changed);
            EventManager.XmlEvents["/my:myFields/my:group1/my:group2/my:field1"].Validating += new XmlValidatingEventHandler(field1_Validating);
            EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
        }

        //private bool _isLooped = false;

        public void SetNull(XPathNodeIterator iter)
        {
            while (iter.MoveNext())
            {
                {
                    iter.Current.SelectSingleNode("my:field1", NamespaceManager).SetValue("");
                }
            }
            FormState["isLooped"] = true;
        }

        public void field1_Changed(object sender, XmlEventArgs e)
        {
           
            XPathNavigator nav = (XPathNavigator)sender;
            if (nav.LocalName != "field1")
                return;
            if (nav.Value == "")
                return;
            try
            {
                XPathNavigator xpn = this.CreateNavigator().SelectSingleNode("/my:myFields/my:group1", NamespaceManager);
                XPathNodeIterator iter = xpn.SelectChildren(XPathNodeType.Element);


                if ((bool)FormState["isLooped"] == false)
                {
                    SetNull(iter);
                    nav.SetValue("Y");
                }
                FormState["isLooped"] = false;
            }
            catch
            { }
            finally
            {

            }
        }

        public void field1_Validating(object sender, XmlValidatingEventArgs e)
        {
            // 在此处编写代码。
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
            // 在此处编写代码。
            FormState["isLooped"] = false;
        }
    }
}

你可能感兴趣的:(Path)