Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 7th, 2011, 02:15 PM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default Project-simple question

I'm trying to build a real world project for calculating the number of objects seen. What I want to do is create a button on the form that will increment a text box, pretty simple. I seem to be having trouble getting the sytnax correct. So far I have
private void segbutton_Click(object sender, EventArgs e)
{
int seg = 0:
seg++;
segtextBox.Text = seg.?

}

Can one of you experts steer me in the right direction? thanks
  #2 (permalink)  
Old July 7th, 2011, 07:44 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

The line to display the value is:

segtextBox.Text = seg.ToString();

But if you declare the variable seg inside the Click event handler as you've done here, it is created anew every time the code enters the event handler. That means you get a new variable named seg each time so it starts initialized to 0, you add 1, and the result you see will always be 1.

To make it work the way you want it to, you need to declare seg outside of any event handler. Try this:

Code:
int seg = 0:

private void segbutton_Click(object sender, EventArgs e)
{
    seg++;
    segtextBox.Text = seg.ToString();
}
Now seg is created outside of the event handler so the same variable is used every time the event handler executes.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #3 (permalink)  
Old July 8th, 2011, 11:40 AM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rod, thanks for your reply. One more quick question. I have a whole string of these button clicks, and am trying to get them to display a running total. Unless I place the summation statement within one of the button click handler, it isn't recognized. What would be the best way to engineer this?
  #4 (permalink)  
Old July 8th, 2011, 04:02 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Do you mean you have a whole bunch of buttons? If you want them all to do *exactly* the same thing, hook them all up to the same event handler. Select one, click the Event button in the Properties window (the lightning bolt), select the Click event, and select the event handler you made for the first one.

If you want them to do slightly different things, either:

1. Copy and paste the code for each event handler. Don't re-declare the variable, though. That only needs to happen once.

Or:

2. Move the incrementing code into a separate method and then invoke it from the event handlers. (You may need to look at later lessons to see how to make a method.)

With either of these methods, you can add other code to make the different buttons do different things in addition to incrementing the counter.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #5 (permalink)  
Old July 18th, 2011, 12:53 PM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One last issue, I'm trying to write some code that will raise a Message box when a value reaches a certain number

private void Total200(decimal ding)
{
ding = decimal.Parse(totaltextBox.text, NumberSytles.Any);

if (ding = 200) MessageBox.Show("Done");

I'm getting an error that says, Cannot implicity convert type 'decimal' to
'bool' I'ved tried inserting some converison, but am not getting anywhere. I must be missing something on the way VS handles if statements.
  #6 (permalink)  
Old July 18th, 2011, 01:23 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

(Please start new threads for new questions. It makes it easier for people to find particular issues.)

This is a subtle and very common problem in C#, C, and C++.

Code:
if (ding = 200) MessageBox.Show("Done");
The test "ding = 200" uses = instead of == so it performs an assignment. It assigns the value 200 to the variable ding. The "result" of that is the value itself, 200, so the code then tries to evaluate 200 as a Boolean so it can perform the "if" test.

If you change it to "ding == 200," you should be all set. (It's very common to accidentally use = instead of ==.)
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #7 (permalink)  
Old July 18th, 2011, 04:20 PM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rod, thanks again for you reply. That was pretty simple. If you wouldn't mind taking a look at the code, the message box isn't being raised. Without posting the whole code, there is a method similar to the one below for each button, then a percent button config. for them all. I had the method listed in the previous post tacked onto the end, but the message box didn't get raised either. Is almost like the project is needing to have an event trigger it, button clicked etc.

private void blastbutton_Click(object sender, EventArgs e)
{
blast++;
blasttextBox.Text = blast.ToString("D");
int total = seg + band + lymph + mono + baso + eo + plasma +
rbc + meta + myelo + promyelo + blast;
totaltextBox.Text = total.ToString("D");
}



private void Form1_Load(object sender, EventArgs e)
{
decimal ding;
ding = decimal.Parse(totaltextBox.Text, NumberStyles.Any);
if (ding == 200) MessageBox.Show("Done");
}



private void percentbutton_Click(object sender, EventArgs e)
{
float pers = 200;
float percen = 100;
float segt;
if (float.TryParse(segtextBox.Text, NumberStyles.Any, null, out segt)) ;
{
float pseg = segt / pers * percen;
persegtextBox.Text = pseg.ToString();
}
float lympdt;
  #8 (permalink)  
Old July 18th, 2011, 10:58 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I see a couple of things that might be issues.

Code:
private void blastbutton_Click(object sender, EventArgs e)
{
blast++;
blasttextBox.Text = blast.ToString("D");
int total = seg + band + lymph + mono + baso + eo + plasma +
rbc + meta + myelo + promyelo + blast;
totaltextBox.Text = total.ToString("D");
}
The previous code doesn't have a MessageBox.Show statement so it won't display a message box. It should be displaying a value in the totaltextBox control, though.

Code:
private void Form1_Load(object sender, EventArgs e)
{
decimal ding;
ding = decimal.Parse(totaltextBox.Text, NumberStyles.Any);
if (ding == 200) MessageBox.Show("Done");
}
The previous code declares the variable ding so every time the event handler runs (and it will only run once then the form loads because it's a form Load event handler), that value is initialized to 0. The code parses whatever is in totaltextBox, but that will be whatever value it contains at design time because the form just loaded. It won't be updated until later when you click the button, but the form's Load event handler won't run again later.

I think you want something more like this:

Code:
private void blastbutton_Click(object sender, EventArgs e)
{
    blast++;
    blasttextBox.Text = blast.ToString("D");
    int total = seg + band + lymph + mono + baso + eo + plasma +
        rbc + meta + myelo + promyelo + blast;
    totaltextBox.Text = total.ToString("D");

    if (total == 200) MessageBox.Show("Done");
}
I think you can remove the form's Load event handler.

Let me know if that helps.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
  #9 (permalink)  
Old July 19th, 2011, 11:53 AM
Authorized User
Points: 139, Level: 2
Points: 139, Level: 2 Points: 139, Level: 2 Points: 139, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Rod, its working now, I added the if statment to each of the button click methods, its not elegant, but it works. The final piece is to try map some specific keys to activate the button clicks, I'll go back to my corner and hammer on this. I'm sure it'll take me some time to figure it out. Thanks a bunch for your assitance.
  #10 (permalink)  
Old July 19th, 2011, 12:00 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

A better solution is to put the common code in a method and call it from each button. I think that's Lesson 20.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple question? Cade_Feng BOOK: Beginning Regular Expressions 3 October 18th, 2012 07:14 PM
Very Simple Question YoungLuke C# 2 May 4th, 2007 02:23 AM
Simple ASP.NET Project anbane80 ASP.NET 1.0 and 1.1 Basics 2 December 8th, 2006 01:20 AM
Simple Question MSK888 C# 4 August 1st, 2004 12:12 AM



All times are GMT -4. The time now is 02:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.