C#關於控制項的上下左右移動

來源:互聯網
上載者:User

標籤:

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;
}
}

 

C#關於控制項的上下左右移動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.