none
Passing Properties before opening a Form Fails RRS feed

  • Question

  • I have a form which I have created several Property variables on (see below) I did this after watching Pat Tormey's video on the Learning section of the Windows.Client site (http://windowsclient.net/learn/video.aspx?v=108089)...

    It all looks great and I wish I could get it to work... but when I go to the 2nd form to open the 1st form with the Property vars on NONE of the variables show up in the Intelesense.  What I am I missing????
    The load lines are in the code window #2 below...
        public partial class frmShipsLogDetails : Form
        {
            private PopulateControlsClass pCC = new PopulateControlsClass();
            
            private static string varPosID;
            public string posID
            {
                get { return varPosID; }
                set { varPosID = value;
                lblPosID.Text = varPosID;
                }
            }
            private static string varT_ID;
            public string T_ID
            {
                get { return varT_ID; }
                set { varT_ID = value;
                lblT_ID.Text = varT_ID;
                }
            }
            private static string varV_ID;
            public string V_ID
            {
                get { return varV_ID; }
                set { varV_ID = value;
                lblVesselID.Text = varV_ID;
                }
            }
            private static bool varnewLog;
            public bool newLog
            {
                get { return varnewLog; }
                set { varnewLog = value; }
            }
    
            public frmShipsLogDetails()
            {
                InitializeComponent();
            }
            // ---------------------------------------------------------------------
      private void btnAdd_Click(object sender, EventArgs e)
            {
                // load the New Trip Log form...
                frmShipsLogDetails.XXXXXXXXX
                FormsLoadClass.LoadfrmNewShipsLogDetails();
                this.Close();
            }

    Phill
    Monday, March 1, 2010 10:02 PM

Answers

  • Calle,

    Thanks for the assist....

    I tried the example you gave (above) but it keeps telling me that posID doesn't exist?

    That's because you are declaring variable as Form. Form class does not have the property posID. Only your class derived from Form declares this property.

    You need to declare it like this:

    frmShipsLogDetails newMDIChildSLEditDetails = new frmShipsLogDetails();
     ... 
    newMDIChildSLEditDetails.posID = pPos_ID;

    Now it'll compile :)

    best regards,
    Vladimir






    • Proposed as answer by Calle Mellergardh Tuesday, March 2, 2010 12:25 PM
    • Marked as answer by SquireDude Tuesday, March 2, 2010 5:20 PM
    Tuesday, March 2, 2010 11:54 AM

All replies

  • You're not accessing an object of type frmShipsLogDetails, but merely the static members of the System.Windows.Forms.Form class.
    You must declare an object first before you can set your properties. It's like allocating memory for all your private vars.
    It sounds like the line FormsLoadClass.LoadfrmNewShipsLogDetails(); creates an instance of the Form for you, if you could access the result of that operation I bet you could also set the values for your Form.
    If that line both creates the form and displays it before you have a chance to set your properties then you must split that method.

    /Calle

     
    - Still confused, but on a higher level -
    Monday, March 1, 2010 10:28 PM
  • hmmm... I think I tried that...

    Here is the code in the FormsLoadClass.LoadfrmNewShipsLogDetails();...
            public static void LoadfrmShipsLogDetails(string pPos_ID, string pT_ID, string pV_ID)
            {
                try
                {
                        //WriteToStatusBar("Opening the Ships Log Edit form.");
                        Form newMDIChildSLEditDetails = new frmShipsLogDetails();
                        // Set the Parent Form of the Child window.
                        newMDIChildSLEditDetails.MdiParent = MDI_Main.ActiveForm;
                        // Display the new form.
                        newMDIChildSLEditDetails.Show();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Only one instance of the Ships Log Edit can be opened. " + ex, "YachtLog3", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
    As you can see I have passed the parameters to this function.
    BUT I have tired to pass them on to the form by...
    newMDIChildSLEditDetails.?????? none of the var properties show up in the Intelesense...
    also frmShipsLogDetails.?????? same results...

    I must be missing something simple and basic here But I dont know what???

    Am I trying to pass the values in the wrong place?
    Phill
    Tuesday, March 2, 2010 3:23 AM
  • I can see you have passed arguments to the function but you never use them to set anything.

    Right after this line...
     Form newMDIChildSLEditDetails = new frmShipsLogDetails();

     ... you can set the properties such as:
    newMDIChildSLEditDetails.posID = pPos_ID;


    /Calle
    - Still confused, but on a higher level -
    Tuesday, March 2, 2010 3:51 AM
  • Calle,

    Thanks for the assist....

    I tried the example you gave (above) but it keeps telling me that posID doesn't exist?

    I will post all the code for the form just in case I am missing something here...  Its got me really stumped!!! :-(
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    using CustomerClassLibrary;
    using YachtLog3.App_Code;
    
    namespace YachtLog3.Forms
    {
        public partial class frmShipsLogDetails : Form
        {
            private PopulateControlsClass pCC = new PopulateControlsClass();
            
            private static string varPosID;
            public string posID
            {
                get { return varPosID; }
                set { varPosID = value;
                lblPosID.Text = varPosID;
                }
            }
            private static string varT_ID;
            public string T_ID
            {
                get { return varT_ID; }
                set { varT_ID = value;
                lblT_ID.Text = varT_ID;
                }
            }
            private static string varV_ID;
            public string V_ID
            {
                get { return varV_ID; }
                set { varV_ID = value;
                lblVesselID.Text = varV_ID;
                }
            }
            private static bool varnewLog;
            public bool newLog
            {
                get { return varnewLog; }
                set { varnewLog = value; }
            }
    
            public frmShipsLogDetails()
            {
                InitializeComponent();
            }
            // ---------------------------------------------------------------------
            private void frmShipsLogDetails_Load(object sender, EventArgs e)
            {
                // get the Title Vessel Name & ID
                GetVesselNameID();
    
                // new or Edit Log entry
                if (varnewLog == true)
                {
                    this.Text = this.Text + " - New Log Entry";
                }
                else
                {
                    this.Text = this.Text + " - Edit Log Entry";
                }
    
            }
    
            private void GetVesselNameID()
            {
                string retVesselRegistration = null;
                string retVesselName = null;
                // get the Vessels ID value
                this.lblVesselID.Text = GlobalVar.GetVesselIDValue();
                // get the vessels Registration number
                retVesselRegistration = GlobalVar.GetVesselRegistration(GlobalVar.GetVesselIDValue());
                // get the Vessels name
                retVesselName = GlobalVar.GetVesselNameValue(GlobalVar.GetVesselIDValue());
                this.lblVesselName.Text = retVesselName;
                this.Text = this.Text + " - " + retVesselName + "  (" + retVesselRegistration + ")";
    
            }
    
            private void btnCancel_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void btnOk_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
    

    Phill
    Tuesday, March 2, 2010 11:31 AM
  • Calle,

    Thanks for the assist....

    I tried the example you gave (above) but it keeps telling me that posID doesn't exist?

    That's because you are declaring variable as Form. Form class does not have the property posID. Only your class derived from Form declares this property.

    You need to declare it like this:

    frmShipsLogDetails newMDIChildSLEditDetails = new frmShipsLogDetails();
     ... 
    newMDIChildSLEditDetails.posID = pPos_ID;

    Now it'll compile :)

    best regards,
    Vladimir






    • Proposed as answer by Calle Mellergardh Tuesday, March 2, 2010 12:25 PM
    • Marked as answer by SquireDude Tuesday, March 2, 2010 5:20 PM
    Tuesday, March 2, 2010 11:54 AM
  • Calle,

    Thanks for the assist....

    I tried the example you gave (above) but it keeps telling me that posID doesn't exist?

    That's because you are declaring variable as Form. Form class does not have the property posID. Only your class derived from Form declares this property.

    You need to declare it like this:

    frmShipsLogDetails newMDIChildSLEditDetails = new frmShipsLogDetails();
     ... 
    newMDIChildSLEditDetails.posID = pPos_ID;

    Now it'll compile :)

    best regards,
    Vladimir







    So true Vladimir, I must have been tired yesterday...

    /Calle
    - Still confused, but on a higher level -
    Tuesday, March 2, 2010 12:26 PM
  • Daaahhhh

    How stupid do I feel!!!!!

    You hit the nail on the head.

    Thanks so much
    Phill
    Tuesday, March 2, 2010 5:20 PM