Jump to content

How to pass a value from a textbox to a class in C#

so i have been trying this for a while now. i want to pass the user input at textbox1 to a class and i have gone through some sites like stack overflow but still couldnt find any solutions.im new to C# programming and i have tried the code shown below but it hasnt worked.

   
--------------------Form1---------------------------------------------------
 private void button1_Click(object sender, EventArgs e)
        {
            Login_class l1 = new Login_class();
            l1.login();
        }

public string c;

        public string TextBox1Text
        { 

            set 
            {
                c = textBox1.Text;
            }

            get{ return textBox1.Text; }

        }

--------------------------------------------------------------------------------
  
----------------class-----------------------------------------------------------
  
  
  class Log_class
    {
        public void log()
        {
          
           Form1 f1 = new Form1();
            string x;            
            MessageBox.Show(f1.textBox1.Text);
          
        }
  
  
}
 ---------------------------------------------------------------------------------

 

Link to comment
Share on other sites

Link to post
Share on other sites

The current problem is that you're making a new instance of Form1, so there's no text in the text box in that instance.

 

Are you wanting to log the text when the button is pressed? If so, change "public void log()" to "public void log(String arg)", then call it from the button press event with "new Log_class().log(textBox1.Text);"

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, TheKDub said:

Set textBox1's "Modifiers" property to "Internal", then in the Log_class, remove "Form f1 = new Form1();", then use "MessageBox.Show(Form1.textBox1.Text);".

 

The current problem is that you're making a new instance of Form1, so there's no text in the text box in that instance.

few minutes ago i found this method:

i passed the textBox1 value from the form to the class as shown below.is it good practice?

log_class logc1 = new log_class();
logc1.login(textBox1.Text);

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Shammikit said:

few minutes ago i found this method:

i passed the textBox1 value from the form to the class as shown below.is it good practice?


log_class logc1 = new log_class();
logc1.login(textBox1.Text);

 

It's certainly an option, I'd use it, though I'm not sure if most would consider it as good practice or not.

 

I edited my post with how to make that functional, and removed the previous method I suggested. It didn't like how the textbox isn't a static object.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×