3

I would like to add a button with value to my tab control during runtime. A lot of tutorial show how it's done while creating connecting with database. Is there any way that it can be done without connecting to database?

enter image description here

After I input data into both textbox and clicked save, new button should appear on tab control on another form.

4
  • 5
    Show us your code. Adding a button to something on its own has absolutely nothing to do with connecting to a database, so yes, it is possible to do that. Commented May 11, 2016 at 14:18
  • A key part of this question is what framework you are using: Are you trying to add a button to a web page? To a Windows application (WPF)? Commented May 11, 2016 at 14:22
  • @JamieF i'm using window forms application Commented May 11, 2016 at 14:28
  • @Glubus I have edited my post. Commented May 11, 2016 at 14:28

3 Answers 3

3

In you save button put :

 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); } 

And also you can handel the click of new button created :

public void Button_Click(object sender, EventArgs e) { Button button = (Button)sender ; MessageBox.Show("Button is pressed "+button .Text ); } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, your code are working to display new button on tab control but the save button don't get the data from the textbox.
You can change newButton.Text= "your text"; to newButton.Text= textbox1.text";
If the answer was helpful for you please mark it ,thanks
Yes, it helped. Now I'm trying to adjust the position on the tab control when new button is appeared.
Ok thanks ,i have edit my answer to show you how change position ,good luck
|
0

I propose such decision

Main form:

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 ); } } } 

Second form:

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(); } } } 

Comments

-2
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)); } } } 

Make dynamic

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); } } } } 

1 Comment

How does this answer OP's question? You're using columns and stuff, OP doesnt. Onclick you show a messagebox, OP does not want this behavior. Clearly OP could use this code to dick around in his own project, but you could at least elaborate on your code snippet.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.