Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey all! Been a while since I have been here and this one should be fairly simple. There are many ways to do this request I find, but nothing that I understand quite simply enough to implement into my project.

Currently I am just making a simple login page that authenticates to a users database I have in MySQL. I have everything working fine but I can't get the textbox text from form1 to a variable in a class that I pulled off of Code Project here to build the database connection. Here is the project I am using for the database connection, I love how simple it is, but I want to send the username and all other database information from a different form rather than hard coding it into the class. Thanks for the advice!

Connect C# to MySQL[^]

Let me know if you need more information. I would put in some example code, but it doesn't work. Making private static strings ect, I just can't grasp passing the information along. I think a lot of the information I am finding has to do with passing information between forms and it doesn't apply here or something. Thanks again.

Edit: Here is a code snippet...

The form: with a textbox control called "txtUsername"

C#
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 Goetnet ControlPanel
{
     public partial class LoginPage : Form
     {
          private MySQL MySql;
          public string loginUsername
          {
          get {return this.txtUsername.Text;}
          set {this.txtUsername.Text = value;}
          }
     }

     public LoginPage()
     {
          InitializeComponent();

          MySql = new MySQL();
     }

     private void button1_click(object sender, EventArgs e)
     {
          //do stuff with the username text being sent to the MySQL class
     }
}


and here is the Class

C#
private Form _usernameInstance

private string username;

public MySQL()
{
Initialize();
}

private void Initialize()
{
if (this._usernameInstance == null)
{
this._usernameInstance = new Form();
this._usernameInstance.Show();
}
username = this._usernameInstance.loginUsername; //this apparently is what does not work


Hope this bring into perspective what I am not actually grasping here. Thanks!
Posted
Updated 1-Oct-15 14:06pm
v2
Comments
Wendelius 30-Sep-15 13:01pm    
I believe it would be helpful if you'd add the code you have and explain where you have the problem.
Goaty65109 30-Sep-15 13:23pm    
Here's what I have in the form:

public string username
{
get { return txtUsername.Text; }
set { txtUsername.Text = value; }
}

From what I understand that sets up the variable - username - to be accessible from sources outside the form correct?

And to call this variable I tried this in the class file:

private string uid;

uid = Form1.username;

^^^ this is what is extremely wrong. I think I am referencing the public string incorrectly.
Richard Deeming 30-Sep-15 13:38pm    
At a guess, Form1 is the name of the form's type, not a variable pointing to an instance of the form.

You'll get a compiler error similar to:
"An object reference is required for the non-static field, method, or property 'Form1.username.get'"

To access the value of the property, you'll need the instance of the form that you're trying to read from. What you've got at the moment is like asking "What is Person's name?", without specifying which person you're talking about.
ZurdoDev 30-Sep-15 13:03pm    
Getting data from forms is very simple. Please post the code you have and show where you are stuck.
phil.o 30-Sep-15 14:15pm    
Richard Deeming is right, you need an instance of the Form1 class to access its Username property.
And RyanDev is right also, we will need to see the part of your class file where you do the plumbing with the form.
Please place your code blocks in your question, not in comments. Use the improve button, and place the code between <pre> tags.

As already pointed out the confusion seems to be with referencing the instance instead of the class.

Try the following:
- In your project make sure that you have Form1 and Form2.
- Create two buttons on Form1, button1 and button2
- Add a textbox named textBox1 on Form2
- Add the following code to Form1
C#
public partial class Form1 : Form {

   // Class level field to hold the instance of Form2. The value is retained between method calls
   private Form2 _form2Instance;

   public Form1() {
      InitializeComponent();

      // Add event handlers for buttons
      this.button1.Click += button1_Click;
      this.button2.Click += button2_Click;
   }

   private void button1_Click(object sender, EventArgs e) {
      // Is an instance of Form2 already created, if not, create one
      if (this._form2Instance == null) {
         this._form2Instance = new Form2();
         this._form2Instance.Show();
      }
      // Set the text on Form2 instance using the property
      this._form2Instance.MyText = "Some text from an instance of Form1";
   }

   private void button2_Click(object sender, EventArgs e) {
      // Is an instance of Form2 already created, if it is, get the text.
      if (this._form2Instance != null) {
         System.Windows.Forms.MessageBox.Show(this._form2Instance.MyText);
      }
   }
}

- and on Form2 use the following code
C#
public partial class Form2 : Form {

   // Property for setting ang getting the text from the text box
   public string MyText {
      get {
         return this.textBox1.Text;
      }
      set {
         this.textBox1.Text = value;
      }
   }

   public Form2() {
      InitializeComponent();
   }
}


Now try running the code, place breakpoints on the methods and investigate what happens. I hope this gives you better understanding...
 
Share this answer
 
Comments
Goaty65109 1-Oct-15 19:52pm    
Ok this looks like it will do what I want, but that's with two forms. I don't seem to be able to make this happen sending textBox1.Text data to a class I have called MySQL. See my updated question for more code examples I have (using your technique, that might help get this rolling).
Wendelius 1-Oct-15 23:27pm    
As far as I can see the problem is that you instantiate a blank form. Use the form you have created.

In other words replace
private Form _usernameInstance
with
private LoginPage _usernameInstance

and replace
this._usernameInstance = new Form();
with
this._usernameInstance = new LoginPage();
I'm not sure i understand you well, but...

If you want to pass data between forms, it is strongly recommended to read this: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900