【问题标题】:C# Adding button with value at runtime [closed]C#在运行时添加带有值的按钮[关闭]
【发布时间】:2016-09-07 00:25:58
【问题描述】:

我想在运行时向我的选项卡控件添加一个具有值的按钮。很多教程展示了它是如何在创建与数据库的连接时完成的。有什么方法可以不连接数据库吗?

在我将数据输入两个文本框并单击保存后,新按钮应出现在另一个表单的选项卡控件上。

【问题讨论】:

  • 向我们展示您的代码。单独添加按钮与连接数据库完全无关,所以是的,可以这样做。
  • 这个问题的一个关键部分是您使用的是什么框架:您是否尝试向网页添加按钮?到 Windows 应用程序 (WPF)?
  • @JamieF 我正在使用窗体应用程序
  • @Glubus 我已经编辑了我的帖子。

标签: c# winforms


【解决方案1】:

在你保存按钮放:

 private void btnSave_Click(object sender, EventArgs e)
            {
                x = 4;
                 y = panel1 .Controls.Count * 70;
                Button newButton = new Button ();
                newButton.Height = 150;
                newButton.Width = 60;
                newButton.Location = new Point(x, y);
                newButton.Text= "your text";
                 newButton.Click += new       
              System.EventHandler(Button_Click);             
              tabControl1.TabPages [0].Controls.Add(newButton);

        }

您还可以处理创建的新按钮的点击:

public void Button_Click(object sender, EventArgs e)
    {
        Button button = (Button)sender ;
        MessageBox.Show("Button is pressed "+button .Text );

    }

【讨论】:

  • 谢谢,您的代码正在显示选项卡控件上的新按钮,但保存按钮没有从文本框中获取数据。
  • 你可以改变 newButton.Text="your text";到 newButton.Text= textbox1.text";
  • 如果回答对你有帮助,请标记,谢谢
  • 是的,它有帮助。现在我正在尝试在新按钮出现时调整选项卡控件上的位置。
  • 好的,谢谢,我已经编辑了我的答案来告诉你如何改变位置,祝你好运
【解决方案2】:

我提出这样的决定

主要形式:

namespace stackoverflow
{
    public partial class MainForm : Form
    {
        private static Form2 new_form = new Form2();

        public MainForm()
        {    
            InitializeComponent();
            new_form.Show();
        }


        void Button1Click(object sender, EventArgs e)
        {
            new_form.CreateBtn( richTextBox1.Text );
        }
    }
}

第二种形式:

namespace stackoverflow
{
    public partial class Form2 : Form
    {
        private Button btn = null;

        public Form2()
        {
            InitializeComponent();
        }

        public void CreateBtn( string text ) {
            if ( btn == null ) {
                btn= new Button();
                btn.Parent = this.tabPage1;
            }
            btn.Text = text;
            this.Refresh();
        }
    }
}

【讨论】:

    【解决方案3】:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Buttons
    {
        public partial class Form1 : Form
        {
            const int ROWS = 5;
            const int COLS = 10;
            public Form1()
            {
                InitializeComponent();
                this.Load += new System.EventHandler(this.Form1_Load);
            }
            public void Form1_Load(object sender, EventArgs e)
            {
                new MyButton(ROWS, COLS, this);
            }
    
    
        }
        public class MyButton : Button
        {
            const int WIDTH = 50;
            const int HEIGHT = 50;
            const int SPACE = 5;
            const int BORDER = 20;
    
            public static List<List<MyButton>> buttons { get; set; }
            public static List<MyButton> buttonList { get; set; }
            public Form1 form1;
            public int row { get; set; }
            public int col { get; set; }
            public MyButton()
            {
            }
            public MyButton(int rows, int cols, Form1 form1)
            {
                buttons = new List<List<MyButton>>();
                buttonList = new List<MyButton>();
    
                this.form1 = form1;
                for(int row = 0; row < rows; row++)
                {
                    List<MyButton> newRow = new List<MyButton>();
                    buttons.Add(newRow);
                    for (int col = 0; col < cols; col++)
                    {
                        MyButton newButton = new MyButton();
                        newButton.Height = HEIGHT;
                        newButton.Width = WIDTH;
                        newButton.Top = row * (HEIGHT + SPACE) + BORDER;
                        newButton.Left = col * (WIDTH + SPACE) + BORDER;
                        newButton.row = row;
                        newButton.col = col;
                        newRow.Add(newButton);
                        buttonList.Add(newButton);
                        newButton.Click += new System.EventHandler(Button_Click);
                        form1.Controls.Add(newButton);
                    }
                }
            }
            public void Button_Click(object sender, EventArgs e)
            {
                MyButton button = sender as MyButton;
                MessageBox.Show(string.Format("Pressed Button Row {0} Column {1}", button.row, button.col));
    
            }
    
        }
    }
    

    动态

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication72
    {
        public partial class Form1 : Form
        {
            const int ROWS = 5;
            const int COLS = 10;
      
            public Form1()
            {
                InitializeComponent();
    
                new PictureText(10, this);
    
     
            }
        }
        public class PictureText : Panel
        {
            const int WIDTH = 50;
            const int HEIGHT = 50;
            const int SPACE = 5;
            const int BORDER = 20;
    
            public TextBox textbox { get; set; }
            public PictureBox picturebox { get; set; }
            public int row = 0;
            public static List<PictureText> pictureText = null;
    
            public PictureText() { }
            public PictureText(int rows, Form1 form1) 
            {
                this.Left = BORDER;
                this.Top = BORDER ;
                this.Width = 2 * (Width + BORDER);
                this.Height = rows * (HEIGHT + BORDER);
                this.BackColor = Color.Blue;
                form1.Controls.Add(this);
                pictureText = new List<PictureText>();
    
                for(int row = 0; row < rows; row++)
                {
                    PictureText newRow = new PictureText();
                    this.Controls.Add(newRow);
                    pictureText.Add(newRow);
    
                    newRow.textbox =  new TextBox();
                    newRow.textbox.Text = "Hello";
                    newRow.picturebox = new PictureBox();
                    newRow.picturebox.BackColor = Color.Green;
                    newRow.picturebox.Text = "Goodbye";
                    this.Controls.Add(newRow.textbox);
                    this.Controls.Add(newRow.picturebox);
    
    
                    newRow.textbox.BringToFront();
                    newRow.textbox.Height = HEIGHT;
                    newRow.textbox.Width = WIDTH;
                    newRow.textbox.Top = row * (HEIGHT + SPACE) + BORDER;
                    newRow.textbox.Left = BORDER;
    
    
                    newRow.picturebox.BringToFront();
                    newRow.picturebox.Height = HEIGHT;
                    newRow.picturebox.Width = WIDTH;
                    newRow.picturebox.Top = row * (HEIGHT + SPACE) + BORDER;
                    newRow.picturebox.Left = 2 * (BORDER + Width);
                }
            }
        }
     
    }
    

    【讨论】:

    • 这如何回答 OP 的问题?您正在使用列和东西,OP 没有。 Onclick 你会显示一个消息框,OP 不想要这种行为。显然 OP 可以使用此代码在他自己的项目中四处游荡,但您至少可以详细说明您的代码 sn-p。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2013-11-11
    相关资源
    最近更新 更多