Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 7th, 2011, 04:35 PM
Authorized User
 
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
 
Old July 7th, 2011, 05:38 PM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
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
 
Old July 7th, 2011, 06:19 PM
Authorized User
 
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.
 
Old May 1st, 2014, 04:31 PM
Friend of Wrox
 
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();

}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Increment a tag stefan.yu XSLT 10 April 16th, 2007 05:45 AM
How to auto increment ? Shawn Mohan SQL Server 2000 2 June 22nd, 2006 03:00 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 10:36 PM
Increment date akibaMaila VB.NET 2002/2003 Basics 1 August 11th, 2005 11:04 PM
auto increment? hosefo81 PHP Databases 1 February 2nd, 2004 03:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.