473,624 Members | 2,302 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting textbox.text to real number

How do I convert a value in a textbox to a real number so I can perform some
math ops on it? I understand I will need to validate the textbox's input to
make sure it is a real number and not a string so any suggestions along
those lines are also appreciated.

Thanks,
Scott
Nov 17 '05 #1
3 11149
OK, I did some more searching and found I can use the Convert class
(convert.todoub le) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
"Homer Simpson" <homer@moe's.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
How do I convert a value in a textbox to a real number so I can perform
some math ops on it? I understand I will need to validate the textbox's
input to make sure it is a real number and not a string so any suggestions
along those lines are also appreciated.

Thanks,
Scott

Nov 17 '05 #2
double d = 0;
try{d=double.Pa rse(yourtextbox .Text)
} catch {}

"Homer Simpson" <homer@moe's.co m> wrote in message
news:eg******** ******@TK2MSFTN GP10.phx.gbl...
OK, I did some more searching and found I can use the Convert class
(convert.todoub le) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
"Homer Simpson" <homer@moe's.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
How do I convert a value in a textbox to a real number so I can perform
some math ops on it? I understand I will need to validate the textbox's
input to make sure it is a real number and not a string so any
suggestions along those lines are also appreciated.

Thanks,
Scott


Nov 17 '05 #3
Hi Scott,

Convert will thrown an exception if it fails. You can use

bool success = double.TryParse (...)

You can also limit the TextBox input to numbers only + any special characters like decimal point etc. Handle the TextBox.KeyPres s event for this.
On Sun, 24 Jul 2005 06:46:13 +0200, Homer Simpson <homer@moe's.co m> wrote:
OK, I did some more searching and found I can use the Convert class
(convert.todoub le) to convert the string to a double. It works but is this
the best way?

I'm still looking for how to validate the input so any help there is
appreciated.

Thanks,
Scott
"Homer Simpson" <homer@moe's.co m> wrote in message
news:%2******** **********@TK2M SFTNGP10.phx.gb l...
How do I convert a value in a textbox to a real number so I can perform
some math ops on it? I understand I will need to validate the textbox's
input to make sure it is a real number and not a string so any suggestions
along those lines are also appreciated.

Thanks,
Scott



--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6799
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the nearest even penny using the code that follow. The problem I have is that once I set the even rounded value back to the text field it adds a 01 on the end of the raounded value (i.e. 1234.56 is 1234.5601)
0
1272
by: PeterZ | last post by:
Hi, I have a win forms project, for simplicity's sake, lets say that I have a dataset and a textbox that is bound to a numeric column in the dataset. I also specified a ConvertEventHandler to format the number in the textbox. The problem I have is when I run the app, the very first record the textbox displays is never formatted correctly (the ConvertEventHandler is never called, I checked). Navigating to the next and subsequent
5
21472
by: Brian Robbins | last post by:
In standard C/C++ SDK, MFC, or VCL if I wanted to make a TextBox (CEditBox, TEditBox, etc.) only except numbers I have dozens of ways to handle it. But none of those methods are working in C#.NET. I may be just having a paradigm shift issue and I just don't understand new the methods fully, yet. I would love to use the Validation methods, but they occur WAY TOO LATE in the event order list! I don't want the TextBox.Text to ever have...
1
2942
by: Shawn | last post by:
Anybody know how to use wordwrap=true and multiline = true on a textbox, but still have the textbox.lines attribute advance if the wordwrap happens? I have a large textbox that is setup like this and I want to get the number of lines of text, but right now, unless a user hits Enter, it always reports 1 line.
12
3145
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
3
4008
by: RG | last post by:
I am reading in from a serial port a 16 bit number from 0 to 65536. It is being read as a string from my microcontroller into VB using DEC5 or 5 digits are displayed. I need to display this value live as a decimal number so I can manipulate the value. For instance I have the following line: TextBox2.Text = buffer1 - 32768 but this generates an error after a few iterations because buffer1 is
16
5135
by: mj.redfox.mj | last post by:
Can anyone help? I have a textbox which I'm programatically adding by using the following code: txtTest = New TextBox txtTest.ID = "txtLeft" + cntCount.ToString Page.FindControl("tdInput").Controls.Add(txtTest) This successfully creates a textbox called "txtLeft1" in the table
25
4375
by: Blasting Cap | last post by:
I keep getting errors that pop up when I am trying to convert an application from dotnet framework 1.1 to framework 2.0. The old project was saved in sourcesafe from Visual Studio 2003, and I have opened it in Visual Studio 2005. I've cleared up most errors, but have one that beats the heck out of me. I have a page: sales/Item.aspx that has several datagrids in it.
1
31356
by: Brad Pears | last post by:
I am using vb.net 2005 and SQL server 2000. In my table I have a date field of type "smalldatetime". In my vb application, the user may or may not enter a date value into the appropriate text box. I then want to pass the value of this text box as a datetime variable to my stored procedure which inserts or updates the row. As I mentioned above, the textbox may be left blank - hence I would want to pass a null to the stored proc. How do I...
0
8242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8177
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8629
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7170
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1793
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1488
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.