C
C#3y ago
Kartal

C# Help on accessing a textbox's text from another form and panel

in case if you didnt get point, there's an email textbox and i want to save the email adress and use it on another form in different panel but it doesnt save the email at the variable that i created for the email
12 Replies
Anu6is
Anu6is3y ago
assuming you are using winforms based on "textbox" and you have 2 forms...Form A where the text is entered, Form B where the text is to be used. you can pass the string from formA to formB via the constructor of formB
Kartal
KartalOP3y ago
it automatically deletes the data that i saved on the variable when i pass to another form and couldnt find a solution yet already doin' this
Anu6is
Anu6is3y ago
show your code then
Kartal
KartalOP3y ago
sure give me a sec please class uyeGiris { public Boolean x = false; public string kayitTut { get; set; } public void girisYap(string Eposta, string kullaniciSifre) { destekpaneli dp = new destekpaneli(); frmWebPanel web = new frmWebPanel(); string conn = "Data Source=NICHT;Initial Catalog=otomasyon;Integrated Security=True"; SqlConnection connection = new SqlConnection(conn); string stri = "SELECT * FROM kullGiris WHERE Eposta=@eposta AND Sifre=@sifre"; SqlCommand command = new SqlCommand(stri, connection); command.Parameters.AddWithValue("@eposta", Eposta); command.Parameters.AddWithValue("@sifre", kullaniciSifre); connection.Open(); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) { kayitTut = Eposta; // Kullanıcı adı ve şifre doğru MessageBox.Show("Giriş başarılı!"); web.Show(); x = true; } else { // Kullanıcı adı veya şifre yanlış MessageBox.Show("Kullanıcı adı veya şifre yanlış!"); } reader.Close(); connection.Close(); } } ---------------------------------------------------------------
Anu6is
Anu6is3y ago
you can pass the string from formA to formB via the constructor of formB
where do you do this ^
Kartal
KartalOP3y ago
well actually i deleted the codes after it didnt work, may you tell me how to use the constructor like how it should be like
Anu6is
Anu6is3y ago
where do you instantiate and show the second form?
Kartal
KartalOP3y ago
that's where i should set and save the variable i guess
sabri abi
sabri abi3y ago
actually he just wanna use kayitTut variable on destekpaneli's any textbox
Kartal
KartalOP3y ago
yes i gotta use the kayitTut's value on another textbox and the textbox located in different panel and form
Anu6is
Anu6is3y ago
ok, i'm still not sure what i'm looking for in your code
private void button4_Click(object sender, EventArgs e)
{
frmMain geridon = new frmMain();
geridon.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
frmMain geridon = new frmMain();
geridon.Show();
this.Hide();
}
but taking the above method as an example.... if you are moving from frmUyeGiris to frmMain you can pass data to frmMain like 1️⃣
private void button4_Click(object sender, EventArgs e)
{
frmMain geridon = new frmMain("some value there"); //pass data via constructor
geridon.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
frmMain geridon = new frmMain("some value there"); //pass data via constructor
geridon.Show();
this.Hide();
}
that would require you to have a constructor in frmMain that looks like
private string textToUse;

public frmMain(string text) //update constructor to accept data
{
InitializeComponent();
textToUse = text; //store passed in data in field
}
private string textToUse;

public frmMain(string text) //update constructor to accept data
{
InitializeComponent();
textToUse = text; //store passed in data in field
}
or... if you don't want to change the constructor, you can do 2️⃣
//this is in frmMain
public string TextToUse { get; set; } //add property to form

public frmMain()
{
InitializeComponent();
}
//this is in frmMain
public string TextToUse { get; set; } //add property to form

public frmMain()
{
InitializeComponent();
}
and to pass the data you'd do
private void button4_Click(object sender, EventArgs e)
{
frmMain geridon = new frmMain();
geridon.TextToUse = "some text here"; //set value of frmMain property
geridon.Show();
this.Hide();
}
private void button4_Click(object sender, EventArgs e)
{
frmMain geridon = new frmMain();
geridon.TextToUse = "some text here"; //set value of frmMain property
geridon.Show();
this.Hide();
}

Did you find this page helpful?