How would I add a textbox?

This is a discussion on How would I add a textbox? within the C# Programming forums, part of the General Programming Boards category; I have... Code: using System; using System.Windows.Forms; class MainForm : Form { public MainForm() { Text = "First WinApp"; } ...

  1. #1
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230

    How would I add a textbox?

    I have...

    Code:
    using System;
    using System.Windows.Forms;
    class MainForm : Form
    {
    	public MainForm()
    	{
    		Text = "First WinApp";
    	}
    	
    	public static void Main(string[] args)
    	{
    		Form form1 = new MainForm();
    		TextBox text = new TextBox();
    		Application.Run(form1);
    	}
    }
    but from there, how do I had a TextBox to form1?

    thanks,
    Ken

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    The Textbox should be a private member of your class.
    Create a new Textbox in the constructor. Then set the
    size and location. Example:

    textBox1.Location = new System.Drawing.Point(104, 80);
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Yeah, I figured it out, thanks. But you forgot one thing:
    this.Controls.Add(textbox1);

    Ok, well now I have a whole lot sorted out there's one thing bugging me. Why won't this work:

    Code:
    using System;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;
    
    class MainForm : Form
    {
    	public TextBox tbox = new TextBox();
    	public MainForm()
    	{
    		TextBox tbox = new TextBox();
    		Button butt = new Button();
      	
    		tbox.Location = new Point(10,10);
    		this.Controls.Add(tbox);
    	  	
    		butt.Location=new Point(tbox.Left+tbox.Width+20,10);
    		butt.Size=new Size(150,24);
    		butt.Text="Ok";
    		butt.Click += new System.EventHandler(this.ButtClick); 
    		this.Controls.Add(butt);
      	
    		Text = "Cool Stuff";
    	}
    	protected void ButtClick(object sender, System.EventArgs e) 
    	{
    		MessageBox.Show(tbox.Text,"You typed",MessageBoxButtons.OK); 
    	}
      
    	public static void Main()
    	{
    		Application.Run(new MainForm());
    	}
    }
    Why doesn't it show up the text?

    nevermind, I figured it out.
    Last edited by -KEN-; 11-13-2001 at 03:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a multiline textbox
    By Zeokat in forum C# Programming
    Replies: 4
    Last Post: 10-24-2008, 01:14 PM
  2. inserting text in a textbox
    By Rune Hunter in forum C# Programming
    Replies: 1
    Last Post: 01-07-2006, 04:32 PM
  3. Help needed Please
    By jereland in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 04:30 AM
  4. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 09:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21