I am coming from a VB .NET background and I am a little confused with how to access another forms properties in C#. Basically I am taking one of the exercises from my VB book and trying to convert to C# to help me learn C#.
Anyhow, here is the task at hand. Form 1 is basically a list view of items that were sold for a particular day. One of the buttons on this form allows the user to open another form to enter the actual items that were sold that day. I have both the interfaces completed, have the second form loading when the user hits the "Enter sales" button on form 1.
However, the next step in my VB book starts the exectution of a do until loop. It accesses the text property of the second form and continuously loops until the text property is empty, meaning the user did not enter another item.
Example: Do Until objAddItemForm.txtItemDescription.Text=""
...process data and add to listview on form 1...
Loop
Ok, so my problem is, how do I access this text field on form 2, to check to see if it is blank, or add its text property to the listview, from form 1?
Thanks for any help,
Shawn
*Edit* - Here is the code I have so far:
Form1
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
WindowsApplication6{
public partial class frmTodaysSalesCheck : Form{
public frmTodaysSalesCheck(){
InitializeComponent();
}
private void btnEnterSales_Click(object sender, EventArgs e){
//Declares objAddItemForm as a new Form //and opens it in modal form. Modal means //only the currently activated window is //accessible for input/action. Form objAddItemForm = new frmAddItem();objAddItemForm.ShowDialog();
}
}
}
Form2
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
WindowsApplication6{
public partial class frmAddItem : Form{
public frmAddItem(){
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e){
this.Close();}
}
}
Right now the second form is just closing when the user clicks the Ok button. What I need to do is read the textbox entry on form2 and add it to the listview on form1. Any ideas how to do this?
Thanks again,
Shawn
Fred de la RocaPosted Jun 28, 2007, 4:45 PM
Something I've just learned yesterday (I'm in the same case, passing from VB to C#), is that in C# all is classes, so, the same case I've done this:
In a new codefile, create a static class, for instance "ClassGlobal", and add there a static variable, that would do the same as a public variable in a module in VB.
namespace Example
{
public static class ClassGlobal
{
public static int varNeeded;
}
}
and in the code, you can call this variable as:
ClassGlobal.varNeeded = 1;
without having to instantiate the class, because is static.
Hope to help you
ShawnPosted Jun 28, 2007, 3:02 PM
Form 1 Code
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
WindowsApplication6{
public partial class frmTodaysSalesCheck : Form{
private string txtFromForm2=""; public frmTodaysSalesCheck(){
InitializeComponent();
}
private void btnEnterSales_Click(object sender, EventArgs e){
//Declares objAddItemForm as a new Form //and opens it in modal form. Modal means //only the currently activated window is //accessible for input/action. Form objAddItemForm = new frmAddItem(this);objAddItemForm.ShowDialog();
lstTodaysSales.Items.Add(txtFromForm2);
}
public string TextFromForm2{
set { txtFromForm2 = value; }}
}
}
Form 2 Code
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;namespace
WindowsApplication6{
public partial class frmAddItem : Form{
private frmTodaysSalesCheck f1; public frmAddItem(frmTodaysSalesCheck f1){
this.f1 = f1;InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e){
f1.TextFromForm2 = txtItemDescription.Text;
this.Close();}
}
}
AlanPosted Jun 28, 2007, 2:23 PM
One possibility would be to do the following:
On Form1:
1. Create a private field:
private textFromForm2 = "";
2. Create a public write-only property:
public TextFromForm2
{
set { textFromForm2 = value };
}
3. Change this line:
Form objAddItemForm = new frmAddItem();
to this:
Form objAddItemForm = new frmAddItem(this);
4. After this line:
objAddItemForm.ShowDialog();
insert code to add textFromForm2 to your ListView.
Now on Form2:
1. Create a private field:
private Form1 f1;
2. Change the constructor to this:
public frmAddItem(Form1 f1)
{
this.f1 = f1;
InitializeComponent();
}
3. In btnOk_Click(), just before this.Close():
insert code to assign your text field to the property f1.TextFromForm2
Good luck!