WinForms 如何显示来自 FlowDirectionPanel 的特定面板

WinForms how to show specific panel from FlowDirectionPanel

我有以下 FlowDirectionPanel http://prntscr.com/aao6lt 你可以看到这些是扑克的成就 game.When 你获得了一个新成就 yes/no 消息框弹出问你是否想去看看你的新获得如果你按是我希望能够自动导航到新解锁的成就,它包含在 FlowDirectionPanel 中,如上图所示。此外,每个成就都包含在另一个面板中,该面板是 flowPanel 的子面板,如果您仔细观察,您会发现它们有一些轮廓边框。我动态创建和添加面板,但它们确实有可以帮助我导航到所需面板的名称。这就是我创建它们的方式:

        public void PanelForAchievements(Form currentForm, FlowLayoutPanel flp, AchivementRequirements achivement)
    {
        FlowLayoutPanel retFlp = flp;
        string pGetAchivementName = @"pGet" + achivement.Name;
        string lbAchivementName = @"lb" + achivement.Name;
        string lbAchivementRewardName = @"lb" + achivement.Name + @"Reward";
        string cbGetAchivementName = @"cbGet" + achivement.Name;
        string pbAchivementName = @"pb" + achivement.Name;
        var pGetAchivement = new Panel
        {
            Name = pGetAchivementName,
            Size = new Size(retFlp.Width, 100),
            BorderStyle = BorderStyle.FixedSingle,
        };
        currentForm.Controls.Add(pGetAchivement);

        var lbAchivement = new Label
        {
            Name = lbAchivementName,
            Location = new Point(pGetAchivement.Location.X, pGetAchivement.Location.Y),
            Size = new Size(135, 30),
            AutoSize = false,
            BorderStyle = BorderStyle.FixedSingle,
            Font = new Font("Microsoft Sans Serif", 10F, FontStyle.Regular, GraphicsUnit.Point, (byte)0),
            Text = achivement.TitleText,
        };

        var lbAchivementReward = new Label
        {
            Name = lbAchivementRewardName,
            AutoSize = true,
            Top = (pGetAchivement.Height - pGetAchivement.Height) / 2,
            Text = achivement.RewardLabelText,
            TabIndex = 2,
            BorderStyle = BorderStyle.FixedSingle,
            Location = new Point(lbAchivement.Location.X, lbAchivement.Location.Y + lbAchivement.Height + 5)
        };

        var cbGetAchivement = new CheckBox
        {
            Name = cbGetAchivementName,
            AutoCheck = false,
            AutoSize = true,
            Location = new Point(lbAchivement.Location.X + lbAchivement.Width + 10, lbAchivement.Location.Y),
            TabIndex = 1,
            UseVisualStyleBackColor = true
        };
        achivement.IsUnlocked(MainPoker.AllAchievements[achivement.EnumCasted], achivement.Requirement,cbGetAchivement);

        var pbAchivement = new PictureBox
        {
            BorderStyle = BorderStyle.Fixed3D,
            Name = pbAchivementName,
            Dock = DockStyle.Right,
            BackgroundImageLayout = achivement.PictureBoxImageLayout,
            //Location = new Point(pGetAchivement.Right, pGetAchivement.Location.Y),
            Size = new Size(145, 90),
            SizeMode = PictureBoxSizeMode.Zoom,
            TabIndex = 9,
            TabStop = false,
            Image = achivement.PackPreview,
        };

        pGetAchivement.Controls.Add(lbAchivement);
        pGetAchivement.Controls.Add(lbAchivementReward);
        pGetAchivement.Controls.Add(cbGetAchivement);
        pGetAchivement.Controls.Add(pbAchivement);

        retFlp.Controls.Add(pGetAchivement);

        achivement.Title = lbAchivement;
        achivement.RewardLabel = lbAchivementReward;
        achivement.Unlocked = cbGetAchivement;
        achivement.Preview = pbAchivement;
    }

这只是简单的代码,这就是我初始化它们的方式:

    public static RoyalFlush RoyalFlush = new RoyalFlush(1, new Tuple<string, int?>("Royal Card Pack", 100000));
    private readonly CreatePanels _createAchivementPanels = new CreatePanels();
    foreach (var achi in AchivementRequirements.AchivementList)
    {
                    _createAchivementPanels.PanelForAchievements(this, pAchievementsCards, achi);
//im also doing other stuff here..
//pAchivementsCards is the name of the FlowDirectionPanel
    }

现在,当显示 yes/no 消息框时,我已经知道解锁了哪个成就,而且我的成就有 类,如上所示 public static RoyalFlush RoyalFlush 和那些 类有 属性 - Name,其中显然包含成就的名称,我使用此名称为我从 public static RoyalFlush RoyalFlush 创建的每个控件创建各自的名称,例如:

string pGetAchivementName = @"pGet" + achivement.Name;

p 代表 panel,我只是使用 属性 achivement.Name 获取当前成就名称,我们最终得到这样的结果:pRoyalFlush 作为我们面板的名称.现在我知道了面板的名称以及正在解锁的成就,我需要浏览我的 FlowDirectionPanel 并找到特定的面板并将焦点留在那里。我不知道该怎么做,如果现在还不清楚,我将展示一个带有我想要的图片的示例:

我没有 post 超过 2 个链接的声誉,所以我不得不在其中添加一些点.. 这是我的第一个问题,我的母语不是英语,所以请原谅我犯的任何错误。

您可以制作一些 foreahc 循环,在其中比较名称,然后,当您找到所需的控件时,将您的 flowlayoutpanel 滚动到它。比如我的flowlayoutpanel有4个按钮,只有2个可见,我想滚动到button4:

        foreach (Control c in flowLayoutPanel1.Controls)
        {
            if ((c as Button).Name == "button4")
            {
                (c as Button).Focus();
                flowLayoutPanel1.ScrollControlIntoView(c);
                break;
            }
        }

希望我能正确回答你的问题。