3 Replies - 108 Views - Last Post: 08 February 2012 - 08:59 AM Rate Topic: -----

Topic Sponsor:

#1 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Form class and methods vs classes

Posted 08 February 2012 - 08:18 AM

Hi,

This is probably an extremely basic question (im sorry) but its still fuzzy in my head.

Question: When should i be creating seperate classes in a WinForm project vs just writting methods in the form1 partical class?

eg, I have a tab panel which has several user inputs. When the user goes to the next tab panel and clicks a button, i want to verify all the user inputs on the previous page. Atm, i have several methods written within the form1 partical class and calls to them from the button_Clicked event. Is this the correct way to lay the program out? It seems a bit messy.

Thanks

Edit: I already have some functionality in class libraries.

This post has been edited by scolty: 08 February 2012 - 08:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Form class and methods vs classes

#2 tlhIn`toq  Icon User is offline

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3291
  • View blog
  • Posts: 6,899
  • Joined: 02-June 10

Re: Form class and methods vs classes

Posted 08 February 2012 - 08:32 AM

THat is where the 'engineer' part of software engineer comes in. There is no rule to follow when to do it.

Its is about your style and the complexity of the program and data. But my own rule of thumb is to have the objects do as much of their own work and the GUI only exists to interact with the data. It shows it to the user, and gets input from the user.

If you are making a human resources program for example and you enter a social security number into an EmployeObject I would expect the object to validate not the form. If hte form validates that means each and every input/change form has to validate. But if the EmployeeObject validates there is only one set of code.

That's another way to look at it: We don't duplicate code! If you find you are duplicating code in 2-3 forms, then its wrong: Most times. And you should probably be looking to place it in the object.


But then if it is a simply one-form, self-contained little whack it out in a day type tool then it probably doesn't matter.

You as the software engineer are going to have to decide. And that is just going to come with time and experience. After 100 programs you're not going to wonder any more.
Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon

Reputation: 3134
  • View blog
  • Posts: 5,402
  • Joined: 08-June 10

Re: Form class and methods vs classes

Posted 08 February 2012 - 08:35 AM

There's no definitive answer to this question. It's a matter of style and appropriateness for the task at hand.

One thing to consider is an MVC-style approach. Design models to represent your data. Design forms/user controls to act as views for your data. The code-behind is a pseudo-controller, that maps the models to the forms, by taking input from controls and storing them in the model, and/or the other way around, taking data from a model and placing it into the form's controls. You can put your validation and transformation logic in the model. And you can pass the models from one form/user control to the next, if you need to do that.

I do suggest looking into User Controls to represent functional grouping of UI controls.
Was This Post Helpful? 1
  • +
  • -

#4 scolty  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 259
  • Joined: 27-April 11

Re: Form class and methods vs classes

Posted 08 February 2012 - 08:59 AM

Thanks for replying. Thats a fair point with regards to shifting the error checking code into the class or placing it in the User Controls. The following is a snippet i was asking about yesterday (Method is called HeaderNamer) and its not directly tied to a different object (i guess it could be contained in the button if i created a User Control there). Assuming i/we havent created a User Control, im interested to know where you guys (much more experienced than i) would choose to put it, a separate class declaration in the CSVAppForm(equivalent of form1) or just leave it within the CSVAppForm class?

public partial class CSVAppForm : Form
    {
        public CSVAppForm()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            LoadNewFile();
        }

        public void Logger(string input)
        {
            txtLog.Text += input + Environment.NewLine;
            txtLog.Selectionstart = txtLog.Text.Length;
            txtLog.ScrollToCaret();
        }

        ...

        //Updates number of TextBoxes and ComboBoxes visible to the user.
        private void numColCount_ValueChanged(object sender, EventArgs e)
        {
            //Number of DataType combo boxes available & specific counter.
            int numberDataType = (int)numColCount.Value;
            int countDataType = 0;
            
            //Number of column name TextBoxes available & specific counter.
            int numberNames = numberDataType;
            int countName = 0;

            foreach (Control control in groupBoxTableComp.Controls)
            {
                if (control is ComboBox)
                {
                    if (countDataType < numberDataType)
                    {
                        control.Visible = true;
                        control.Enabled = true;
                    }
                    else
                    {
                        control.Enabled = false;
                        control.Visible = false;
                    }
                    countDataType++;
                }
            }

            foreach (Control controlTxt in groupBoxTableComp.Controls)
            {
                if (controlTxt is TextBox)
                {
                    if (countName < numberNames)
                    {
                        controlTxt.Visible = true;
                        controlTxt.Enabled = true;
                    }
                    else
                    {
                        controlTxt.Enabled = false;
                        controlTxt.Visible = false;
                    }
                    countName++;
                }
            }   
        }

        //Verifies input data on "Selection" tab and then check the file.
        private void btnCheckData_Click(object sender, EventArgs e)
        {
            //ComboBox and TextBoxes contain information.
            int columns = (int)numColCount.Value;
            bool verifiedColumnNames = HeaderNames();

            if (verifiedColumnNames == false)
            {
                //Refocus on the "Selection" tab and display an error message.
                tabControl1.SelectedIndex = 0;
                MessageBox.Show("Column names are not unique, please re-enter the names", "User Input Error:");
            }
            else
            {
                //Update log
                string log = string.Format(Environment.NewLine +  "----- New Operation -----" + Environment.NewLine + 
                    "The selected file contains {0} columns.", numColCount.Value.ToString());
                
                Logger(log);
                Logger("Column name(s) have been verified.");
            }
        }

        //Itterate through TextBoxes and add to array if they are unique.
        public bool HeaderNames()
        {
            int counter = 0;
            //Declare an array which contains the same number of elements as columns specified by the user.
            string[] headerNames = new string[(int)numColCount.Value];
 
            foreach (Control control in tabPage1.Controls)
            {
                if (control == groupBoxTableComp)
                {
                    foreach (Control textbox in control.Controls)
                    {
                        if (textbox is TextBox)
                        {
                            if (counter < ((int)numColCount.Value))
                            {
                                if (headerNames.Contains(textbox.Text.ToUpper()))
                                {
                                    //Return false if one or more of the TextBoxes are not unique.
                                    return false;
                                }
                                else
                                {
                                    headerNames[counter] = textbox.Text.ToUpper();                                    
                                }
                                counter++;
                            }
                        }
                    }
                }
            }
            headerNames = null;
            //Return true if the TextBoxes are unique.
            return true;
        }
    }



Thanks guys.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1