Page 1 of 1

First programming class project... help me!

Posted: Tue Feb 10, 2009 1:57 pm
by Levio91
http://tinyrealm.com/~efa/cisc2305/cisc2305_l4.php

Thats the lesson thing I need to do. I already created the gui (using visual C# 2008) I just dont know how I add this code routine.

Code: Select all

public bool IsNumeric(string s)
{
try
{
Double.Parse(s);
}
catch
{
return(false);
}

return(true);
} 
This is what I have so far
lab5.zip
Lab 5
(44.47 KiB) Downloaded 108 times
It says something about making it a method... someone please explain

Re: First programming class project... help me!

Posted: Tue Feb 10, 2009 7:26 pm
by LeonBlade
That public bool IsNumeric is the method.

A method is a function inside a class, you call a method in order to do something.
It's used so that you can do something dynamic, or use it over and over again, saving you lot's of time in writing code and making it more efficient.

Where you have the IsNumeric method is fine right now, it needs to be inside of the class braces and not inside any other methods...

Code: Select all

class 
{

   method 
   {
      // do stuff
   }

   method 
   {
      // do stuff
   }   

}
Now you need to make a button event handler.
The easiest way to do this is just by double clicking on the button in the Design view.
This will auto-generate the Event Handler code for you and take you right to the method created.
You should defiantly see how it works someday so that you can get an understanding at what it is doing.

You can paste in your other code within that new button click method you just created...

Code: Select all

if(IsNumeric(TextBox1.Text) == true)
{
        // your temp convert code goes here
}
        else
{
        // your message window code goes here
} 
This code calls the IsNumeric method and is passing in a string(text).
The method is of type bool (boolean) meaning it returns a true or false depending on what it says in the method.
So you can assume by the name IsNumeric it returns true if what you wrote was a number, and false if it wasn't a number.

Everything within the first loop with "your temp convert code goes here" is what you write when you get true.
And the else is for anything other than true, which can only be false (in this case of a boolean).

The method you wrote in is advanced for just beginners however, you might want to either forget that or move on (if you even can) unless you know what Try...Catch is...
Hope this helps.

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 2:18 pm
by Levio91
Thanks LeonBlade.

I have another problem though I cant figure out whats wrong with my equation thats supposed to be used to convert celsius to fahrenheit!!!!!! could you look at it and tell me whats wrong this is the equation.

Code: Select all

((temp_c/(5/9))+32);
you can look at the entire code here.
Lab5-MTH.zip
(66.96 KiB) Downloaded 90 times

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 2:36 pm
by dandymcgee
Levio91 wrote:Thanks LeonBlade.
I have another problem though I cant figure out whats wrong with my equation thats supposed to be used to convert celsius to fahrenheit!!!!!! could you look at it and tell me whats wrong this is the equation.

Code: Select all

((temp_c/(5/9))+32);
Should be:

Code: Select all

((temp_c*(9/5))+32);

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 2:48 pm
by Levio91
That got it working better. but It still isnt accurate. I need it to convert fahrenheit to celcius.

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 3:37 pm
by Levio91
I got it to work.

Code: Select all

((temp_c / (5.0 / 9.0)) + 32);
Now all I need to know is how to make this file an exe like when I install games and stuff...

Id let you download it but maximum download size is 256 kb the program is 276 kb...

Code: Select all

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;
//*********************************************************
// Convert:: A simple conversion example
// Levi Harman A.K.A.  (mth)
// ********************************************************
// Date        Comments                                pgr
// =====       =========                               ====
// 2/10/09     First working version with input tests  mth
//*********************************************************
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            float temp_c;
            float temp_f;

            InitializeComponent();
        }

        public bool IsNumeric(string s)
        {
            try
            {
                Double.Parse(s);
            }
            catch
            {
                return (false);
            }

            return (true);
        } 




        // f2c
        private void button1_Click(object sender, EventArgs e)
        {
            float temp_f = 0;
            float temp_c = 0;


            if (IsNumeric(textBox1.Text) == true)
            {
                // temp convert code goes here
                temp_f = float.Parse(textBox1.Text);

                temp_c = (float) ((temp_f - 32) * 5 / 9.0);

                textBox2.Text = temp_c.ToString();
            
            }
            else
            {
                // message window code goes here
                textBox1.Text = "need number";
            } 
        }




        // c2f
        private void button2_Click(object sender, EventArgs e)
        {

            float temp_f = 0;
            float temp_c = 0;


            if (IsNumeric(textBox2.Text) == true)// validity test
            {
                temp_c = float.Parse(textBox2.Text);
                temp_f = (float)((temp_c / (5.0 / 9.0)) + 32);
                textBox1.Text = Convert.ToString(temp_f);
            }
            else
            {
                textBox2.Text = "need a number";
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 3:47 pm
by Levio91
heres an image of it runningImage

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 5:52 pm
by MarauderIIC
Build a release (under proj->active configuration, then push build) and then pull the exe out of project/release directory. But if you're using MSVS it might not run on the other computer for reasons explained (obfuscated, perhaps) in the links in this news post.

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 6:26 pm
by LeonBlade
Also, you might want to rename that "Form1" dialog text to something more appropriate.
And you have some methods that the code made for you that aren't used, you should remove those as well...
Then maybe make your own Icon and then you got an impressive program :) (for a beginner lol)

Re: First programming class project... help me!

Posted: Fri Feb 13, 2009 9:14 pm
by KuramaYoko10
Levio91 wrote:
Now all I need to know is how to make this file an exe like when I install games and stuff...
You said you are using Microsoft Visual C#? I am using the Microsoft Visual Studio 2008 so it should look alike (I guess), here is how I do it... (I believe it doesnt have this feature in the express edition though):

- You right click "Solution" in the Solution's Panel, and click add -> New Project
- Look for the "Other Project Types -> Setup & Deployment" , and select Setup Project
- Then you can do several things with this, add a shortcut to the users desktop, decide which files you want to insert in the folder, where to install it, do registry keys and etc... you have to play with it, but the basic is to right click "Application Folder" and select "add -> Project Output"
- Then you just have to build the project again, and the Setup folder will be created inside your projects folder... then you have just to install it using either the setup.exe or setup.msi

Tried to do my best explaining it xD... try it and pm me if you have any questions with it ;)