简体   繁体   English

C#WinForms面板问题

[英]C# WinForms Panel Problem

I have an annoying problem - I want to use a listview with images as Menu. 我有一个烦人的问题 - 我想使用带有图像的列表视图作为菜单。 Let's say the listview contained 3 "images". 假设列表视图包含3个“图像”。

  • Image 1. ) "Main" 图1.)“主要”
  • Image 2. ) "Options" 图2.)“选项”
  • Image 3. ) "Misc" 图3.)“杂项”

Now when I click the "Main" image, I would display the "Main" panel, same for "Options" image where I display the "Options" panel. 现在,当我单击“Main”图像时,我将显示“Main”面板,同样显示“Options”图像,其中我显示“Options”面板。 but here's the annoying part. 但这是烦人的部分。 I have 3 panels on my form. 我的表格上有3个面板。 So whenever I want to edit one panel, I have to resize the other two just so i can edit the first one, since they are all thrown on the form. 因此,每当我想编辑一个面板时,我必须调整其他两个面板的大小,以便我可以编辑第一个面板,因为它们都被抛在窗体上。 It gets really annoying to edit the panels. 编辑面板真的很烦人。

Is there some kind of control equivalent to the wxWidgets wxListBook control? 是否有某种控件相当于wxWidgets wxListBook控件? Or does anyone have an idea how to solve this? 或者有人知道如何解决这个问题吗? I'm asking about design time 我在问设计时间

Thank you! 谢谢!

I assume that you are talking about editing the panels at design time and not run time. 我假设你在谈论在设计时编辑面板而不是运行时间。 Just make all your panels the same size, lay them right on top of one another, and when you name them, make sure they all have the same prefix. 只需将所有面板设置为相同尺寸,将它们放在彼此的顶部,当您命名它们时,请确保它们都具有相同的前缀。 Select the panel that shows on the top, and if it is not the one you want, use the dropdown at the top of the Properties window to select the panel that you want, then right-click on the highlighted Panel grip (moving and resizing thingys) and select 'BringToFront'. 选择顶部显示的面板,如果它不是您想要的面板,请使用“属性”窗口顶部的下拉列表选择所需的面板,然后右键单击突出显示的Panel夹点(移动和调整大小) thingys)并选择'BringToFront'。

You could just display the current item panel and hide the other panels. 您可以只显示当前项面板并隐藏其他面板。 On your form, add an empty panel which is the container for your list view item panels. 在表单上,​​添加一个空面板,它是列表视图项面板的容器。

private Panel container;

private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
    //get the item panel associated with the current selection.
    Panel itemPanel = GetItemPanelFromSelection(); 

    container.Controls.Clear();
    container.Controls.Add(itemPanel);
    itemPanel.Dock = DockStyle.Fill;
}

Personnally, I would create an user control for each type of panel that you want. 在个人情况下,我会为您想要的每种类型的面板创建一个用户控件。 The advantage is you can edit them easily. 优点是您可以轻松编辑它们。 After that, you can use the same code above. 之后,您可以使用上面相同的代码。

I've done this before - I just make the window large enough to fit all panels. 我以前做过这个 - 我只是让窗户足够大以适应所有面板。 At runtime, I hide all but the default panel, and set their locations equal. 在运行时,我隐藏除默认面板之外的所有面板,并将它们的位置设置为相等。 Additionally, I set the size of the parent window (or control) to be the size I want such that one panel fits nicely in the window. 另外,我将父窗口(或控件)的大小设置为我想要的大小,使得一个面板非常适合窗口。 Your selection method (clicking images) seems ok, as you'd detect which image is clicked, and show/hide the corresponding panels. 您的选择方法(单击图像)似乎没问题,因为您将检测到单击了哪个图像,并显示/隐藏相应的面板。

It's a little bit clunky, but for some cases it works just fine. 它有点笨重,但在某些情况下它运作得很好。 This works great for applications where you want to see all the panels at design time, but only see one panel at runtime. 这适用于您希望在设计时查看所有面板但仅在运行时看到一个面板的应用程序。 Thus, using Visual Studio's built-in localization tools will continue to work. 因此,使用Visual Studio的内置本地化工具将继续工作。

Here's some sample code I've copied from a project. 这是我从项目中复制的一些示例代码。 This is actually a Settings window, where I select panels to view from a TreeView. 这实际上是一个设置窗口,我在其中选择要从TreeView查看的面板。

Width = 640;  // set the size of the form, as it's larger in Designer mode
Height = 480;

simulationPanel.Visible = false;   // hide all panels, and set them to be top-left
simulationPanel.Top = 0;           // relative to their parent control
simulationPanel.Left = 0;

delaysPanel.Visible = false;
delaysPanel.Top = 0;
delaysPanel.Left = 0;

occurrencesPanel.Visible = false;
occurrencesPanel.Top = 0;
occurrencesPanel.Left = 0;

languagePanel.Visible = false;
languagePanel.Top = 0;
languagePanel.Left = 0;

Then, to select a panel, you might do something like the following: 然后,要选择面板,您可能会执行以下操作:

private void ShowPanel(string name)
{

     // its easy to just hide all panels again if one is currently visible
     simulationPanel.Visible = false;
     delaysPanel.Visible = false;
     occurrencesPanel.Visible = false;
     languagePanel.Visible = false;

     if (name == "language")
     {
          languagePanel.Visible = true; 
     } else if (name = "delays")
     {
          delaysPanel.Visible = true;
     }
     ... etc
}

For editing the panels in Visual Studio think about building the panels themselves as custom controls — and perhaps have them inherit from a common base type. 要在Visual Studio中编辑面板,请考虑将面板本身构建为自定义控件 - 并且可能让它们从公共基本类型继承。 Then they each get their own designer window. 然后他们每个人都有自己的设计师窗口。

As for the menu, what about setting all the panels full size so they perfectly overlap? 至于菜单,如何将所有面板全尺寸设置为完全重叠? You can layer controls on top of each other, after all. 毕竟,您可以将控件叠加在一起。 Then you can just call .BringToFront() whenever the user makes a menu selection. 然后,只要用户进行菜单选择,您就可以调用.BringToFront()

Create your own TabControl descendant as shown below. 创建自己的TabControl后代,如下所示。 This code was obtained from here : 这段代码来自这里

using System;
using System.Windows.Forms;

public class MyTabControl : TabControl {
    protected override void WndProc(ref Message m)
    {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328 && !DesignMode)
            m.Result = (IntPtr)1;
        else
            base.WndProc(ref m);
    }
}

Create a tab page for each menu panel that you mention. 为您提到的每个菜单面板创建一个标签页。 At design time you can easily switch between the panels by clicking on the appropriate tab page header tab. 在设计时,您可以通过单击相应的标签页标题选项卡轻松切换面板。 Associate each menu item with a tab page, so that at run time, whenever you select your menu item you can activate/select the appropriate tab page to display the intended panel. 将每个菜单项与标签页相关联,以便在运行时,无论何时选择菜单项,都可以激活/选择相应的标签页以显示预期的面板。 At run time the tab header will not be visible. 在运行时,标签页眉将不可见。 I tested the above and it works. 我测试了上面的,它的工作原理。

Alternatively, you might find a tab control that has built-in support to hide the tab header. 或者,您可能会发现一个选项卡控件,它具有内置支持以隐藏选项卡标题。 For example, I use the tab control available in the DevExpress WinForm controls suite , which supports hiding the headers. 例如,我使用DevExpress WinForm控件套件中提供的选项卡控件 ,它支持隐藏标头。

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

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