FrmImportSlideSelect.cs 3.29 KB
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 FrmImportSlideSelect : Form
    {
        public FrmImportSlideSelect()
        {
            InitializeComponent();
        }

        private void FrmImportSlideSelect_Load(object sender, EventArgs e)
        {
            GlobalInfo.SysLanguage.SetLanguage(this.Name, this);
        }

        /// <summary>
        /// 获取题目编号范围。若返回=null则为取消。若count=0则未选择。
        /// 杨斌 2015-08-18
        /// </summary>
        /// <returns></returns>
        public static List<int> GetNumRange()
        {
            List<int> res = null;

            FrmImportSlideSelect frm = new FrmImportSlideSelect();
            frm.ShowDialog();
            if (frm.DialogResult == DialogResult.OK)
                res = GetNumRange(frm.txtZone.Text);

            return res;
        }

        /// <summary>
        /// 从字符串表达式获取编号列表。如"1-3,5,7"={1,2,3,5,7}
        /// 杨斌 2015-08-18
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        public static List<int> GetNumRange(string range)
        {
            List<int> res = new List<int>();

            if (string.IsNullOrEmpty(range))
                return res;

            string[] ary = range.Split(',');
            if (ary.Length > 0)
            {
                foreach (var v in ary)
                {
                    int num = 0;
                    string[] ary2 = v.Split('-');
                    if (ary2.Length > 1)
                    {
                        int nA = 0;
                        int nB = 0;
                        if (int.TryParse(ary2[0], out nA))
                        {
                            if (int.TryParse(ary2[1], out nB))
                            {
                                if ((nA > 0) && (nB > 0))
                                {
                                    if (nA > nB)
                                    {
                                        num = nA;
                                        nA = nB;
                                        nB = num;
                                    }
                                    for (int i = nA; i <= nB; i++)
                                    {
                                        if (!res.Contains(i))
                                        {
                                            res.Add(i);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (int.TryParse(v, out num))
                        {
                            if (num > 0)
                            {
                                if (!res.Contains(num))
                                {
                                    res.Add(num);
                                }
                            }
                        }
                    }

                }
            }

            return res;
        }
    }
}