Hi,

I am new to C# and Forms/GUIs.

Basically I am trying to pass a value in a textBox to my programs program.cs file where I can apply my programs logic, but I'm stuck with how to do this.

To get started all I am making is a form which takes two variables, and I want to add those variables together and display the sum in a third text box.

I am stuck though with getting the value entered by the user into my program.cs file where I can use it/add the two values together.

My skeleton app is as follows:

The GUI:
SCREEN SHOT


Form1.cs :

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 MyApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //get the value from the textBox1 and put it into the variable a in the class/object Program.a
            Application.Form1.a = this.textBox1.Text;//???????
        }
    }
}


Program.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace MyApp
{
    public class Program
    {
        public string a = "";
        public string b = "";

        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

On line 22 of Form1.cs I am trying to pass the value in the textbox to the variable 'a', which is part of the class Program. This is not working though, the compiler reports:

error CS0117: 'MyApp.Program' does not contain a definition for 'Form1'

Been stuck for ages trying to figure this out now. I'm using Visual Studio 2008 on Windows & 64 bit.

Could anyone tell me what I am doing wrong? I just want to pass the values entered by the user over to the file Program.cs where I can apply my programs logic, but despite trying to figure it out, I'm stuck.

Normally you keep form logic in the form, so you'd do your math in the form class not in the program class.

That said, the variables a and b are instance variables. This means that they are only avialable if you create an instance of that object. The Program class doesn't normally have an instance created as the Main method is static, and doesn't require one. You can't access a and b because they don't exist.

Also, you are trying to access them through the form class (Application.Form1) when they are not part of the Form1 class.

Learn here wath an application Run method does: http://www.daniweb.com/software-development/csharp/code/217165
I think the word program confuses you and you believe that it is there where all the action is taking place. In fact a lot is happening there, but consider it a black box.
Concentrate on your form code. In your case this is the Click event handler of Button1, I assume it is your "Go" button.
Your task here should be to get the Text out of two textboxes, convert to some numeric value, add the values, convert the sum to a string, and assign this string to the text property of the third textbox.
So alot of work to do! Success! :)

Edited 5 Years Ago by ddanbe: n/a

Thank you to everyone who posted, after reading your posts, reading some text books on GUI development, re-reading your comments confirms I 'get it' now. :)

I've used C++ before, and I'm inexperienced with GUI designing, so I think I have to think in a much more OO way from now one. The partial classes seem interesting, as do delegates which I've seen some about.

Thanks for the help.

This question has already been answered. Start a new discussion instead.