How to pass Form1 value to Form3 in C# - c#

How to pass value of form1 directly to Form3.
In Form1 i have button and a Textbox named txtUsername .
public string username = string.Empty;
private void LoginBT_Click(object sender, EventArgs e)
{
username = txtUsername.Text;
Form3 form = new Form3(username);
Form2 frm = new Form2();
frm.Show();
this.Hide
}
and in From2 i have this
private void Button_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
this.Hide();
}
and in Form3
public string Name;
public Form3(string CName)
{
InitializeComponent();
Name= Cname;
}
private void frmTicketandCottages_Load(object sender, EventArgs e)
{
MessageBox.Show(Name);
}
to display the text.
But when i display the text i only get an Empty Text
Any other method to do this ?

Why do you still need to go to form 2 if you want to directly pass the value to form3? Anyway you can do it this way...
form1:
private void LoginBT_Click(object sender, EventArgs e)
{
username = txtUsername.Text;
Form2 frm = new Form2(username);
frm.Show();
this.Hide
}
form2:
string Name;
public Form2(string CName)
{
InitializeComponent();
Name= Cname;
}
private void Button_Click(object sender, EventArgs e)
{
Form3 form = new Form3(Name);
form.Show();
this.Close();
}
form3:
string Name;
public Form3(string CName)
{
InitializeComponent();
Name= Cname;
}
private void frmTicketandCottages_Load(object sender, EventArgs e)
{
MessageBox.Show(Name);
}

it's too simple:
in Button_Click you don't pass Name
Form3 form = new Form3();
pass Username to Form2 by Declare
public string username
or in Constructor like Form3:
public string Name;
public Form2(string CName)
{
InitializeComponent();
Name= Cname;
}
and then
public string username = string.Empty;
private void LoginBT_Click(object sender, EventArgs e)
{
username = txtUsername.Text;
Form3 form = new Form3(username);
Form2 frm = new Form2(username);
frm.Show();
this.Hide
}
you must pass the Name like this:
Form3 form = new Form3(username);

Related

Access to SelectedPath in Form2

I have two form, FolderBrowserDialog in Form1 and I want to use SelectedPath in Form 2.
I don't know why but when I click on Form1 Button to show Form2 ,This Error will be appear :
Invalid URI: The URI is empty
I set Form Control Modifiers: Public and FolderBrowseDialog Seletedpath not be transferred to Form2
Form 1 :
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog MyFolderBrowse = new FolderBrowserDialog();
if(MyFolderBrowse.ShowDialog()==DialogResult.OK)
{
txtpath.Text = MyFolderBrowse.SelectedPath;
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.ShowDialog();
}
Form 2 :
private void Form2_Load(object sender, EventArgs e)
{
Form1 Frm1 = new Form1();
webBrowser1.Url = new Uri(Frm1.txtpath.Text);
}
any solution...?
Welcome to StackOverflow!
The problem is, if I understand it correctly from the code you post it, is that you create a new instance of Form2 and then in the Loadevent of the Form2 you create a new instance of Form1 and the information you need is the instance of Form1 you already have.
You already have an instance of Form1. I would do it by two one of the 2 options:
Create a public property in Form2 and assign it when creating the Form2 instance
public class Form2: Form
{
//{...}
public string SelectedPath { get; set;}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(this.SelectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2();
Frm2.SelectedPath = txtpath.Text;
Frm2.ShowDialog();
}
Create an argument in the Form2 constructor and pass the selected path from Form1
public class Form2: Form
{
//{...}
private string _selectedPath;
public Form2(string selectedPath)
{
_selectedPath = selectedPath;
}
//{...}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri(_selectedPath);
}
}
private void Showfrm2Btn_Click(object sender, EventArgs e)
{
Form2 Frm2 = new Form2(txtpath.Text);
Frm2.ShowDialog();
}

Why is the empty result turning?

Firstly, my English is bad, I'm sorry.
I want textbox (in Form2.cs) text to displaying MainForm.cs
When I apply the following codes, displaying blank message.
MainForm.cs
private void btnFilitre_ItemClick(object sender,DevExpress.XtraBars.ItemClickEventArgs e)
{
...
Form2 f2 = new Form2();
f2.Show();
}
private void workingFunction()
{
CommClass com = new CommClass();
MessageBox.Show(comm.Sorgu );
}
Form2.cs
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
CommClass comm = new CommClass();
comm.Sorgu = textBox1.Text;
f1.workingFunction();
Hide();
}
CommClass.cs
public string Sorgu { get; set; }
What is the problem?
You need to pass the argument in. In Form1 make this change:
public void workingFunction(CommClass comm)
{
MessageBox.Show(comm.Sorgu );
}
And in Form2, you will need to keep track of your Form1 reference, instead of creating a new one, and then pass in your CommClass object:
private void button1_Click(object sender, EventArgs e)
{
CommClass comm = new CommClass();
comm.Sorgu = textBox1.Text;
f1.workingFunction(comm);
Hide();
}

C# - Setting a Value in a Button then passing it into a ListView

I have two forms, form1 and form2.
In form1, there is are two buttons, button1 and button2.
In form2, there is a listview, ListView1.
button1 should hold a string value called "Vanilla".
When button2 is pressed it opens form2.
On form2, in listview1 it should show "Vanilla" in the first column.
Form1
public partial class form1 : Form
{
public static string buttonValue = "";
public form1()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
buttonValue = "Vanilla";
}
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2();
form2.Show();
this.Hide();
}
Form2
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can design the second form as bellow:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
private string _passedValue = "";
public form2(string passedValue)
{
InitializeComponent();
_passedValue = passedValue;
listView1.Items.Add(_passedValue);
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
You can pass the value stored in the first button using the bellow code.
public void button2_Click(object sender, EventArgs e)
{
form2 form2 = new form2(buttonValue);
form2.Show();
this.Hide();
}

send string to an existing form?

I understand that this thread can send strings from one form to another. But if you look at the method, it has to create a new form where the strings will then be retrieved. Is there a way to pass the string to the existing form?
For example:
public class Form2
{
...
public string MyProperty { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(this.MyProperty);
}
}
From Form1:
public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
Form2 frm2 = new Form2();
frm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}
When I click the button1, the new form in which the string are sent will be opened. What if frm2 is already opened and I want the string to be passed to that form instead of a new one?
To do what you want, you'll have to save a reference to your form and then simply check whether it's existing already:
private Form2 myForm;
// Then, in your button code:
if (myForm == null)
myForm = new Form2();
myForm.MyProperty = departmentName;
myForm.Show();
You can create a property on the Form1 class and use that as the reference to your Form2:
public class Form1 : Form
{
private Form2 mForm2 { get; set; }
public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
if (mForm2 == null)
mForm2 = new Form2();
mForm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}
}

how to pass listview row data into another existing form

i want to have 2 forms in which the first form has a button that will load up form2 in a dialog form. form2 will show a listview displaying the data of a student. now i need to extract the 1st index of the selected row. once i double click on the row, form2 would close and pass the data into a textbox in form1.
i have used the code below which closes my form1 and creates a new instance of form1 in form2.
from form2:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1 wa= new Form1();
wa.loadid(cl);
wa.Show();
this.Close();
}
from form1:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.Show();
this.Close();
}
public void loadid(String ms)
{
String newstring = ms;
studentid.Text = newstring;
}
I suggest using a Dialog, it makes it very easy:
This is Form1. You instantiate and open f2 as Dialog and then wait for its result.OK
private void Button1_Click(System.Object sender, System.EventArgs e)
{
var f2 = new Form2();
if (f2.ShowDialog() == DialogResult.OK) {
studentId.Text = f2.SelectedStudentId;
} else {
studentId.Text = "Select a Student!!!!";
}
}
This in Form2, where you have created your listview and a public property to expose:
public string SelectedStudentId { get; set; }
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
SelectedStudentId = cl;
DialogResult = DialogResult.OK; //will close this dialog (form2)
}
This should work for you
In Form1 create a public variable like this:
public partial class Form1: Form
{
//New variable
public static string StudentIDVal;
Then, change the button click on Form1 to be:
private void btnReq_Click(object sender, EventArgs e)
{
Form2 f2= new Form2();
f2.ShowDialog();
studentid.Text = StudentIDVal;
}
Then, on Form2 on the item click you can simply have:
private void listView1_DoubleClick(object sender, EventArgs e)
{
var cl = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
Form1.StudentIDVal = cl.ToString();
this.Close();
}

Categories

Resources