簡體   English   中英

如何將TableLayoutPanel中的控件移動到另一個單元格並將其還原回C#中?

[英]How to move controls in TableLayoutPanel to another cell and restore it back in C#?

我正在嘗試制作對屏幕分辨率有反應的自定義控件。

我的想法很簡單。

為此,我必須能夠在TableLayoutPanel的單元格中移動控件。

因此,我正在嘗試下面的這些代碼。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void _btnMove_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.Height    += 30;
            tableLayoutPanel1.RowCount  = 3;
            tableLayoutPanel1.Controls.Add(flowLayoutPanel4, 0, 2);
            tableLayoutPanel1.SetColumnSpan(flowLayoutPanel4, 2);
        }

        private void _btnRestore_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.Controls.Add(flowLayoutPanel4, 6, 0);
        }
    }
}

看起來工作不錯。

在此處輸入圖片說明

這是TableLayoutPanel的原始圖像。

當我按下“移動”按鈕時

在此處輸入圖片說明

是的,tablelayoutpanel現在具有3行,並且Search和Excel按鈕位於第三行。

現在,我必須首先還原它們。

在此處輸入圖片說明

在這里,它似乎工作不正常。

我試圖將它們發送到6行,0行位置。

但是它在另一個單元格中。

有誰知道在哪里修復它?

您需要撤消在_btnMove_Click方法中所做的_btnMove_Click 列跨度仍設置為2是導致其向下移動到第二行的原因。

private void _btnRestore_Click(object sender, EventArgs e)
{
    tableLayoutPanel1.Height -= 30;
    tableLayoutPanel1.RowCount = 2;
    tableLayoutPanel1.SetColumnSpan(flowLayoutPanel1, 1);
    tableLayoutPanel1.Controls.Add(flowLayoutPanel1, 6, 0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM