9 Replies - 5486 Views - Last Post: 17 March 2015 - 05:35 AM Rate Topic: -----

#1 alanmmb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-March 15

Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 06:10 PM

Can't seem to get this code to show the numbers when I type them on click on the check boxes, the clear and exit button work fine but the calculate button doesn't do any calculations nor do I see any numbers in any boxes unless I type them myself



namespace JoesAutomotiveFrm
{
    public partial class frmJoesAutomotive : Form
    {
        public frmJoesAutomotive()
        {
            InitializeComponent();
        }
     private void calculateTotalButton_Click(object sender, EventArgs e)
        {
            OilLubeCharges();
            FlushCharges();
            MiscCharges();
            PartsAndLaborCharges();
            TaxCharges();
            TotalCharges();
        }
        //Method for calculating oil and lube charges
        private int OilLubeCharges()
        {
            int total = 0;

            //add 26 dollars if oil is checked 
            if (oilChangeBx.Checked)
            {
                total += 26;
                servicesOutputLb.Text = total.ToString("c");
            }

            //add 18 dollars to total when lube is checked
            if (lubeJobBx.Checked)
            {
                total += 18;
                servicesOutputLb.Text = total.ToString("c");

                return total;
            }
            else
            {
                return total;
            }
        }


        private int FlushCharges()
        {
            int total = 0;
            //adds 30 dollars when radiatior flush is checked
            if (radiatorFlushBx.Checked)
            {
                total += 30;
                servicesOutputLb.Text = total.ToString("c");
            }
            //add 80 dollars if trans flush is checked
            if (transFlushBx.Checked)
            {
                total += 80;
                servicesOutputLb.Text = total.ToString("c");
                return total;
            }
            else
            {
                return total;
            }
        }

        private int MiscCharges()
        {
            //define local variable
            int total = 0;
            //if inspection is checked, add to total
            if (inspectionBx.Checked)
            {
                total += 15;
                servicesOutputLb.Text = total.ToString("c");
            }
            //add 100 if replace muffler is checked
            if (replaceMufflerBx.Checked)
            {
                total += 100;
                servicesOutputLb.Text = total.ToString("c");
            }
            // adds 20 if tire rotation is checked
            if (tireRotationBx.Checked)
            {
                total += 20;
                servicesOutputLb.Text = total.ToString("c");
                return total;
            }
            else
            {
                return total;
            }
        }
        private int PartsAndLaborCharges()
        {
            int total = 0;
            int labor;
            int parts;

            if (int.TryParse(laborTextBox.Text, out labor))
            {
                servicesOutputLb.Text = labor.ToString("c");
                total = labor * 40;
            }
            if (int.TryParse(partsTextBox.Text, out parts))
            {
                totalOutputLb.Text = parts.ToString("c");

                return total;
            }
            else
            {
                return total;
            }
        }
        private decimal TaxCharges()
        {
            decimal parts = 0;
            decimal addTax;
            

            addTax = parts * 0.06m;
            taxOutputLb.Text = addTax.ToString("c");
            return addTax;
        }

        private decimal TotalCharges()
        {
            decimal total;

            total = OilLubeCharges() + PartsAndLaborCharges () + FlushCharges() + MiscCharges() + TaxCharges();
            totalOutputLb.Text = total.ToString("c");
            return total;
        
    
        }

        private void clearBtn_Click(object sender, EventArgs e)
        {
            // Clears all fields
            oilChangeBx.Checked = false;
            lubeJobBx.Checked = false;
            radiatorFlushBx.Checked = false;
            transFlushBx.Checked = false;
            inspectionBx.Checked = false;
            replaceMufflerBx.Checked = false;
            tireRotationBx.Checked = false;
            partsTextBox.Text = "";
            laborTextBox.Text = "";
            servicesOutputLb.Text = "";
            partsOutputLb.Text = "";
            taxOutputLb.Text = "";
            totalOutputLb.Text = "";


        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


Attached image(s)

  • Attached Image

This post has been edited by macosxnerd101: 16 March 2015 - 06:13 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Joes Automotive application, can't seem to get code to work

#2 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 07:48 PM

I'm going to stick with code questions, there are others here who will explain the best practice violations you've made

My first question, you've got you're methods returning values but you're not doing anything with them?

e.g your call to OilLubeCharges

second do you know how to use debugging? Are you certain that your checkbox state is checked?
Was This Post Helpful? 0
  • +
  • -

#3 alanmmb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-March 15

Re: Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 08:08 PM

View PostDamage, on 16 March 2015 - 07:48 PM, said:

I'm going to stick with code questions, there are others here who will explain the best practice violations you've made

My first question, you've got you're methods returning values but you're not doing anything with them?

e.g your call to OilLubeCharges

second do you know how to use debugging? Are you certain that your checkbox state is checked?



I don't understand what you mean by the first question, how should i address this issue?

and no I don't know how to use debugging, I recently started programming not too long ago. This is for a school assignment.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,228
  • Joined: 05-May 12

Re: Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 08:09 PM

If you set a breakpoint on line 16, and then you click on the Calculate button, is your breakpoint hit? If not, then that means you forgot to hookup the Click event handler for the button.
Was This Post Helpful? 0
  • +
  • -

#5 alanmmb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-March 15

Re: Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 08:12 PM

View PostSkydiver, on 16 March 2015 - 08:09 PM, said:

If you set a breakpoint on line 16, and then you click on the Calculate button, is your breakpoint hit? If not, then that means you forgot to hookup the Click event handler for the button.



I don't know how to do that. I'm doing this for a school assignment and I'm having a lot of difficulty with it. I tried to do as much as I understood out of the book they give us but it's not doing much apparently
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,228
  • Joined: 05-May 12

Re: Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 08:16 PM

Go to line 16. Press F9. This will set a breakpoint. Run your program by pressing F5. Click on your calculate button. Did focus switch back to Visual Studio and highlight line 16?
Was This Post Helpful? 0
  • +
  • -

#7 alanmmb14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-March 15

Re: Joes Automotive application, can't seem to get code to work

Posted 16 March 2015 - 08:32 PM

View PostSkydiver, on 16 March 2015 - 08:16 PM, said:

Go to line 16. Press F9. This will set a breakpoint. Run your program by pressing F5. Click on your calculate button. Did focus switch back to Visual Studio and highlight line 16?



Nothing happened, I clicked two or three times and it didn't take me anywhere or do anything as a matter of fact
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6529
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: Joes Automotive application, can't seem to get code to work

Posted 17 March 2015 - 04:54 AM

As others have pointed out, you're going to have a hard time figuring out where your program has issues if you don't understand how to debug.
Take 30 minutes to work the debugging tutorials linked below. That time will have you dozens of hours of guess work in this one project alone. Work smarter not harder.

I'm going to assume your class teacher does what most do and expect you to code - but (s)he hasn't bothered to first show you how to debug your programs in Visual Studio. I don't really know how they get their credentials with this thinking. Please see the debugging tutorials listed below: They will put you head and shoulders above your competition in class.

See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated February 2015
Spoiler

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6529
  • View blog
  • Posts: 14,446
  • Joined: 02-June 10

Re: Joes Automotive application, can't seem to get code to work

Posted 17 March 2015 - 05:00 AM

Looking at your code its very clear that C# is new to you... GUI programming is new for you... That you don't understand the idea of using properties instead of using your WinForm controls like they are variables. That you don't even realize that by using WinForms you've chosen a dieing GUI technology that hasen't been in employer demand for years. That you haven't worked through some of the most basic debugging tutorials which would give you some skills to solve really basic problems.

I strongly urge you to set this project aside, pick up one or two "Learn C# in 30 days" books and just work them from cover to cover. So many bad things I see here would be taken care of if you would just work on learning at this stage and leave the designing until you are more ready. I think if you then look at this same project with all the new knowledge you'll pick up you'll see where I'm coming from.



Looking at your code you have some bad practices in play that need your immediate attention.
These will form bad habits quickly and should be worked on. This is part of what I mean by "learn foundation concepts first".

Problem 1: Use of GUI controls as your variables.
You have numerous places where you are doing things like: SomeMethodCall(TextBox43.Text);
Your textboxes and other GUI elements are not meant to be your variables to hold values. They exist only to be a way for the user to interact with the data held in the logic code *behind* the GUI. You should have properties bound to the GUI. When the user changes something in a textbox it updates the property in your code. When the property gets a new value, the GUI is updated. But your logic always uses the property not the GUI element.
There is a tutorial on properties linked in my signature block.

Problem 2: This bit about button1 and textbox14 is just horrible. If you are working in old 1980's WinForms for Win98 and WinXP programs then name your control as soon as you make it before doing anything else with it. txtbxFirstName is so much easier to maintain than textbox32. Same with "frmConnectionSettings" instead of "form2" or "wndCustomerDetails" than "window4". If you are making programs for current OSes like Win7/8 then you should be working in WPF where the controls don't need to be named 90% of the time, but the same practice of giving meaningful names should still be followed in the few cases where they are needed.

Problem 3: Don't put meaningful operational code in GUI control handlers. You have a lot of places where the work is being done in the button event handler instead if in a dedicated method. Instead you want that handler to just direct action to the first methods, not actually perform the actions.
btnSave_Click(object sender, eventargs e)
{
   SaveSettings(); // Call the save method
   SavePlayers();
   SaveWindowGeometry();
   // Do NOT put all the save code here
}

This way you can call the same save methods independently from code without having to either duplicate code (evil practice) or without calling the button handler. If you are using WinForms then you have a .Click event handler. If you are writting in WPF then don't call the .Click event but instead create Commands and activate those. The .Click handler in WPF is kind of a carry-over to give WinForms coders a comfortable migration path but is not considered the way a modern profession WPF coder works.

Problem 4:
Eye sore code.
Take the extra 2 minutes to clean up your code to make it easy to read, easy to follow, and most important, easy to manage.
  • Kill all those dead blank lines at the ends of methods.
  • Wrap groups of methods that go together in #region blocks so you can find all your sending code and receiving code easily for example.
  • Get your lines aligned. Control-K, Control-D will do that for you (Think of it as Klean Document)


Problem 5: Decent naming
Methods like private void ClientSession(object sender, eventargs e); don't follow established naming style guides.
A method performs an action. By convention for decades methods are named as verbs to show what they do. Nouns like ClientSession would be presumed to be a property by any other coder looking at your code. private void CreateClientSession(object sender, eventargs e); on the other hand shows that it does something (a method) not that it is something (a variable).

I'm not going to go into a long critique of your code. And I don't want you to feel like I'm just out to pick on you. I'm just pointing out a couple of many foundation concepts you didn't pickup up in the early stages of learning C#. You can't leap-frog the learning process by trying to build a program in your first month. Nobody can. Its not just you. Nobody in the first month of a new discipline has enough knowledge to jump in and start designing finished solutions: Not carpentry, not automotive design, not software engineering. C# is a foreign language with its own syntax. You aren't going to say "I know no Russian. I'm going to write a Russian mystery novel as my vehicle to learn the language." Same here with programming.

I urge you to take a step back. Pick up a "Learn C# in 30 days" type book and work your way through it cover to cover; don't use it a like a reference dictionary when you get stuck. There are several in a thread pinned to head of the C# forum. Plus there are countless C# beginner tutorial series around just like the C# Learning Series - tutorials here on DIC. There are also numerous "My First C# Program" tutorials like this one that will help you with the design and OOP concepts that are vital to good programming.

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6155
  • View blog
  • Posts: 21,228
  • Joined: 05-May 12

Re: Joes Automotive application, can't seem to get code to work

Posted 17 March 2015 - 05:35 AM

Now that you know how to debug, and you have proof that your event handler is not being called, you'll have to hook up your event handler.

I write my forms by hand, so in my case, I just have to remember to add a line that looks like:
btnCalculate.Click += new EventHandler(btnCalculate_Click);


Most likely, the book you are using has you use the Form Designer. With that you should have double clicked on your button while the Form is in design mode, and it would have inserted code similar to above into your InitializeComponent(), and set up a stub for the handler itself where all you needed to do is to your in your code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1