zoukankan      html  css  js  c++  java
  • C#关于控件的上下左右移动

    C#怎么让控件上下左右移动?(转)

    http://wenwen.sogou.com/z/q231436494.htm

    在winform中捕获上下左右键等控制键的按键事件(转)

    http://blog.csdn.net/jiankunking/article/details/47320439

    C#中 根据左右上下键控制不多个控件的焦点(转)

    如图  有多个控件 根据上下左右键 控制每个控件的焦点;

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            //定义一个二位数组  存放TextBox控件
            TextBox[,] arr;

            private void Form1_Load(object sender, EventArgs e)
            {
                //初始化数组
                arr = new TextBox[3, 3]{{textBox1,textBox2,textBox3},
                   {textBox4,textBox5,textBox6},
                   {textBox7,textBox8,textBox9}
                };

            }

            /// <summary>
            /// 窗体键盘事件   注: 启用此事件之前 先设置KeyPreView属性为True
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                //首先获取当前焦点的控件
                TextBox txt = (TextBox)GetFocusedControl();

                //获取当前焦点控件的在数组中对应的位置  此值事先存放在控件的Tag属性中
                int x = int.Parse(txt.Tag.ToString().Substring(0, 1));
                int y = int.Parse(txt.Tag.ToString().Substring(1, 1));

                if (e.KeyCode == Keys.Left)
                {
                    if (y > 0)
                    {
                        arr[x, y-1].Focus();
                    }
                }
                else if (e.KeyCode == Keys.Right)
                {
                    if (y < 2)
                    {
                        arr[x,y + 1].Focus();
                    }
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (x > 0)
                    {
                        arr[x-1, y].Focus();
                    }
                }
                else if (e.KeyCode == Keys.Down)
                {
                    if (x < 2)
                    {
                        arr[x+1, y].Focus();
                    }
                }
            }

            // DLL调用注册
            [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
            private static extern IntPtr GetFocus();
            /// <summary>
            /// 当前拥有焦点的控件
            /// </summary>
            /// <param name="formControl"></param>
            /// <returns></returns>
            public static Control GetFocusedControl()
            {
                Control focusedControl = null;
                try
                {
                    IntPtr focusedHandle = GetFocus();

                    if (focusedHandle != IntPtr.Zero)
                    {
                        focusedControl = Control.FromChildHandle(focusedHandle);
                    }
                }
                catch { }

                return focusedControl;
            }
        }

    C# Winform 自适应大小 按上下键切换控件焦点(转)

    按上下键切换控件焦点(只对textbox有效)

    复制代码
    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.KeyValue == 40 || e.KeyValue == 13)
    {
    SendKeys.Send("{TAB}");
    }
    if (e.KeyValue == 38)
    {
    int index= this.textBox2.TabIndex;
    for (int i = 0; i < this.Controls.Count; i++)
    {
    if (this.Controls[i].TabIndex == (index - 1))
    {
    this.Controls[i].Focus();
    break;
    }
    }
    }
    }
    复制代码

    自适应大小

    复制代码
    private void Form1_SizeChanged(object sender, EventArgs e)
    {

    if (this.WindowState.ToString() == "Maximized")
    {
    this.dataGridView1.Width = this.Size.Width-35;
    }
    else if (this.WindowState.ToString() == "Normal")
    {
    this.dataGridView1.Width = this.Size.Width-35;
    }
    else
    {
    this.dataGridView1.Width = this.Size.Width-35;
    }
    }
    复制代码
  • 相关阅读:
    js 自定义属性
    js innerText、textContent、innerHTML的区别和各自用法
    js 的常用选择器
    js Array属性和用法
    js---String对象
    iframe自适应高度js
    thinkphp 的save()不能更新数据解决办法
    转义字符
    获取客户端真实ip
    thinkphp条件查询和模糊查询的一些方法
  • 原文地址:https://www.cnblogs.com/candyzhmm/p/5612090.html
Copyright © 2011-2022 走看看