This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 6 years ago.
I have two issues with a method I am calling.
I have frmForm1 & frmForm2.
frmForm1 contains a method as below...
public frmForm1()
{
InitializeComponent();
}
//This method receives the prog name and WOtype names from frmForm2
int progID;
string programName;
public void GetIDandValue(string valName, int ID, string addWOValue)
{
if (valName == "progName")
{
progID = ID;
programName = addWOValue;
}
}
private void button1_Click(object sender, EventArgs e)
{
frmForm2 loadfrmForm2 = new frmForm2();
loadfrmForm2.Show();
}
Then from frmForm2 (which is opened from a btn click on frmForm1), I am trying to send values back to the method on frmForm1 so they can be used.
private void button1_Click(object sender, EventArgs e)
{
selectedValueID = Int32.Parse(comboBox1.ValueMember);
selectedValueName = comboBox1.DisplayMember;
string valToSend = "progName";
frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
this.Hide();
}
And finally, here's how combobox1 is being initialised...
comboBox1.DisplayMember = dsAddWO.Tables[0].Columns[1].ToString();
comboBox1.ValueMember = dsAddWO.Tables[0].Columns[0].ToString();
comboBox1.DataSource = dsAddWO.Tables[0];
comboBox1.Enabled = true;
Problem 1) combobox1 ValueMember and Displaymember are returning the column headers as values when i try to populate the variables (although the correct data is actually displaying in the combobox on the form).
Problem 2) I cant seem to call the GetIDandValue method from frmForm2, intellisense just doesnt see it.
No doubt im doing something incredibly stupid. Can anyone enlighten me?
You can pass the progID and programName to the constructor of frmForm2
Dose those codes are coded on form2 ?
private void button1_Click(object sender, EventArgs e)
{
selectedValueID = Int32.Parse(comboBox1.ValueMember);
selectedValueName = comboBox1.DisplayMember;
string valToSend = "progName";
frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
this.Hide();
}
If so, u'd better get a structor of from1, it seems u don't. like u did with form2: frmForm2 loadfrmForm2 = new frmForm2(); and then call to the method frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName); Totally Like This From1 frmForm1 =new From1();frmForm1.GetIDandValue(valToSend, selectedValueID, selectedValueName);
Related
This question already has answers here:
Passing Values from one Form to another Form in a Button click
(4 answers)
Closed 2 years ago.
Okay, I saw a couple of answers to similar questions but nothing was quite right, so I thought I would write down my own problem. I have a couple of Forms that are supposed to be connected.
private void btnAdd_Click(object sender, EventArgs e)
{
AuthorForm authorForm = new AuthorForm();
authorForm.Show();
}
This is the code from the Form2 that opens once I want to create a new list of Authors. With that, the third Form "AuthorForm" pops up and that's where I want to enter a name to add to a listBox that is in Form2. I don't know how to pass the string from the TextBox to the other form.
I'm sorry if this doesn't make much sense. I'm pretty bad at trying to explain what I need but if you have questions, I'll try to explain better.
Hi and welcome to StackOverflow. There are several ways to achieve this but I believe the easiest would be to use a property in your AuthorForm as follows:
// Inside AuthorForm create the following...
public string AuthorName { get; private set; }
Having done that, you can set this property either in text changed, or when the user clicks to commit the entered text, you then read this property from the Form2.
// This goes inside the text changed off your name text box.
this.AuthorName = AuthorTextBox.Text; // Assuming the name of the textox is AuthorTextBox
Inside Form2 you call read the property as follows.
private void btnAdd_Click(object sender, EventArgs e)
{
AuthorForm authorForm = new AuthorForm();
authorForm.ShowDialog();
// Read the entered text from the property
var userInput = authorForm.AuthorName;
}
You can proceed to make use of the text the user entered after this.
If i'm not mistaken you want to change Form2 instance after authorForm.Show(); statment
You can initial public variable for example name in Form2 and then send all the form2 instance to AuthorForm with constructor (use this keyword) , now you can change variable from form2 in authorForm
see :
AuthorForm authorForm = new AuthorForm();
authorForm.Show(this);
AuthorForm :
Form2 form2;
public AuthorForm(Form2 form2)
{
this.form2 = form2;
}
public void DoSomething()
{
form2.name = "test";
}
If I got it right, you want to get values from from AuthorForm.
You can do that by adding a property to the AuthorForm, like that:
public string AuthorName { get; set; }
And use authorForm.ShowDialog() so all the opened forms will freeze until you use the Close() method in the form you opened as a dialog. And then you can continue the form.
For example:
AuthorForm.cs:
public void BtnClose_Click(object sender, EventArgs e)
{
AuthorName = "rom_totach";
Close();
}
MainForm.cs:
public void BtnOpenAuthorForm(object sender, EventArgs e)
{
AuthorForm form = new AuthorForm();
form.ShowDialog();
MessageBox.Show(form.AuthorName);
}
I hope I helped!
This question already has answers here:
Convert a list to a string in C#
(14 answers)
Closed 4 years ago.
I've referred to this link (thread 1) but I get an error. For my example, I only have one list in one class. The class name is Savestate. I have 2 forms.
Form1 contain a textbox where the string that I saved inside will be transferred to the list when I press button1. Button1 will also open
Form2 where there is a label that should reflect the string in the textbox. However, the label will reflect systems.collection...
Below is my code.
Savestate: class name
public static List<string> number = new List<string>();
Form1
private void button1_click(object sender, System.EventArgs e)
{
Savestate.number.Add(textbox1.Text);
Formscollection.Form1.Hide(); //Form 1 and Form 2 saved in another class called formscollection
Formscollection.Form2.Show();
}
Form2 (Show the systems.collections..)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = Savestate. number.ToString();
}
I tried another code based on other forums but I received an error
Form2 (got error: cannot implicitly convert type void to string)
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text = Console.WriteLine(item)
}
}
Hope to get help. Thanks.
You should display items of the list.
private void Form2_VisibleChanged(object sender, EventArgs e)
{
label1.Text = string.Join(", ", Savestate.number);
}
Or approach you already tried, but remove Console.WriteLine
private void Form2_VisibleChanged(object sender, EventArgs e)
{
foreach (string item in Savestate.number)
{
label1.Text += item;
}
}
you're trying to get a vlaue of the list. you can only get the last value of the LIST. and in the second code example you did wrong. Because Console.WriteLine doesnt work in win forms. Try to use this code:
foreach (string item in Savestate.number)
{
label1.Text += item;
}
I've just about got this but I'm not doing something right. I'm trying to pass a value from form1 to form2. On form2 I've got a property set up allowing access to one of it's text boxes. On form1 I've got it set to open an instance of form2 and pass a value from an object in a listbox to form2's text box. It seems like I've got things set up almost right because I tested it by posting the object value in a messagebox.show and it displayed the different object values just how I planned. For some reason though when I actually run it form2 will open but it will not set the value I passed to the textbox in the form, it's just a blank form. I've got no errors but I'm thinking it has something to do with the data not being passed directly to my new instance of form2. I hope I explained it well enough. Any help is appreciated.
form 1
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
editProperties.ShowDialog();
Employee person = (Employee)lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
editProperties.TextFirstName = person.EmployeeFirstName;
}
form 2
public string TextFirstName
{
get { return txtFirstName.Text; }
set { txtFirstName.Text = value; }
}
You have to set the textbox before you show the dialog.
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
Employee person = (Employee)lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
editProperties.TextFirstName = person.EmployeeFirstName;
editProperties.ShowDialog();
}
private void propertiesToolStripMenuItem_Click(object sender, EventArgs e)
{
frmProperties editProperties = new frmProperties();
editProperties.ShowDialog();
Employee person = new Employee ();
person.EmployeeFirstName = lstBoxEmployees.Items[lstBoxEmployees.SelectedIndex];
editProperties.TextFirstName = person.EmployeeFirstName;
}
What im trying to do:
Search products by BarCode on Form1 form;
if it cannot be found:
1. Open Inventory form
2. Search product by name or description
3. On ListView click on the found product copy its BarCode and paste it to the Form1 barcode textbox.
All that is done corectly. The problem is that every time I add product from Inventory form a new Form1 is opened.
The values are not processed in the same Form1, so assume I sell 4 products:
2 of them are added through Form1 barcode search
2 of them are added through Inventory search form
In the end I get 3 opened Form1 forms, one with 2 products and two forms with single product (added through Inventory form). I need them to be all in one.
Thanks
//-------------------------Form1--------------------------------------------
private void btnInventory_Click(object sender, EventArgs e)
{
Inventory Inventory = new Inventory();
Inventory.Show();
}
private string _theValue;
public string TheValue
{
get
{
return _theValue;
}
set
{
_theValue = value;
txtItems.Text = value;
}
}
//-----------------------------Inventory---------------------------------
private void ShowForm1()
{
string value = label9.Text;
Form1 newForm = new Form1();
newForm.TheValue = value;
this.Close();
newForm.ShowDialog();
}
private void lvList_Click(object sender, EventArgs e)
{
label9.Text = lvList.SelectedItems[0].SubItems[0].Text;
this.ShowForm1();
}
Im sorry for the delay, i had to wait 8h before posting again
Thanks for ur reply.
I just tried that
Form1
private void btnInventory_Click(object sender, EventArgs e)
{
Inventory _inv = new Inventory();
if (DialogResult.OK == _inv.ShowDialog())
{
txtItems.Text = _inv.fugi;
}
}
and in Inventory Form
private string test;
public string fugi
{
get { return test; }
set { test = label9.Text; }
}
private void lvList_Click(object sender, EventArgs e)
{
label9.Text = lvList.SelectedItems[0].SubItems[0].Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
txtItems.Text does not get the value of test from inventory form
Its opening a new dialog because you tell it to in ShowForm1, Personally I would change your btnInventory click as follows
private void btnInventory_Click(object sender, EventArgs e)
{
Inventory _inv = new Inventory();
if(DialogResult.OK == Inventory.ShowDialog())
{
valueIWantToSet = _inv.Accessor;
}
}
Accessor you will need to make yourself similar to
public TypeOfVar Accessor
{
get{return m_privateVariableThatIWillMakeAndSetToMyBarcode;}
}
Edit:
Once you have gotten the value of your barcode you need to set DialogResult as Follows
this.DialogResult = DialogResult.OK;
and then set the variable you wish to access to the barcode before closing your form
Edit2:
Your ShowForm1 will end up similar to this (May want to rename this method!)
{
this.DialogResult = DialogResult.OK;
m_myVar = SelectedItem..;
this.Close;
}
UPDATE ANSWER
You are still having problems as you haven't used the set property correctly, your get is fine as it is. There is a keyword in c# called value that should be used for setters. this value will take the value of whatever is on the right hand side of an = sign.. you can think of it like this...
fugi = label9.Text
In the above line, fugi is using your properties getter in order to get the value that needs to be set to label9.Text. The = sign says that you intend to use the setter for this property and set the value of value to label9.Text.
Properties with a getter and setter are used so you do not have to provide access to the underlying variable to somewhere you would not like to and then can have the option to just set or get this variable as needed.
This means that your problem persists have you have yet to set the value of test, it is still the default string value.
So you have a couple of ways to solve your problem.
The first way is just to provide a getter for label9.Text and remove the need for your private variable.
public string Fugi //Properties should really start with capital letter
{
get{return label9.Text;}
}
The second is to set the value of test before you call your getter in btnInventoryClick and remove the setter method
private void lvList_Click(object sender, EventArgs e)
{
test = label9.Text;
and the third ist to set test as shown in method 2 but also change the set method of Fugi to the following to allow this test variable to be set elsewhere.
set{text = value;}
I want to get some data to fill a listview control, but this data it's determined in other form. This is what I code in form1 (Nuevo_Credito):
private void combo_cliente_SelectionChangeCommitted(object sender, EventArgs e)
{
Credito_Grupo ventana = new Credito_Grupo(combo_cliente.SelectedItem);
ventana.ShowDialog();
}
public void AgregaIntegrantes(string id, string nombre, string monto)
{
ListViewItem elem = new ListViewItem(id);
elem.SubItems.Add(nombre);
elem.SubItems.Add(monto);
listView_integrantes.Items.Add(elem);
}
I'm invoking form2 (Credito_grupo) as show dialog window, then I want to retrieve some values and pass them to Form1 using the public method "AgregaIntegrantes". So in form2 I did the following:
public Credito_Grupo(dynamic item)
{
this.id = item.IDCliente;
this.nombre = item.NomComp;
InitializeComponent();
}
private void Credito_Grupo_Load(object sender, EventArgs e)
{
text_nombre.Text = this.nombre;
}
private void button_AgregaCliente_Click(object sender, EventArgs e)
{
Nuevo_Credito obj = new Nuevo_Credito();
obj.AgregaIntegrantes(id.ToString(), nombre, text_monto.Text);
this.Close();
}
When the event button_AgregaCliente_click is triggered I need to add the data to listview in form1 using the method described above, but none data is added. I found a solution using delegates here 3077677, is there an approach using objects?
You have an error in button_AgregaCliente_Click method (the last one in the listing). You create a new Nuevo_Credito form there, and pass the data to listview. It looks OK. But this newly created Nuevo_Credito form does exist only in the local variable, so then you throw it away without displaying it when button_AgregaCliente_Click finishes.
I think you need to delete this line: Nuevo_Credito obj = new Nuevo_Credito();
You need to get your real Nuevo_Credito form, not create a new one here.
You can send this from your Nuevo_Credito to the constructor of the Credito_Grupo form. Then you can use it to call back to the original Nuevo_Credito. This approach is based only on objects, and not delegates. As you wanted. :-)