| View previous topic :: View next topic |
| Author |
Message |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Sun Aug 31, 2008 7:44 pm Post subject: Organizing Tips in C# |
|
|
Hi there, I'm going to go straight to the point.
I started C# almost a month ago, my codes are basic
Anything I really need I just define within the first event handler I make.
I was just wondering if thats a bad habit?
Here's an example code...
| Code: | private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Invalid Value", "Error");
return;
}
else if (textBox1.Text == "0")
{
MessageBox.Show("Invalid Value", "Error");
return;
}
string box = textBox1.Text;
int interval = Convert.ToInt32(box + "000");
timer1.Enabled = true;
timer1.Interval = interval;
this.button1.Text = "Timer has started.";
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
this.button1.Text = "Start Timer";
MessageBox.Show("Time is up!", "Time's Up");
} |
Basically, is there a way, I could put my codes to be more neat?
If you have any organizing tips, I'd greatly appreciate it.
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Sun Aug 31, 2008 8:16 pm Post subject: |
|
|
you can put them in form load, initialize event, etc.
_________________
Blog
| Quote: | Rhys says:
you can be my maid
Rhys says:
ill buy you a french maid outfit
Tyler says:
Sounds good
Rhys says:
ill hold you to that |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sun Aug 31, 2008 8:38 pm Post subject: Re: Organizing Tips in C# |
|
|
| Code: | private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("Invalid Value", "Error");
return;
}
else if (textBox1.Text == "0")
{
MessageBox.Show("Invalid Value", "Error");
return;
}
string box = textBox1.Text;
int interval = Convert.ToInt32(box + "000");
timer1.Enabled = true;
timer1.Interval = interval;
this.button1.Text = "Timer has started.";
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
this.button1.Text = "Start Timer";
MessageBox.Show("Time is up!", "Time's Up");
} |
For starters, name your controls.
Also, I'd have done it like this:
| Code: |
if (txtValue.Text == String.Empty)
{
//do something
return;
}
int Time = int.Parse(txtValue.Text) * 1000;
if (Time < 1)
{
//do something
return;
}
tmrSomething.Interval = Time;
tmrSomething.Enabled = true;
btnStart.Text = "Timer started.";
|
Just me, though.
_________________
|
|
| Back to top |
|
 |
yoyonerd Grandmaster Cheater
Reputation: 0
Joined: 26 Apr 2008 Posts: 699 Location: -->formerly yoyonerd<--
|
Posted: Mon Sep 01, 2008 12:45 am Post subject: |
|
|
thanks samurai, I didn't make my code exactly that, but it did give me ideas
do you by chance know anyway, i could perhaps filter any letters from being typed into the textbox?
like a filter
|
|
| Back to top |
|
 |
pkedpker Master Cheater
Reputation: 1
Joined: 11 Oct 2006 Posts: 412
|
Posted: Mon Sep 01, 2008 5:32 pm Post subject: |
|
|
I don't check ever if value is valid or not when you click a button I dont even add buttons for this kind of work. But instead add a check when the text changes is what I do.. so if someone puts a number like 999
and text changed has a checker resets the value back to original default value like 100 or so.. otherwise It would modify the global timer settings or whatever it is suppose to do..
| Code: |
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
//reset to defautl value goes here.. if it is too high.. or not NUMBERIC
//blah blah new text is = TextBox1.Text;
//change timer settings here without pressing buttons!
}
|
_________________
|
|
| Back to top |
|
 |
|