Post: My first ever program [C#]
04-29-2011, 03:51 AM #1
Blackstorm
Veni. Vidi. Vici.
(adsbygoogle = window.adsbygoogle || []).push({}); Here's my first ever program for C#. I made it the same day I BARELY started C#.

I think I did good! I know this sounds weird but I got most of my experience from MW2 GSC scripting.. Even though C# differs every now and then vastly with functions, etc. from MW2 GSC.

Well, I'm uploading my whole source, I created a basic calculator.

Tell me what you think, what I can improve, and how I did on it! =D

Constructional criticism IS accepted. Here's my main code:

    
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;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int i;
string textStr1, textStr2;
string val = "abcdefghijklmnopqrstuvwxyz/*+=!@#$%^&(){}][';><,";
textStr1 = textBox1.Text;
textStr2 = textBox2.Text;
bool invalidVal;
invalidVal = false;
if (textStr1.Length == 0) invalidVal = true;
if (textStr2.Length == 0) invalidVal = true;
for (i = 0; i < textStr1.Length; i++)
{
foreach (char ch in val)
{
if (textStr1[i] == Char.ToLower(ch))
{
invalidVal = true;
break;
}
}
}
for (i = 0; i < textStr2.Length; i++)
{
foreach (char ch in val)
{
if (textStr2[i] == Char.ToLower(ch))
{
invalidVal = true;
break;
}
}
}
if (invalidVal == false)
{
progressBar1.Maximum = 100000;
progressBar1.Minimum = 0;
progressBar1.Value = 0;
while (progressBar1.Value < progressBar1.Maximum) progressBar1.Value += 10;
float a = Convert.ToSingle(textBox1.Text);
float b = Convert.ToSingle(textBox2.Text);
if (comboBox1.SelectedItem == "+") textBox3.Text = Convert.ToString(a + b);
else if (comboBox1.SelectedItem == "-") textBox3.Text = Convert.ToString(a - b);
else if (comboBox1.SelectedItem == "/") textBox3.Text = Convert.ToString(a / b);
else if (comboBox1.SelectedItem == "*") textBox3.Text = Convert.ToString(a * b);
else if (comboBox1.SelectedItem == "^")
{
float num = a;
for (int x = 1; x < b; x++) num = num * a;
textBox3.Text = Convert.ToString(num);
}
string textStr3 = textBox3.Text;
}
else if (invalidVal == true) System.Windows.Forms.MessageBox.Show("Invalid Characters, or blank spaces have been inputted.");
}

private void redToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Red;
}

private void purpleToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Purple;
}

private void yellowToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Yellow;
}

private void orangeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Orange;
}
}
}


Here's my source if you wanna see the actual program! The UPDATED program was built in bin\Release.

Enjoy! =D

Source Code: You must login or register to view this content.

Actual Program (*.exe): You must login or register to view this content.

Virus scan: You must login or register to view this content.

The following 4 users say thank you to Blackstorm for this useful post:

helpmeoprah, Kakashii, kiwimoosical, Sigma
05-21-2011, 12:57 PM #20
kiwimoosical
Bounty hunter
I found a couple problems with it, for one if you put a character like ยง into the box, it would throw an exception and halt the program. So instead of running through a list of invalid characters, just check is the char is a digit. Also, always use doubles when computing higher level math, your results will be a lot higher. I saw a few other flaws but good for your first program Winky Winky

Here is it with a few fixes:
    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;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int i;
string textStr1, textStr2;
textStr1 = textBox1.Text;
textStr2 = textBox2.Text;
bool invalidVal;
invalidVal = false;
invalidVal = textStr1.Length == 0 || textStr2.Length == 0;
for (i = 0; i < textStr1.Length; i++)
{
if(!Char.IsDigit(textStr1[i]))
{
invalidVal = true;
break;
}
}
for (i = 0; i < textStr2.Length; i++)
{
if(!Char.IsDigit(textStr2[i]))
{
invalidVal = true;
break;
}
}
if (!invalidVal)
{
progressBar1.Maximum = 100000;
progressBar1.Minimum = 0;
progressBar1.Value = 0;
while (progressBar1.Value < progressBar1.Maximum) progressBar1.Value += 10;
double a = Convert.ToDouble(textBox1.Text);
double b = Convert.ToDouble(textBox2.Text);
if (comboBox1.SelectedItem == "+") textBox3.Text = Convert.ToString(a + b);
else if (comboBox1.SelectedItem == "-") textBox3.Text = Convert.ToString(a - b);
else if (comboBox1.SelectedItem == "/") textBox3.Text = Convert.ToString(a / b);
else if (comboBox1.SelectedItem == "*") textBox3.Text = Convert.ToString(a * b);
else if (comboBox1.SelectedItem == "^")
{
double num = a;
for (int x = 1; x < b; x++) num *= a;
textBox3.Text = Convert.ToString(num);
}
string textStr3 = textBox3.Text;
}
else System.Windows.Forms.MessageBox.Show("Invalid Characters, or blank spaces have been inputted.");
}

private void redToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Red;
}

private void purpleToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Purple;
}

private void yellowToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Yellow;
}

private void orangeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.BackColor = System.Drawing.Color.Orange;
}
}


baii x)

The following 2 users say thank you to kiwimoosical for this useful post:

Blackstorm, Relevant
05-23-2011, 11:19 PM #21
|C++|
< ^ > < ^ >
how did you learn to script for mw2, people say its c++ but i am pretty advanced in c++ and mw2 is nothing like c++, also can i get documentation for the api(engine)
Last edited by |C++| ; 05-23-2011 at 11:30 PM.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo