Passing an object through different window classes in Windows Forms
-
In a Windows Forms application, I have a class Students and then I create a new object student inside the class Form1 which represents the main window of my application. So far so good, but I need a second window which will show the user some of the student's data. Therefore I create a second class derived from Form, let's say StudentsData : Form and then I let a new object of that class be created inside the Form1 class, and again through ShowDialog() I let that new Window be visible. What I do not succeed is to pass the student's data into that window. I think the real question should be, how do I make an object persist through different other classes/objects? Any help would be appreciated.
-
In a Windows Forms application, I have a class Students and then I create a new object student inside the class Form1 which represents the main window of my application. So far so good, but I need a second window which will show the user some of the student's data. Therefore I create a second class derived from Form, let's say StudentsData : Form and then I let a new object of that class be created inside the Form1 class, and again through ShowDialog() I let that new Window be visible. What I do not succeed is to pass the student's data into that window. I think the real question should be, how do I make an object persist through different other classes/objects? Any help would be appreciated.
If all you are trying to do is to pass the Students class through to your second form, you simply need to make sure you have added this into your StudentsData form before you call ShowDialog. Now, you could do this by passing it in a constructor in StudentsData, or you could simply expose a property that accepts StudentsData. Depending on what you are trying to achieve with this, I would choose the property because you are probably going to want to be reading the contents of this after your dialog has closed.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
In a Windows Forms application, I have a class Students and then I create a new object student inside the class Form1 which represents the main window of my application. So far so good, but I need a second window which will show the user some of the student's data. Therefore I create a second class derived from Form, let's say StudentsData : Form and then I let a new object of that class be created inside the Form1 class, and again through ShowDialog() I let that new Window be visible. What I do not succeed is to pass the student's data into that window. I think the real question should be, how do I make an object persist through different other classes/objects? Any help would be appreciated.
There's two common approaches I use for modal dialogs. The first is to write a method in StudentsDataForm (yes I just renamed your class :) ) which takes the Student, shows the form and returns a modified version:
public Student EditStudent(Student input){
this.firstNameTextBox.Text = input.FirstName;
// ... etc to copy data into other controls
// you could also use data binding
if(DialogResult.OK == ShowDialog()){
Student result = new Student();
result.FirstName = firstNameTextBox.Text;
// etc
return result;
} else return input;
}There's also a variant of this where you update the input object in place, and return the DialogResult, which you can use if you always want the data object to be updated by the form when OK is used. The second way is to have a property in the second form:
public Student Student {
get {
Student student = new Student();
student.FirstName = firstName.Text;
// etc
return student;
}
set {
firstName.Text = value.FirstName;
// etc
}
}Then you push the data into the form, and use the data from it if the user pressed OK:
studentDataForm.Student = someStudent;
if(DialogResult.OK == studentDataForm.ShowDialog(this)){
// Do something with studentDataForm.Student to store the changed data
} -
If all you are trying to do is to pass the Students class through to your second form, you simply need to make sure you have added this into your StudentsData form before you call ShowDialog. Now, you could do this by passing it in a constructor in StudentsData, or you could simply expose a property that accepts StudentsData. Depending on what you are trying to achieve with this, I would choose the property because you are probably going to want to be reading the contents of this after your dialog has closed.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
I have a main Form public partial class Form1 : Form a Data Form public partial class Data : Form and a class Students in another file with public string Name { get; set; } Now on Form1 I create Students stud = new Students(); and then I give stud.Name = "hisname"; Then by clicking a button on Form 1 the following happens Data datawindow = new Data(); datawindow.ShowDialog(); When Data Form loads I want to get in a text box txtName.Text = stud.Name; But the object stud is not available on Data form.
-
I have a main Form public partial class Form1 : Form a Data Form public partial class Data : Form and a class Students in another file with public string Name { get; set; } Now on Form1 I create Students stud = new Students(); and then I give stud.Name = "hisname"; Then by clicking a button on Form 1 the following happens Data datawindow = new Data(); datawindow.ShowDialog(); When Data Form loads I want to get in a text box txtName.Text = stud.Name; But the object stud is not available on Data form.
In Data class:
private Students students;
public Students Students
{
get{ return students; }
set
{
students = value;
txtName.Text = students.Name;
}
}In your Button Click handler in Form1:
Data datawindow = new Data();
datawindow.Students = stud;
datawindow.ShowDialog();Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
In Data class:
private Students students;
public Students Students
{
get{ return students; }
set
{
students = value;
txtName.Text = students.Name;
}
}In your Button Click handler in Form1:
Data datawindow = new Data();
datawindow.Students = stud;
datawindow.ShowDialog();Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Thanks for your help. Unfortunately I am getting an error, and I think I have seen it before when I was trying to solve that problem, maybe because I have a class Students on a file students.cs The error is: Inconsistent accessibility: property type 'Studentstest.Students' is less accessible than property 'Studentstest.Data.Students' Where Studentstest is the name of the project containing the following classes: Form1.cs, Data.cs and Students.cs
-
Thanks for your help. Unfortunately I am getting an error, and I think I have seen it before when I was trying to solve that problem, maybe because I have a class Students on a file students.cs The error is: Inconsistent accessibility: property type 'Studentstest.Students' is less accessible than property 'Studentstest.Data.Students' Where Studentstest is the name of the project containing the following classes: Form1.cs, Data.cs and Students.cs
OK, I found this http://msdn.microsoft.com/en-us/library/532wtsbc(v=vs.90).aspx and corrected accordingly. Thanks again.
-
In a Windows Forms application, I have a class Students and then I create a new object student inside the class Form1 which represents the main window of my application. So far so good, but I need a second window which will show the user some of the student's data. Therefore I create a second class derived from Form, let's say StudentsData : Form and then I let a new object of that class be created inside the Form1 class, and again through ShowDialog() I let that new Window be visible. What I do not succeed is to pass the student's data into that window. I think the real question should be, how do I make an object persist through different other classes/objects? Any help would be appreciated.
Hello and thanks for your help. Of course it works that way, but assuming I have 10 different objects in the Main Form and I need their data in several other Forms of my programme, let's say 6 new forms, do I have to create similar objects of all those 10 in everyone of the 6 Forms? Isn't there a way to make an object available in the whole programme?