Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion 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 Display Modes
  #1 (permalink)  
Old July 7th, 2011, 05:35 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 Increment button

I'm trying to build a 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, 06:38 PM
Wrox Author
Points: 1,160, Level: 13
Points: 1,160, Level: 13 Points: 1,160, Level: 13 Points: 1,160, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2010
Location: Austria
Posts: 174
Thanks: 3
Thanked 53 Times in 53 Posts
Default

because the Text property is of type string, you need to do seg.ToString(). However, the value is always "1" as you create seg within the handler every time the button is clicked. You need to declare the variable seg outside of the handler to keep its value.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
  #3 (permalink)  
Old July 7th, 2011, 07:19 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

Christian, I knew the answer would be simple. Thanks alot for your help.
  #4 (permalink)  
Old May 1st, 2014, 05:31 PM
Friend of Wrox
Points: 528, Level: 8
Points: 528, Level: 8 Points: 528, Level: 8 Points: 528, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

if you want the button and the Text box to keep track of the value its self try this.


Code:
private void segbutton_Click(object sender, EventArgs e)
{
        // if the box is empty set seg to 0 other wise convert contents of text box to an int32 and set seg
        // will be empty on first post back of form if not set at client side
        int seg =  String.IsNullOrEmpty( segtextBox.Text.Trim() ) ? 0 : Convert.ToInt32(segtextBox.Text.Trim());
        segtextBox.Text = (seg+=1).ToString();

}
 


Thread Tools
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
Increment a tag stefan.yu XSLT 10 April 16th, 2007 06:45 AM
How to auto increment ? Shawn Mohan SQL Server 2000 2 June 22nd, 2006 04:00 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 11:36 PM
Increment date akibaMaila VB.NET 2002/2003 Basics 1 August 12th, 2005 12:04 AM
auto increment? hosefo81 PHP Databases 1 February 2nd, 2004 03:56 PM



All times are GMT -4. The time now is 11:42 PM.


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