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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Passing values to another Form

Passing values to another Form

Scheduled Pinned Locked Moved C#
help
6 Posts 3 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.
  • R Offline
    R Offline
    rich_wenger
    wrote on last edited by
    #1

    I've got a second form (LogonUser) that I'm opening with a ShowDialog() method inside a button event; I would like to pass this second form the value of a boolean public variable (IsLoggedOn) I have declared in Form1. Any help would be greatly appreciated. This is how I'm opening the second from======================== LogonUser validateLogonUser = new LogonUser(); validateLogonUser.MyParentForm = this; validateLogonUser.StartPosition = FormStartPosition.CenterParent; validateLogonUser.ShowDialog();

    S A 2 Replies Last reply
    0
    • R rich_wenger

      I've got a second form (LogonUser) that I'm opening with a ShowDialog() method inside a button event; I would like to pass this second form the value of a boolean public variable (IsLoggedOn) I have declared in Form1. Any help would be greatly appreciated. This is how I'm opening the second from======================== LogonUser validateLogonUser = new LogonUser(); validateLogonUser.MyParentForm = this; validateLogonUser.StartPosition = FormStartPosition.CenterParent; validateLogonUser.ShowDialog();

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Just expose a property on the second form and set it from the first form. Like this.

      class SecondForm : Form
      {
      bool val;
      public bool IsLoggedOn
      {
      set { val = value; }
      }
      }

      class FirstForm : Form
      {
      public void SomeMethod()
      {
      SecondForm sf = new SecondForm();
      sf.IsLoggedOn = this.IsLoggedOn;
      ...
      sf.ShowDialog();
      }
      }

      Or, if you are passing "this" to the second form (like you did in your sample code), you can directly access the public variable, provided MyParentForm is typed strongly. I'd recommend the first approach though. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      R 1 Reply Last reply
      0
      • S S Senthil Kumar

        Just expose a property on the second form and set it from the first form. Like this.

        class SecondForm : Form
        {
        bool val;
        public bool IsLoggedOn
        {
        set { val = value; }
        }
        }

        class FirstForm : Form
        {
        public void SomeMethod()
        {
        SecondForm sf = new SecondForm();
        sf.IsLoggedOn = this.IsLoggedOn;
        ...
        sf.ShowDialog();
        }
        }

        Or, if you are passing "this" to the second form (like you did in your sample code), you can directly access the public variable, provided MyParentForm is typed strongly. I'd recommend the first approach though. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        R Offline
        R Offline
        rich_wenger
        wrote on last edited by
        #3

        Thanks for responding. I've been chipping away at this problem and I've had some success with the following code blocks: form1.cs===================================== LogonUser validateLogonUser = new LogonUser(); validateLogonUser.MyParentForm = this; validateLogonUser.loggedOnUser = this.validatedUser; validateLogonUser.StartPosition = FormStartPosition.CenterParent; validateLogonUser.ShowDialog(); LogonUser.cs================================= public Form1 MyParentForm; private void LogonUser_Load(object sender, EventArgs e) { this.textBoxUserName.text = this.loggedOnUser; } I don't seem to be using any reference to MyParentForm or Form1 properties to access the value passed from Form1? It works, but I don't know why.:confused:

        S 1 Reply Last reply
        0
        • R rich_wenger

          I've got a second form (LogonUser) that I'm opening with a ShowDialog() method inside a button event; I would like to pass this second form the value of a boolean public variable (IsLoggedOn) I have declared in Form1. Any help would be greatly appreciated. This is how I'm opening the second from======================== LogonUser validateLogonUser = new LogonUser(); validateLogonUser.MyParentForm = this; validateLogonUser.StartPosition = FormStartPosition.CenterParent; validateLogonUser.ShowDialog();

          A Offline
          A Offline
          Aldorado
          wrote on last edited by
          #4

          Hi. You can do it bay many ways. one of the simplest way is to write the value of boolean to the hard desk and then to read it from the second form. in the first form: private void SaveInfo() { try { FileStream file = new FileStream("info.txt",FileMode.Create); StreamWriter sw = new StreamWriter(file); sw.WriteLine(IsLoggedOn.ToString(),true); sw.Close(); } catch(System.Exception se) { MessageBox.Show(se.ToString()); } } and then you have to call this method from the method that call the other form. in the second form: private bool ReadInfo() { try { FileStream file = new FileStream("info.txt",FileMode.Open); StreamReader sr = new StreamReader(file); bool b = sr.ReadLine(); sr.Close(); } catch(System.Exception se) { MessageBox.Show(se.ToString()); } return b; } so now bool b has the value of IsLoggedOn. There are other ways but this on is simple. Thanks. Asaad Mamoun Hassan Bc in Computer Science.

          S 1 Reply Last reply
          0
          • A Aldorado

            Hi. You can do it bay many ways. one of the simplest way is to write the value of boolean to the hard desk and then to read it from the second form. in the first form: private void SaveInfo() { try { FileStream file = new FileStream("info.txt",FileMode.Create); StreamWriter sw = new StreamWriter(file); sw.WriteLine(IsLoggedOn.ToString(),true); sw.Close(); } catch(System.Exception se) { MessageBox.Show(se.ToString()); } } and then you have to call this method from the method that call the other form. in the second form: private bool ReadInfo() { try { FileStream file = new FileStream("info.txt",FileMode.Open); StreamReader sr = new StreamReader(file); bool b = sr.ReadLine(); sr.Close(); } catch(System.Exception se) { MessageBox.Show(se.ToString()); } return b; } so now bool b has the value of IsLoggedOn. There are other ways but this on is simple. Thanks. Asaad Mamoun Hassan Bc in Computer Science.

            S Offline
            S Offline
            S Senthil Kumar
            wrote on last edited by
            #5

            Whoa, don't you think it is overkill to write to the hard disk just for passing a value to an in-memory object? And what would happen if two instances of the form are launched at the same time? Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            1 Reply Last reply
            0
            • R rich_wenger

              Thanks for responding. I've been chipping away at this problem and I've had some success with the following code blocks: form1.cs===================================== LogonUser validateLogonUser = new LogonUser(); validateLogonUser.MyParentForm = this; validateLogonUser.loggedOnUser = this.validatedUser; validateLogonUser.StartPosition = FormStartPosition.CenterParent; validateLogonUser.ShowDialog(); LogonUser.cs================================= public Form1 MyParentForm; private void LogonUser_Load(object sender, EventArgs e) { this.textBoxUserName.text = this.loggedOnUser; } I don't seem to be using any reference to MyParentForm or Form1 properties to access the value passed from Form1? It works, but I don't know why.:confused:

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              It works because of this line

              rich_wenger wrote:

              validateLogonUser.loggedOnUser = this.validatedUser;

              You are setting the value of the loggedOnUser variable in LogOnUser to the one in Form1. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

              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