473,396 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

converting to integer

Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was not in the correct format' error. Ive tried declaring the variables as strings and converting the textbox to integer from there and still get the same thing. I'm just trying to get a value to store it in that variable so i can use it for later calculations. Theres a lot more to it, but considering i'm getting this so early i don't wanna continue until i get it right.

Expand|Select|Wrap|Line Numbers
  1. Option Strict On
  2. Option Explicit On
  3.  
  4. Public Class Form1
  5.  
  6.     Private Sub cboCommute_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCommute.SelectedIndexChanged
  7.  
  8.         Dim intCommuteChoice As Integer
  9.         intCommuteChoice = Me.cboCommute.SelectedIndex
  10.         Select Case intCommuteChoice
  11.             Case 0
  12.                 CarCommute()
  13.  
  14.         End Select
  15.  
  16.     End Sub
  17.  
  18.  
  19.     Private Sub CarCommute()
  20.         Me.GroupBox1.Visible = True
  21.  
  22.         Dim intlabel1 As Integer
  23.         Dim intlabel2 As Integer
  24.  
  25.         intlabel1 = Convert.ToInt32(Me.Label1.Text)
  26.         intlabel2 = Convert.ToInt32(Me.Label2.Text)
  27.  
  28.     End Sub
  29.  
  30. End Class
  31.  
Mar 9 '08 #1
7 1751
Killer42
8,435 Expert 8TB
Which line throws the error?
Mar 10 '08 #2
25. I guess ill just declare variables as strings and take the input from the message boxes and convert them to integers when doing the calculations. Just wondering what the syntax is to force a textbox to accept an integer value instead of string.
Mar 10 '08 #3
QVeen72
1,445 Expert 1GB
Hi,

Try This :

Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  2.         If InStr("0123456789", e.KeyChar.ToString) <= 0 Then
  3.             e.Handled = True
  4.         End If
  5.     End Sub
  6.  
Regards
Veena
Mar 10 '08 #4
QVeen72
1,445 Expert 1GB
Hi,

If you are getting error on 25 and 26th line, you can just use the "Val" function like this..:

Expand|Select|Wrap|Line Numbers
  1. intlabel1 = Val(Me.Label1.Text)
  2. intlabel2 = Val(Me.Label2.Text)
  3.  
Regards
Veena
Mar 10 '08 #5
Killer42
8,435 Expert 8TB
Assuming it's still available in your version (I use VB6) I'd recommend using the Masked Edit control rather than a textbox. It allows much better control over what is input.
Mar 10 '08 #6
kadghar
1,295 Expert 1GB
what i've found easier is using CType(Var, Type) to make all the type changes you need while using Option Strict (yeah, the easy way out).
Mar 11 '08 #7
Killer42
8,435 Expert 8TB
Generally speaking though, I think it's better practice to restrict user input to what is acceptable, rather than waiting until they've entered something then complaining about it.
Mar 11 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nikola | last post by:
Hi all, I have a problem converting datetime to integer (and than back to datetime). Depending whether the time is AM or PM, same date is converted to two different integer representations,...
4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
2
by: Asbjørn Ulsberg | last post by:
Hi. I'm trying to convert Brady Hegberg's great RTF2HTML VB 6.0 module to C#. I've managed to convert the VB code to VB.NET, which gave me the following code: Option Strict On Option...
0
by: Mark Allen | last post by:
Hello, I am creating an RTF document server side for a report. However I am having problems converting images into the required RTF format. I am converting the image into a string (binary)...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
2
by: Alex Buell | last post by:
Is there an elegant way of converting strings containing digits between different number bases in C++? I.e.: 10 (base 2) = 2 (base 10) FF (base 16) = 256 (base 10) F (base 16) = 1111 (base 2)...
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
0
by: ling2000 | last post by:
Hello all, I'm trying to upgrade a VB6 project into VB.net, and the problem I had is in converting 'address of' to 'delegate'. I had the error "Value of type 'DelegateIDccManSink_OnLogIpAddr'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.