TextBox编辑框间的自动跳转C#实现

来源:互联网 发布:公司网封端口 编辑:程序博客网 时间:2024/06/05 09:39

为了满足用户对于编辑框操作的一些键盘操作,让编辑框间的跳转变得更加友好。我实现了这样的一个小程序来实现编辑框的自动跳转功能。

对于自动跳转,我设计了以下几条规则:

上下跳转:在控件的宽度带范围,距离此控件最近的其他控件,才能实现上下跳转。


例如:本图中的1向下可以跳转的选择只有2,而没有3

左右跳转:在控件的高度的五倍的高度带范围内的左右跳转才可实现,仅在此范围内的距离本控件最近的其他控件中进行择优选择。


例如:在本图中的2想要向右跳转,就不应该考虑控件5.


在上述的规则条件下,我设计了向上向下向左向右跳转的算法。通过计算满足条件的两个控件的距离来实现自动跳转。

计算两点间距离:

        /// <summary>        /// 计算两点间距离        /// </summary>        /// <param name="p1"></param>        /// <param name="p2"></param>        /// <returns>距离</returns>        private double calcDistance(Point p1, Point p2)        {            return Math.Sqrt(Math.Abs(p1.X - p2.X) * Math.Abs(p1.X - p2.X) + Math.Abs(p1.Y - p2.Y) * Math.Abs(p1.Y - p2.Y));        }

对于每个控件,都将其的横纵坐标保存在一个Point中。然后再计算结束后,需要知道某个点对应的是那个控件。

        /// <summary>        /// 判断某个点所在的Textbox        /// </summary>        /// <param name="p">点</param>        /// <returns>点所在的控件</returns>        private TextBox equalLocation(Point p)        {            foreach (Control control in Controls)            {                if (control is TextBox)                {                    if (control.Location.Equals(p))                    {                        TextBox tb = (TextBox)control;                        return tb;                    }                }            }            return null;        }

向上跳转(和向下类似):

        /// <summary>        /// 选取距离某点向上最近的那个点        /// </summary>        /// <param name="p">某点</param>        /// <returns></returns>        private Point ToUP(Point point)        {            int[] index = new int[7];            int j = 0;            double[] distance = new double[7];            double minDistance = 0.0;            int minIndex = 0;            for (int i = 1; i < mp.Length; i++)            {                if (mp[i] != point && mp[i].Y < point.Y)  //必须在原控件的上边                {                    if ((mp[i].X - point.X >= 0 && mp[i].X - point.X <= standardWidth) || (point.X - mp[i].X >= 0 && point.X - mp[i].X <= standardWidth))                    {                        index[j] = i;                        distance[j] = calcDistance(mp[i], point);                        j++;                    }                }            }            minDistance = distance[0];            minIndex = index[0];            for (int k = 1; k < distance.Length; k++)//找到距离最小的控件所在的点            {                if (distance[k] > 0)                {                    if (distance[k] < minDistance)                    {                        minDistance = distance[k];                        minIndex = index[k];                    }                }            }            return mp[minIndex];        }

向左跳转(和向右类似):

        /// <summary>        /// 选取距离某点向左最近的那个点        /// </summary>        /// <param name="point"></param>        /// <returns></returns>        private Point ToLeft(Point point)        {            int[] index = new int[7];            int j = 0;            double[] distance = new double[7];            double minDistance = 0.0;            int minIndex = 0;            for (int i = 1; i < mp.Length; i++)            {                if (mp[i] != point && mp[i].X < point.X)  //必须在原控件的左边                {                    if ((mp[i].Y - point.Y >= 0 && mp[i].Y - point.Y <= 2 * standardHeight) || (point.Y - mp[i].Y >= 0 && point.Y - mp[i].Y <= 3 * standardHeight))                    //可跳转的范围是5个原控件宽度的宽度带                    {                        index[j] = i;                        distance[j] = calcDistance(mp[i], point);                        j++;                    }                }            }            minDistance = distance[0];            minIndex = index[0];            for (int k = 1; k < distance.Length; k++)//找到距离最小的控件所在的点            {                if (distance[k] > 0)                {                    if (distance[k] < minDistance)                    {                        minDistance = distance[k];                        minIndex = index[k];                    }                }            }            return mp[minIndex];        }

使用说明:编辑框之间可以用↑、↓、Ctrl+←、Ctrl+→进行焦点切换


原创粉丝点击