Am using An inputbox in my form.
If i Press OK the code is fine.
When i Press cancel the Program displayin an error. Wat should i Do ?
You should look for an empty string
Dim MyString As String
MyString = InputBox("Please enter something", "Request Info", Nothing)
If (MyString Is Nothing OrElse MyString = "") Then
'User hit cancel
Else
'Read MyString
End If
Change the code that is after the InputBox to support empty strings. InputBox will return an empty string if you cancel so the reason for the error must be that your code expects the string to have a lenght > 0.
If you edit the question to show the code that calls InputBox as well as a few lines following that line, someone can probably point out the exact error.
See the documentation for a working sample:
Interaction.InputBox Method
Related
Replace("",vbLf, "")
Go figure.
It should return ""
No. It returns nothing.
Just put the code in vb.net
I think it should return "". Replace all occurance of vbLF with "". Because the original string is "" then it simply replace nothing and we got back ""
\No. We got back nothing.
You are using Visual Basic string functions, not .Net. The Visual Basic runtime usually evaluates Nothing as an empty string ("").
I second the original post, VB.net should not return NOTHING with its REPLACE function. However, it does if your replace happens to return Nothing if the expression is an empty string.
To elaborate on this for anyone winding up here, when you just call "Replace" you're getting Microsoft.VisualBasic.Replace, not the String.Replace you're expecting.
Microsoft.VisualBasic.Replace: https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic?view=netframework-4.7.2
String.Replace: https://learn.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.7.2
If you want to return an empty string you need to call the .Replace method of a String:
dim myString as String = ""
myString.Replace("a", "b")
This of course finds no match, but the behavior is instead to return and empty string now instead of Nothing when the input expression is an empty String.
I have a form that has a textbox (say TextBox1). This field is of string type.
On clicking on a button, I have the below code
Dim field1 As String
If (TextBox1.Text) Then field1 = TextBox1.Text Else MsgBox("TextBox1 Code can not be empty. Enter proper value!", vbCritical, "Empty TextBox1")
I built the solution and ran it. When the form is opened, I did not enter anything in TextBox1. I clicked on the button. It throws an exception as below:
InvalidCastException was unhandled
Conversion from string "" to type 'Boolean' is not valid.
Can anyone guide as to how can I handle this exception? Also, why is it trying to convert my string to a Bool anyway?
The problem is this part:
If (TextBox1.Text)
It's trying to convert TextBox1.Text to Boolean to see whether or not to go into the block. You might have meant:
If (TextBox1.Text <> "")
When I run my program and get it to give me the value (Yes, I do have the items in the drop down list selected) in the combo-box all I get is this,
System.Windows.Forms.ComboBox+ObjectCollection
This is the code I am using
Dim name As String
name = cmbworld.Text
MsgBox(name)
Any ideas?
P.S. The code I used to insert the values is
cmbworld.Items.Clear()
If File.Exists(root + "\setting\world.txt") Then
For Each line As String In File.ReadLines(root + "\setting\world.txt")
If line.Length <> 0 Then
cmbworld.Items.Add(line)
End If
Next line
Else
This code would reproduce the problem:
Dim name As String
name = cmbworld.Items.ToString()
MsgBox(name)
You have some other code somewhere that is assigning the value of the Text property incorrectly. You need to index the Items collection. For example:
cmbworld.Text = cmbWorld.Items(0)
The code you are posting can't be what is not working in your code.
Taking your example, I get a clean message with a line of data from my text file.
The only way I get your message is when I do the following:
MessageBox.Show(cmbworld.Items.ToString)
I would put a stop debugger on that MsgBox line and check the values.
You are using the wrong property, use SelectedText.
cmbworld.SelectedText
I am using an input box. In tat am getting the number as input. When the user presses OK the program is workin. But when I press the cancel button am getting a error message
" Conversion from String "" to 'Integer' type is not valid "
If i press cancel I need the program to end.
and also i want to know how to move to other form when i press cancel in input box
Its probably a good idea to use try parse for these situations it handles more cases than empty strings for example non numeric characters characters
Dim number As Integer
Dim result As Boolean = Int32.TryParse(inputBox.Text, number)
if Not result Then
number = 0
End If
If user didn't enter anything in the input box and press cancel, it will be an empty string. Your system won't be able to convert an empty string to integer. Therefore, your program should handle this scenario with code similar to below.
If inputBox.Text = "" Then
inputValue = 0
Else
inputValue = inputBox.Text
I'm making a SIMPLE, and when I mean simple, I mean simple. But any way, I have this thing where, the person enters in a word, and it will preview a text file. I have everything setup even the text reader, but something unusual I get in my code is the "if statement". Whenever I type in the word into the textbox, it won't run the thing I want it to, instead it will go directly to the else and display the error message. This is the code I have:
If mainText.Text = "book" Then
startProcess()
ElseIf mainText.Text = "greet" Then //Just there for testing..
mainText.Text = "Hello there..."
Else
mainText.Text = "Please either enter a valid command."
End If
Now i looked into mainText.text and saw the value when entering the greet command..
{System.Windows.Forms.TextBox, Text: greet" & vbCrLf & "}
.. I don't really understand why this is happening.. All my other program work fine.
Can anyone help me solve this!??!
Thanks,
Kevin
Is the textbox multi-line? A single-line textbox shouldn't allow these characters. Either way, you can probably just trim the string.
Dim command As String = mainText.Text.Trim()
If command = "book" Then
startProcess()
ElseIf command = "greet" Then // Just there for testing..
mainText.Text = "Hello there..."
Else
mainText.Text = "Please either enter a valid command."
End If
The fact that it works when you break it sounds like a synchronization problem (multithreading) .. could be something connected to the timer you use.
Can you provide more information regarding your code/logic?