1.11M Members

Passing values with textbox

 
0
 

I have a small problem passing values between classes while a textbox is used.

I can reach the public variable (field) for the textbox that belongs to the Form class from my other class, but no values in textbox1.text can be passed from the Form class to my other class. Thought is was some datatype issue first, but no values at all, completely empty.

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 myNameSpace
{
    public partial class myForm : Form
    {

        public string myFirstValue;

        public myForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myOtherClass getOther = new myOtherClass();
            getOther.SecondText();

            FirstText();
            label1.Text = Convert.ToString(myFirstValue) + " " + getOther.mySecondValue.ToString();
        }

        public void FirstText()
        {
            myFirstValue = textBox1.Text;
        }
    }

    class myOtherClass
    {
        public string mySecondValue;
        string FirstValue;

        public void SecondText()
        {
            myForm getForm = new myForm();                              // 1. Instance
            getForm.FirstText();                                        // 2. Method           
            FirstValue = getForm.myFirstValue.ToString();               // 3. Field

            mySecondValue = FirstValue;                                 // The value from textbox1
        }
    
    }
}
 
0
 

If you looked in the csharp code snippets you would have found this :
http://www.daniweb.com/code/snippet1008.html
Which probably solves your problem. Succes!

 
0
 

Hi, thanks

Is it possible to use the set get without creating a new textbox in the second class. The second class is not a form so I like to pass just the value of the textbox. My code below doesnt work but I like to pass the value from the public string myFirstValue to myOtherClass. It is no problem to pass a static values from A to B, but not when it comes from a textbox or some other form input.

The code is for my own learningbydoing.

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 myNameSpace
{
    public partial class myForm : Form
    {

        public string myFirstValue;

        public myForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myOtherClass getOther = new myOtherClass();
            getOther.SecondText();

            this.FirstText();

            label1.Text = Convert.ToString(myFirstValue) + " " + getOther.mySecondValue.ToString();
        }

        public void FirstText()
        {
            myFirstValue = textBox1.Text;
        }
    }

    class myOtherClass
    {
        public string mySecondValue;
        string FirstValue;

        public string getFirstValue
        {

            get
            {
                return this.FirstValue;
            }
            set
            {
                this.FirstValue = value;
            
            }
        
        }

        public void SecondText()
        {
            
            //myForm getForm = new myForm();                              // 1. Instance
            //getForm.FirstText();                                        // 2. Method           
            //FirstValue = getForm.myFirstValue.ToString();               // 3. Field

            mySecondValue = this.FirstValue;                              // The value from textbox1
        }
    
    }
}
 
0
 

Pieuw...
It would be too difficult to try and explain your code line by line ofwhat is wrong and right.
So changed your code a bit, I think this is the way you +- want it I hope.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public string myFirstValue = "String value in the Form class";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myOtherClass getOther = new myOtherClass();

            //set string value with the text of the textbox of the Form1 class
            getOther.StringValue = this.textBox1.Text;
            //this.FirstText();

            label1.Text = myFirstValue + " " + getOther.StringValue;
        }

        //public void FirstText()
        //{
        //    myFirstValue = textBox1.Text;
        //}
    }

    class myOtherClass
    {
        //public string mySecondValue;changed to
        private string myStringValue;

        //string FirstValue;

        public string StringValue
        {
            get
            {
                return this.myStringValue;
            }
            set
            {
                this.myStringValue = value;
            }
        }

        //public void SecondText()
        //{
        //    //myForm getForm = new myForm();                              // 1. Instance
        //    //getForm.FirstText();                                        // 2. Method           
        //    //FirstValue = getForm.myFirstValue.ToString();               // 3. Field

        //    mySecondValue = this.FirstValue;                              // The value from textbox1
        //}
    }
}

Hope the code explains itself.

 
0
 

Hi there ! Thanks.

I am still a bit confused about the values. It seem not to be possible to reach the values in my other class as long they come from the textbox in Form class. It works if I take away the textbox and give the public variable myFirstValue a static string value.

Lets say I like to perform a calculation of the value passed by the textbox, and the calculation need to done in the other class, and then send back the result to the Form class to be viewed in the label there.

Form class:
myFirstValue = 2

The other class:
mySecondValue = 2

namespace myNameSpace
{
    public partial class myForm : Form
    {
        public string myFirstValue;

        public myForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myOtherClass getOther = new myOtherClass();

            this.myFirstValue = textBox1.Text;

            label1.Text = getOther.SecondValue();
        }

    }


    class myOtherClass
    {
        myForm getForm = new myForm();
        
        private string mySecondValue;
        private string myFirstValue;

        public string FirstValue
        {
            get
            {
                return getForm.myFirstValue;
            }
            set
            {
                value = getForm.myFirstValue;
                this.myFirstValue = value;
            }
        }

        public string SecondValue 
        { 
            get 
            { 
                return this.mySecondValue; 
            } 
            set 
            {
                value = "2";
                this.mySecondValue = value + this.myFirstValue; 
            } 
        }
    }
}
 
0
 

Correct me if I am wrong.
You have a Form with a textbox a button and a label.
When you click the button you perform a calculation with a value used from the textbox in another class and show the result in the Form. Right?

 
0
 

Yes that is correct :-)

There is some deeply hidden secret how that can done...
If I pass static values from A to B, I can do whatever I like with the values.

I am trying to avoid making instances of form elements in my other class, only calculations and other things, and then pass just the results back to the form class where it can be presented in labels or something else.

This is really a major headache, and I have struggled for weeks to understand this piece.

 
0
 

Perhaps checking out this might help :

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //pass the value of this form textbox to another class
            myOtherClass getOther = new myOtherClass(int.Parse(this.textBox1.Text));
            //read the result of a calculation back in
            label1.Text = "Square of textbox value is: " + getOther.Square.ToString();
        }
    }

    class myOtherClass
    {
        //private field
        private int myIntValue;
        //contructor
        public myOtherClass(int i)
        {
            myIntValue = i * i;//perform a calculation
        }
        //public property
        public int Square
        {
            get
            {
                return this.myIntValue;
            }
            set
            {
                this.myIntValue = value;
            }
        }
    }
}

No error checking for simplicity...

 
0
 

Outstanding as usual ddanbe :cool:

I need som time to analyze what the code is doing, but it is working the way I needed.

What is so special with the form elements, and why is so hard to populate a public variable with a textbox value as I can do with a hardcoded value.


//Torbjorn

 
1
 

Nothing special about form elements.
A textbox is just what it says : a textbox.
It is up to you to transform this "userinput" to whatever you want it to be. An int a double an emailaddress a name etc.
As another example look at this :
http://www.daniweb.com/code/snippet1094.html
Happy analising and computing.

 
0
 

Thanks

A lot of good help

Question Answered as of 5 Years Ago by ddanbe
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article