Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

listbox deselect property

Status
Not open for further replies.

lunaclover

Programmer
Jun 22, 2005
54
Hi everybody -
I am just trying to do something simple and can't find the right way to do it.

I have a listbox whose content changes depending on selections a user has made previously. I then use this selection to produce part of a model number. Sometimes the things in the listbox are strings, sometimes they are doubles. But if you have a selection made in the listbox, and you go back and change another selection you have made somewhere else, it will give you a:
[error]
An unhandled exception of type 'System.InvalidCastException' occurred in system.dll

Additional information: Cast from string "20 KW" to type 'Double' is not valid.
[/error]

So now I'm trying to make a code that if these certain items are changed after a selection in the listbox has been made, then unselect that item. Hopefully that will stop it from breaking.

I have tried
Code:
listbox.selecteditem = ""
Code:
listbox.selecteditem = nothing
Code:
listbox.selectedvalue = nothing
Code:
listbox.selecteditem = false

Sorry I am really not very experienced, how do I acheive what I am trying to do? Which property should I use? I wish there was a deselect or unselect property on this control.

Thanks for any help anyone can provide!

~Luna
 
Thanks! I knew it would be something easy. Have a great day!
 
Actually, it isn't working.. I am still getting that error.. any suggestions?

Thanks,
Luna
 
This line of code de-selects, and it works. As i see you try to do something like double.parse(listbox1.text). In case of "20 KW" ... the right thing is an exception to be thrown.

Yes, if it is a number, the parse, or cdbl or ctype(.., double) will work fine. You have to check (many ways) the type of the listbox1.text (currently selected item's text). If a number then convert it to double, else (string) do something else.

One way without ifs (not very very good) is:

try
dim x as double=double.parse(listbox1.text)

' go on
catch ex as exception
' if there is error then I know the it was a string
finally
' perhaps do something here
end try


:)
 
Hopefully this will point you in the right direction:

Code:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim s As String = "Fred"
    Dim i As Integer = 1234
    Dim d As Double = 1.234
    Dim dt As Date = CDate("12/01/2006")

    With ListBox1.Items
      .Add(s)
      .Add(i)
      .Add(d)
      .Add(dt)
    End With

  End Sub

  Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

    Dim typ As String
    If ListBox1.SelectedIndex >= 0 Then
      typ = ListBox1.Items(ListBox1.SelectedIndex).GetType.ToString
      MessageBox.Show("System Type is: " + typ)
      Select Case typ
        Case "System.String"
          'Do something relating to a string value"
          MessageBox.Show("String processed")
        Case "System.Int32"
          'Do something relating to an int32 value"
          MessageBox.Show("Integer processed")
        Case "System.Double"
          'Do something relating to a double value"
          MessageBox.Show("Double processed")
        Case "System.DateTime"
          'Do something relating to a date value"
          MessageBox.Show("Date processed")
        Case Else
          'Not an expected type
          MessageBox.Show("Shouldn't get here")
      End Select
    Else
      MessageBox.Show("Nothing is selected")
    End If

  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    ListBox1.SelectedIndex = -1

  End Sub


Hope this helps.

[vampire][bat]
 
I have not had a chance to try this yet, been crazy busy here, but I will and once I do I will let you know. Thanks so much in advance earthandfire and tipgiver!

Luna
 
Hi earthandfire, sorry for the delay in getting back to you.. I have finally tried this code, (well, most of it) and I think I can do beautiful things with it. The case statement wouldn't really pertain to what I am trying to do, so I didn't use it.

But - what I need to do, is if an item in the listbox has been selected, this code (listbox.selectedindex = -1) needs to be called IF:

* the user chooses another item where there is already a choice (radiobutton) selected in a combobox of about 8 radiobuttons.

I was looking for something like groupbox2.enter or groupbox2.mouseenter or something that basically says if this groupbox is entered (or really if any radiobutton within it is selected when (1) one is already selected or (2) a listbox selection is made) then unselect the listbox selection using the aforementioned code.

However, these events (enter and mouseenter) don't exist with a groupbox. Is there a way I can accomplish this with a groupbox? Or how can I make it inherit these events so I can use them? Can I do this at all?

Thanks again for your help in advance.
Luna
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top