简体   繁体   English

C# WinForm TabControl 格式问题

[英]C# WinForm TabControl Formatting Issue

When programmatically adding controls to a tab control, I have been using the Form_Load event to create and embed things like datagridviews into my UI.当以编程方式向选项卡控件添加控件时,我一直在使用 Form_Load 事件来创建数据网格视图之类的内容并将其嵌入到我的 UI 中。 I made a class that inherits from DataGridView我创建了一个继承自 DataGridView 的类

class DBDataGridView : DataGridView
{
    public DBDataGridView()
    {
        DoubleBuffered = true;
        AllowUserToAddRows = false;
        AllowUserToDeleteRows = false;
        AllowUserToResizeRows = false;
        AllowUserToOrderColumns = false;
        AllowUserToResizeColumns = false;
        RowHeadersVisible = false;
        AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        ReadOnly = true;
        Dock = DockStyle.Fill;
        SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        TabStop = false;
    }
}

And I call it later in the Form_Load event like so我稍后会像这样在 Form_Load 事件中调用它

    private void MainDesignerForm_Load(object sender, EventArgs e)
    {
        DBDataGridView _DGV = new DBDataGridView();

        var listOfOverlays = new List<OverlaySelectionList>()
        {
            new OverlaySelectionList { Description = "Description 1", PartNumber = "123-R1"},
            new OverlaySelectionList { Description = "Description 2", PartNumber = "456-R1"}
        };
        var overlayList = new BindingList<OverlaySelectionList>(listOfOverlays);
        _DGV.DataSource = overlayList;
        Tab_Overlay.Controls.Add(_DGV);
        _DGV.ClearSelection();
    }

This gridview is on the THIRD tab of the TabControl, and everything works as expected except the ClearSelection().此网格视图位于 TabControl 的第三个选项卡上,除 ClearSelection() 外,一切都按预期工作。 No matter where I call it, it does not clear the initial row selection of the DGV.无论我在哪里调用它,它都不会清除 DGV 的初始行选择。 However, if I fire the same code block from a button ON the third tab, the formatting AND the ClearSelection() behave as expected.但是,如果我从第三个选项卡上的按钮触发相同的代码块,则格式和 ClearSelection() 的行为将符合预期。

What is causing this behavior?是什么导致了这种行为?

Thanks to 41686d6564 and Jimi for the insight into the specifics on why this was happening.感谢 41686d6564 和 Jimi 深入了解为什么会发生这种情况。

Reiterating what they said in the comments: Assignment of properties appear to be cached regardless of whether the control they belong to is active or not (Hence why all the sizing and formatting properties were present at run time).重申他们在评论中所说的话:无论它们所属的控件是否处于活动状态,属性的分配似乎都被缓存了(因此为什么所有的大小和格式属性都在运行时出现)。 However, actions that require a handle, like ClearSelection() require the control to be shown and active for the intended behavior to be observed.但是,需要句柄的操作,例如 ClearSelection() 需要显示控件并使其处于活动状态,以便观察到预期的行为。

Setting the selected tab to where the DataGridView before calling ClearSelection() was the solution (Or in my case, I had nested tabs, so I had to follow the tab tree to get to the specific tab that the DataGridView was on)在调用 ClearSelection() 之前将选定的选项卡设置为 DataGridView 的位置是解决方案(或者在我的情况下,我有嵌套的选项卡,所以我必须按照选项卡树到达 DataGridView 所在的特定选项卡)

So now, part of the Load_Form logic is to check WHERE the control is located, make that tab active, THEN format and clear selections for each control that is being added.所以现在,Load_Form 逻辑的一部分是检查控件所在的位置,激活该选项卡,然后格式化并清除正在添加的每个控件的选择。 This allowed ClearSelection() to work as intended.这允许 ClearSelection() 按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM