Passing a groupbox and its controls to another class-VBForums
Results 1 to 2 of 2

Thread: Passing a groupbox and its controls to another class

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2013
    Location
    SF Bay Area
    Posts
    50

    Passing a groupbox and its controls to another class

    I'm working on trying to understand classes better. I didn't feel like I got a good grasp of it in my programming class last semester. So, I'm using a textbox for a different class (Visual Basic) for the no-nothing assignments design to reinforce the lesson.

    Anyway, I'm a bit stuck. I tried doing a search on the web, and just couldn't find the answer to get me over the hump. The assignment is to create some conference registration form. Form design was given. Also said to use a three-tier program design. There is a checkbox for preconferences, when checked shows three conference choices (each is course is the same fee).

    What I did was create classes for the form, validating data, and database. What I was thinking I could do was pass the groupbox and the controls within it (three radio buttons). Then I could check to make sure the user picks one (passing to validation class), and get the radio button text to write to the text delimited file (passing to the database class). I've passed the groupbox, I think, but it's not letting me access the controls.

    Here's the code to show what I was doing. I may have included some extra, unneeded information. I just wasn't sure.

    I've create a class to create an object to hold customer information.
    Code:
    namespace NetworkConferenceRegistration
    {
        public class ConferenceRegistraction
        {
            //fields
            private int corpID;
            private string firstName;
            private string lastName;
            private int days;
            //private string conferenceName;
    
    [removed code]
    
            //constructor
           public ConferenceRegistraction(int intCorpID, string strFirstName, string strLastName, int intDays, GroupBox grpCourses)
            {
                corpID = intCorpID;
                firstName = strFirstName;
                lastName = strLastName;
                days = intDays;
                conferenceName = grpCourses.Controls.OfType<RadioButton>;
                
            }
    
            //properties
            public int CorpID
            {
                get
                {
                    return corpID;
                }
                set
                {
                    corpID = value;
                }
            }
    
    [removed code] 
            
            public string ConferenceName //readonly.  don't think i'll need to set which radio button to use or change its text property
            {
                get
                {
                    
                    return ConferenceName;
                }
            }
                            
            //calculate conferense cost
            public decimal GetCost(CheckBox chkPreconference)
            {
                //local variables
                const decimal decConferenceRatePerDay = 350;    //conference rate per day
                decimal decConferenceCost = 0;                     //total cost of conference
    
                //calculate the cost based on number of days
                decConferenceCost = decConferenceRatePerDay * Days;
                
                //add preconference cost
                if (chkPreconference.Checked == true)
                {
                    decConferenceCost += 675;
                }
                return decConferenceCost;
            }
        }
    }
    the form
    Code:
    namespace NetworkConferenceRegistration
    {
        public partial class frmNetworkRegistraction : Form
        {
           [code removed]
    
            //get user information and calculate costs
            private void btnCalculateCosts_Click(object sender, EventArgs e)
            {
                //check to see if user data is valid
                if (txtCorpID.MaskFull & //did user input ID number
                    Validation.IsDataEntered(txtFirstName, "First Name", ref strErrorMessage) &   //did user enter first name
                    Validation.IsDataEntered(txtLastName, "Last Name", ref strErrorMessage) &    //did user enter last name
                    Validation.IsDataEntered(txtNumberOfDays, "Number of Days", ref strErrorMessage) & //did user enter number of days
                    Validation.IsDataWithinRange(txtNumberOfDays, 1, 4, "Number of Days", ref strErrorMessage) == true)    //is number of days four or less
                {
                    //create object and instantation
                    ConferenceRegistraction objNewConferenceRegistraction = new ConferenceRegistraction(Convert.ToInt32(txtCorpID.Text.Trim()), txtFirstName.Text.Trim(), txtLastName.Text.Trim(), Convert.ToInt16(txtNumberOfDays.Text.Trim()));
                    lblTotalCosts.Text = objNewConferenceRegistraction.GetCost(chkPreConferenceCourse).ToString();
                }
                else
                {
                    MessageBox.Show(strErrorMessage, "Input Error");
                    
                    //reset error message
                    strErrorMessage = string.Empty;
                }
            }
    [code removed]
    
           //change form to display preconferences course selection or not
            private void chkPreConferenceCourse_CheckedChanged(object sender, EventArgs e)
            {
                if (this.grpCourses.Visible == false)
                {
                    this.grpCourses.Visible = true;
                }
                else
                {
                    this.grpCourses.Visible = false;
                }
            }
        }
    }
    Any help or suggestions will be appreciated. Thanks in advance.

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,264

    Re: Passing a groupbox and its controls to another class

    I'm not sure I'm quite following what you're trying to do with the groupbox and radiobuttons, but you shouldn't pass UI controls to your application tier. The use of the term "tier" normally refers to physical deployment, so you're talking about a different server when going from your client tier to the application server tier, so it needs to be some kind of serialised message. I have a feeling they don't necessarily mean "tier", however, and maybe meant "layer". Even when dealing with layers (which are a logical division of code, nothing more) you shouldn't let the UI controls seep out of the UI layer.

    It would be interesting to have more information of the actual assignment. For instance: the "preconference" check box - what is this supposed to do? What your code says it does is when toggled, it toggles the visibility of a group box. There doesn't seem to be much else, and so it isn't really capturing any business logic.

    What I'd expect to see in a more structured application is for the check box to be bound to a setting on another object, either explicitly with methods:
    csharp Code:
    1. //change form to display preconferences course selection or not
    2. private void chkPreConferenceCourse_CheckedChanged(object sender, EventArgs e)
    3. {
    4.     if (chkPreConferenceCourse.Checked)
    5.     {
    6.         _conferenceRegistrationController.ShowPreConferenceCourses();
    7.     }
    8.     else
    9.     {
    10.         _conferenceRegistrationController.HidePreConferenceCourses();
    11.     }
    12. }

    Or explicitly through a single property:
    csharp Code:
    1. //change form to display preconferences course selection or not
    2. private void chkPreConferenceCourse_CheckedChanged(object sender, EventArgs e)
    3. {
    4.     _conferenceRegistrationController.ShowPreConferenceCourses = chkPreConferenceCourse.Checked;
    5. }

    Or with some databinding framework (I'd suggest WPF over WinForms for the built-in databinding, Although it's possible to create your own mini-binding library without too much effort, it is possibly not a beginner thing. As you probably don't have the choice to use WPF, so maybe stick with one of the first two options.)

    The "_conferenceRegistrationController" field is an instance of the "ConferenceRegistrationController" class, and this is where nearly all the logic of the UI should sit. It has no reliance on the UI itself. So, this class is what determines what happens when the check box is toggled. It then notifies the view by some mechanism (as with the view notifying the controller about the check box toglle, there are a few mechanisms by which this can happen) as to what should change. You may have the list of courses not as radio buttons but as items in a list. The list might be bound to a collection that can be simply updated with the relevant items and the listbox will update itself accordingly. Sticking with radio buttons, you might have some code that dynamically generates and removes radio button controls in response to changes in the list of courses (this code is dealing with the UI controls directly, so would be placed in the Form class rather than the Controller class).

    Changing the selection of the course should notify the Controller object as well, so that it knows which course is selected. Then, when the calculate prices button is pressed, the Controller object gets notified and calculates the prices based on its knowledge of the state, not relying on the form's controls' state.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.