Michael Quiles
Michael Quiles

Reputation: 1151

Get data from console to form c# help? very small app

I'm trying to convert a console app to a form I got the form part showing up correctly working but can't figure out how to make the program get its data from the form instead of the console any suggestions?

Console Application

namespace doomsday
{
    public class doomsday
    {


       private string firstName;

        public string GetName()
        {
            return this.firstName;
        }//end method GetName

    public doomsday()
    {
       Console.Write(" what is your first name: ");
       this.firstName = Console.ReadLine();


    }// end default constructor

    public string DisplayGreeting()
    {
        return "Hello, " + this.GetName();
    }
    static void Main()
    {
        Application.EnableVisualStyles();

        doomsday hello = new doomsday();

        Console.WriteLine(hello.DisplayGreeting());

        Console.ReadLine();

       Application.Run(new Form1());


    }

Form Application

namespace doomsday
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();


        }


    }
}

Form http://img251.imageshack.us/img251/6995/capturembq.jpg

Upvotes: 0

Views: 1050

Answers (3)

Iain
Iain

Reputation: 6452

Change private string firstName;

to

public string firstName;



namespace doomsday
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();


        }
       public void whenTextbox1LoseFocus {
            var greet = new doomsday();
            greet.firstname = textBox1.Text;
            textbox2.text = greet.DisplayGreeting()
        }

    }
}

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838216

In the Visual Studio form designer, add a button to your form. Double-click the button to add an event handler for the button's Click event. Inside the body of this new method you can access the Text property of the text box. For example:

private void button1_Click(object sender, EventArgs e)
{
    string name = textBox1.Text;
    textBox2.Text = "Hello, " + name;
}

Upvotes: 3

Yves M.
Yves M.

Reputation: 3318

I am not sure wether i get you on this, but if you want to pass the entries from the console to the form you need to have a public property on the form.

    Form1 form = new Form1();
    form.theDay = doomsday;
    Application.Run(form); 

On the Form1 just offer a Property like this:

public doomsday theDay { get; set; }

Upvotes: 1

Related Questions