hello there... need some help here, is it possible to get the value of a textbox from a form.cs, and use its value to a new class? if yes, how can i do it???, see, i have a textbox from a form.cs, and then i created a new class, in the new class i've created, i need to use the value of the textbox from the form.cs, but im having error " The name 'txtbox.Text' does not exist in the current context" hope someone could help me on this... thank you very very much...

could you provide some code so I can see what is happening? Most likely you are just putting it in the wrong spot but I need to see where you put it.

>how to use the value of a textbox from form.cs to a new class

You can pass textbox value to an instance of new class using - Constructor, public properties or method.

Add constructor to your class,

public class MyNewClass {

    public MyNewClass {}
    public MyNewClass(string val) { ... }
}
MyNewClass a=new MyNewClass(textbox1.text);

If you use the search optiion of this site (IMHO it is used too less, but I agree it can take some time to find something you really need...)
But the searcher often gets rewarded, Among others I found this in the C# snippets section: http://www.daniweb.com/code/snippet217193.html

I agree with adatapost suggestion. Always try to pass the value of the textbox from form.cs to your new class. Do not try to access the value directly (even though you can do that). It will eventually breaks the concept of encapsulation. Try to pass your variables through method call and make sure the method achieve what you want.

Lastly, posting some codes here sure helps.

thank you guys for your suggestions and id been studying it, but im really having a hard time understanding it, here is my code.

form 1

private void btnConnect_Click(object sender, EventArgs e)
        {

            DataBase db = new DataBase();
            db.MyDataBase();  
         }

Database Class

namespace MyFirstSystem
{
    class DataBase
    {
        OleDbConnection MyCon1;
        OleDbCommand MyCmd1;
        OleDbDataAdapter MyDataAdapter1;

        DataTable MyDataTable;

        public void MyDataBase()
        {
            bool MyCon1_suc = false;
            {
                if (MyCon1 != null && MyCon1.State == ConnectionState.Open)
                {
                    MyCon1.Close();
                    MyCmd1.Dispose();
                }
                try
                {
                    //connecting to database
                    MyCon1 = new OleDbConnection();
                    MyCon1.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\ISH\" + txtPath.Text;
                    MyCon1.Open();
                    MyCmd1 = MyCon1.CreateCommand();
                    MessageBox.Show("Connected to " + txtPath.Text, "Dbase connection!!!");
                    MyCon1_suc = true;

                    string commandString = "Select * from Replicate";
                    MyDataAdapter1 = new OleDbDataAdapter(commandString, MyCon1);

                    DataSet ds = new DataSet();
                    MyDataAdapter1.Fill(ds, "Replicate");
                    MyDataTable = ds.Tables["Replicate"];

                    listBox1.DataSource = ds.Tables["Replicate"];
                    listBox1.DisplayMember = "Project";
                    listBox1.ValueMember = "Project";
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}

the reason i created a class Database, is that whenever i'm going to open a database in a different form i'm just going to call the class, and not to code it again in the neew form im going to create. with my code i'm having an error " The name 'txtbox.Text' does not exist in the current context" , please help me again and thank you very very much for the time and effort.

Add a constructor to your class:

class DataBase
{
     OleDbConnection MyCon1;
     OleDbCommand MyCmd1;
     OleDbDataAdapter MyDataAdapter1;
     DataTable MyDataTable;

     System.Windows.Forms.TextBox textBox;

     public DataBase(System.Windows.Forms.TextBox textBox)
     {
              this.textBox = textBox;
      }

      //rest of your class
}

(obviously a using directive will eliminate the System.Windows.Forms, but it is not on there by default for a new class)

Then, for a textbox "tb1" in the main form:

private void btnConnect_Click(object sender, EventArgs e)
{
    DataBase db = new DataBase(tb1);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.