Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing an object through different window classes in Windows Forms

Passing an object through different window classes in Windows Forms

Scheduled Pinned Locked Moved C#
questionwinformshelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nstk
    wrote on last edited by
    #1

    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.

    P B N 3 Replies Last reply
    0
    • N nstk

      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.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • N nstk

        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.

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #3

        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
        }

        1 Reply Last reply
        0
        • P Pete OHanlon

          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

          N Offline
          N Offline
          nstk
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • N nstk

            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.

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            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)

            N 1 Reply Last reply
            0
            • D DaveyM69

              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)

              N Offline
              N Offline
              nstk
              wrote on last edited by
              #6

              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

              N 1 Reply Last reply
              0
              • N nstk

                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

                N Offline
                N Offline
                nstk
                wrote on last edited by
                #7

                OK, I found this http://msdn.microsoft.com/en-us/library/532wtsbc(v=vs.90).aspx and corrected accordingly. Thanks again.

                1 Reply Last reply
                0
                • N nstk

                  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.

                  N Offline
                  N Offline
                  nstk
                  wrote on last edited by
                  #8

                  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?

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups