FrmKeypadCountSet.cs 6 KB
using GeneralLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SunVoteARSPPT
{
    public partial class FrmKeypadCountSet : Form
    {
        public FrmKeypadCountSet()
        {
            InitializeComponent();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //参与者人数要小于可用键盘数
            if (GlobalInfo.response.EnableList)
            {
                if (MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "SaveKeypadCountSet", "当前已启用名单,该操作将禁用名单,是否继续?"), GlobalInfo.GetAppName(), MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.No)
                    return;

            }
            GlobalInfo.response.EnableList = false;

            int personNum = Convert.ToInt32(nudMemberNumber.Value);
            //int avilableNum=GetAvilableKeyNum(txtKeypadZone.Text);
            if (txtKeypadZone.Text.Trim() != "")
            {
                int avilableNum = GlobalInfo.hardwareManage.GetKeyNum(txtKeypadZone.Text);
                if (avilableNum == -1)
                {
                    MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "Message1", "输入的范围格式有误,请重新输入"), GlobalInfo.GetAppName());
                    return;
                }
                if (avilableNum == -2)
                {
                    MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "Message2", "输入的键盘范围超出键盘ID的范围"), GlobalInfo.GetAppName());
                    return;
                }
                if ((avilableNum < personNum) && (avilableNum != 0))
                {
                    MessageBox.Show(GlobalInfo.SysLanguage.LPT.ReadString(this.Name, "Message3", "您设置的键盘数小于参与人数,请重新设置"), GlobalInfo.GetAppName());
                    return;
                }
            }
            GlobalInfo.hardwareManage.PersonNum = personNum;
            GlobalInfo.hardwareManage.RangeOfKey = txtKeypadZone.Text;
            //存到幻灯片Tag值中
            TagSet presTagSet = new TagSet();
            presTagSet.Tags = Globals.SunVoteARSAddIn.PPTEdit.PPT.ActivePresentation.Tags;
            presTagSet.SetValue(TagKey.Response_PersonNum, personNum);
            presTagSet.SetValue(TagKey.Response_KeyRange, txtKeypadZone.Text);
            //写配置文件 杨斌 2015-09-18
            GlobalInfo.sysConfig.WriteSysConfig("Device", "PersonNum", nudMemberNumber.Value);
            GlobalInfo.sysConfig.WriteSysConfig("Device", "RangeOfKey", txtKeypadZone.Text);
            this.Close();
        }

        /// <summary>
        /// 键盘数量
        /// 杨斌 2015-09-18
        /// </summary>
        public static int PersonNum
        {
            get
            {
                return INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).ReadInt("Device", "PersonNum", 100);
            }
        }
        /// <summary>
        /// 键盘范围
        /// 杨斌 2015-09-18
        /// </summary>
        public static string RangeOfKey
        {
            get
            {
                return GlobalInfo.hardwareManage.RangeOfKey = INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH).ReadString("Device", "RangeOfKey", "1-100");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void nudMemberNumber_ValueChanged(object sender, EventArgs e)
        {

        }

        private void FrmKeypadCountSet_Load(object sender, EventArgs e)
        {
            GlobalInfo.SysLanguage.SetLanguage(this.Name, this);
            nudMemberNumber.Value = GlobalInfo.hardwareManage.PersonNum;
            txtKeypadZone.Text = GlobalInfo.hardwareManage.RangeOfKey;
        }

        /// <summary>
        /// 获得可用键盘数量
        /// </summary>
        /// <returns></returns>
        private int GetAvilableKeyNum(string rangeOfKey)
        {
            int sectionNum = 0;
            //没有设置键盘有效范围
            if (rangeOfKey == "") { return 0; }
            string[] aryAvilableKey = null;
            //检验格式是否正确
            //if (!PublicFunction.IsMatchWith(rangeOfKey, @"^((\d)[,](\d))|((\d)[-](\d))"))
            //{
            //    return -1;
            //}
            aryAvilableKey = rangeOfKey.Split(',');
            for (int i = 0; i < aryAvilableKey.Length; i++)
            {
                if (aryAvilableKey[i].IndexOf('-') != -1)
                {
                    int minKey = Convert.ToInt32(aryAvilableKey[i].Substring(0, 1));
                    int pos = aryAvilableKey[i].LastIndexOf('-') + 1;
                    int maxKey = Convert.ToInt32(aryAvilableKey[i].Substring(pos, aryAvilableKey[i].Length - pos));
                    if (minKey > maxKey)
                    {
                        sectionNum = -1;
                        break;
                    }
                    for (int j = minKey; j <= maxKey; j++)
                    {
                        sectionNum += 1;
                    }
                }
                else
                {
                    sectionNum += 1;
                }
            }
            return sectionNum;
        }

        private void txtKeypadZone_KeyPress(object sender, KeyPressEventArgs e)
        {
            //限制只能输入数字,,,-
            //阻止从键盘输入键
            e.Handled = true;
            //当输入为0-9的数字、小数点、回车和退格键时不阻止
            if (e.KeyChar >= '0' && e.KeyChar <= '9' || e.KeyChar == ',' || e.KeyChar == ',' || e.KeyChar == '-' || e.KeyChar == 13 || e.KeyChar == (char)8)
            {
                if (e.KeyChar == ',')
                    e.KeyChar = ',';
                e.Handled = false;
            }


        }


    }
}