【问题标题】:dynamic buttons on panel1 - locations [closed]panel1上的动态按钮-位置[关闭]
【发布时间】:2016-02-14 15:41:32
【问题描述】:

所以我有一个带有可滚动面板的应用程序,它会在面板上动态列出文件夹。在文件夹列表下方,我还有一个按钮。单击按钮后,它会在每个文件夹列表旁边动态创建一个按钮。我的每个按钮的 Y 值每次都会增加 =+ 26 - 如果我的面板还不能滚动,这一切都很好。一旦面板变得可滚动并且我向下滚动然后继续添加更多按钮,它会跳过几个空格,然后只添加更多按钮。我认为这与更改为 Y 位置的滚动有关?我该如何解决这个问题?

C# WinForms

感谢和问候。

private void button1_Click(object sender, EventArgs e)
{
    Button newButton = new Button();
    newButton.Location = new System.Drawing.Point(0, (y+6));
    buttons.Add(newButton);
    y += 20;
    panel1.Controls.Add(newButton);
}

【问题讨论】:

  • 是精灵魔法的功劳!!!除了笑话......请分享一些代码,如果没有代码,它会很暗!
  • 你的目标是什么:Winforms? WPF? ASP? ...?? 总是相应地标记您的问题!
  • 为什么需要? List

标签: c# winforms scrollable panels


【解决方案1】:

只需将您的动态按钮行替换为以下内容:

panel1.AutoScroll = true;
        Button myButton = new Button();
        myButton.Location = new Point(
           panel1.AutoScrollPosition.X,
           panel1.AutoScrollPosition.Y + (y+6));
        y += 20;
        panel1.Controls.Add(myButton);

【讨论】:

    【解决方案2】:

    您可以尝试从最后一个按钮动态获取按钮的 y 位置:在此示​​例中为 (i-1),

        private void button1_Click(object sender, EventArgs e)
        {
            Button newButton = new Button();
            newButton.Location = new System.Drawing.Point(0, buttons[i -1].Location.Y + 26);
            i++;
            buttons.Add(newButton);
            panel1.Controls.Add(newButton);
        }
    

    【讨论】:

      【解决方案3】:

      您可以使用类来创建和管理按钮,例如

      using System;
      using System.Collections.Generic;
      using System.Drawing;
      using System.Windows.Forms;
      
      public class CreateButtons
      {
          /// <summary>
          /// Size to create each button
          /// </summary>
          /// <returns></returns>
          public Size ButtonSize { get; set; }
          /// <summary>
          /// Provides a reference to your buttons
          /// </summary>
          /// <returns></returns>
          public List<Button> Buttons { get; set; }
          /// <summary>
          /// base name of buttone e.g. btn, cmd etc.
          /// </summary>
          /// <returns></returns>
          public string ButtonBaseName { get; set; }
          /// <summary>
          /// Used to spread out buttons
          /// </summary>
          /// <returns></returns>
          public int Base { get; set; }
          public int BaseAddition { get; set; }
          /// <summary>
          /// Count of buttons
          /// </summary>
          /// <returns></returns>
          public int ButtonCount { get; set; }
          /// <summary>
          /// control to place buttons onto
          /// </summary>
          /// <returns></returns>
          public Control ParentControl { get; set; }
          public CreateButtons(string BaseName)
          {
              this.ButtonBaseName = BaseName;
          }
          /// <summary>
          /// Create single button and add it to the list of buttons property
          /// </summary>
          /// <param name="item"></param>
          public void CreateSingleButton(string item)
          {
              if (Buttons == null)
              {
                  Buttons = new List<Button>();
              }
              Button b = new Button
              {
                  Name = string.Concat(ButtonBaseName, item),
                  Text = item,
                  Size = ButtonSize,
                  Location = new Point(25, this.Base),
                  Parent = ParentControl,
                  Visible = true
              };
      
              // wire up an event or have an event it gets wired too
              b.Click += (object s, EventArgs e) =>
              {
                  //Button tb = (Button)s;
                  //MessageBox.Show(tb.Text);
              };
      
              this.ParentControl.Controls.Add(b);
              Buttons.Add(b);
              Base += BaseAddition;
          }
      }
      

      在表单设置中,有一个可滚动的面板、一个列表框(显示我们可以访问所有创建的按钮)、一个文本框,允许您通过为每个按钮命名来测试它。用于创建单个按钮的按钮,该按钮将添加到列表中。

      using System;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Windows.Forms;
      
      namespace CreateDynamicButtons1_cs
      {
          public partial class Form1 : Form
          {
              private CreateButtons buttons = new CreateButtons("btn");
      
              public Form1()
              {
                  InitializeComponent();
              }
      
              private void Form1_Load(object sender, EventArgs e)
              {
                  buttons.ParentControl = panel1;
                  buttons.Base = 10;
                  buttons.BaseAddition = 35;
                  buttons.ButtonSize = new Size(50, 25);
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  if (!string.IsNullOrWhiteSpace(textBox1.Text))
                  {
                      buttons.CreateSingleButton(textBox1.Text);
                  }
              }
              private void button2_Click(object sender, EventArgs e)
              {
                  if (buttons.Buttons != null)
                  {
                      listBox1.DataSource = 
                          buttons.Buttons.Select(item => item.Name).ToList();
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 2012-09-15
        相关资源
        最近更新 更多