RudeYute Posted December 15, 2004 Posted December 15, 2004 Hi, I have created a listbox, with a few buttons inserting items into the listbox. Items Inserted are: "Up", "Down" and "Normal" Now, what I am trying to do is, have another button, when clicked, it scrolls through the listbox, and if it's "Up" in the list, then does something, if "Down" then something Else, and if "Normal" then something else still. The code I think which should work is: While lbList.Items.Item("Up") Do PictureBox.Left += 1 Loop End While The code seems perfect, but when I run the program, I get the following error: "Cast from string "Up" to type 'Integer' is not valid." I don't get why that error comes up, as I haven't declared "Up" as there's no need to. Can anyone help me with this please? I have also tried If statements in there too. Thanks Ps, sorry if this is in the wrong forum - I'm new to VB and so werent too which forum to put it in, but seems relevent. Quote
coldfusion244 Posted December 15, 2004 Posted December 15, 2004 Hi, I have created a listbox, with a few buttons inserting items into the listbox. Items Inserted are: "Up", "Down" and "Normal" Now, what I am trying to do is, have another button, when clicked, it scrolls through the listbox, and if it's "Up" in the list, then does something, if "Down" then something Else, and if "Normal" then something else still. The code I think which should work is: While lbList.Items.Item("Up") Do PictureBox.Left += 1 Loop End While The code seems perfect, but when I run the program, I get the following error: "Cast from string "Up" to type 'Integer' is not valid." I don't get why that error comes up, as I haven't declared "Up" as there's no need to. Can anyone help me with this please? I have also tried If statements in there too. Thanks Ps, sorry if this is in the wrong forum - I'm new to VB and so werent too which forum to put it in, but seems relevent. It's giving you the error because "UP" is a string, whereas in the Items.Item() procedure it wants an integer and cannot implicitly convert that string to an integer. I would go about it like this : private void button1_Click(object sender, System.EventArgs e) { foreach(Object oItem in lb1.Items) { switch(oItem.ToString()) { case "UP": lb2.Items.Add("UP FOUND"); break; case "DOWN": lb2.Items.Add("DOWN FOUND"); break; case "NORMAL": lb2.Items.Add("NORMAL FOUND"); break; } } } Quote -Sean
coldfusion244 Posted December 15, 2004 Posted December 15, 2004 I think the easiest way that you could do it in VB is to use IEnumerator and use listbox.Items.GetEnumerator() and set your's equal to it. Then say While(myEnumerator.MoveNext) Object oItem = myEnumerator.GetCurrent() End While something like that, i'm not sure of VB code. Quote -Sean
RudeYute Posted December 15, 2004 Author Posted December 15, 2004 Hi, Thanks for your reply. If the problem is the "up" word, then If I change it to something like "increase" it should sort the problem right? Would the way I first done it work, if I was to change the text? I don't want to get too advanced on it (hence why I'm sticking to what I know), as the code has to be shown for the work. Thanks Quote
techmanbd Posted December 15, 2004 Posted December 15, 2004 If the problem is the "up" word, then If I change it to something like "increase" it should sort the problem right? No, because you are still putting in a string. It needs to be an integer. Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
coldfusion244 Posted December 15, 2004 Posted December 15, 2004 No' date=' because you are still putting in a string. It needs to be an integer.[/quote'] "UP" is a string, it wants an integer like 0,1,2,3,4,5,6 (unsigned) -1,-2,0,1,2 (signed). Quote -Sean
RudeYute Posted December 15, 2004 Author Posted December 15, 2004 Hmm, now I'm stuck for ideas of what to do. I don't want to use any of the above two methods mentioned by you, as I clearly am not that advanced with vb.:( Thanks Quote
mhsueh001 Posted December 15, 2004 Posted December 15, 2004 (edited) Hi RudeYute, First off, either your explaination is not correct or your code is not correct because the logic of the two don't match up. So I will do a best guess at explaining what I think you want, and you can correct me if I'm wrong. 1. I believe you're saying you've got a list box with a list of items in it. (I'm assuming these's aren't buttons as you've indicated) 2. You want an external button, when pressed, to analyze each of the items in the list box and perform an action if that item exists in the listbox. (this is for all items, and not just the selected one in the list box???) What you need to do is go through the list of all the items. Dim intDummyCounter as integer intDummyCounter = 0 'As long as there are items in the list box perform an action Do while intDummyCounter < Me.ListBox1.Items.Count If me.ListBox1.Items(intDummyCounter) = "Up" Then 'Perform the action for the "Up" ElseIf me.ListBox1.Items(intDummyCounter) = "Down" Then 'Perform the action for the "Down" '... etcetra End If 'Increment to the next item in the listbox intDummyCounter = intDummyCounter + 1 'Or simply intDummyCounter += 1 Loop If you only want the selected items you can loop through the SelectedItems list instead of the full list. (I placed ???? in my statement above because I believe this is what you really want) ie. Do while intDummyCounter < Me.ListBox1.SelectedItems.Count 'Do the check on the selected Items If me.ListBox1.SelectedItems.Items(intDummyCounter) = "Up" Then '... etcetra End If Loop Edited December 15, 2004 by mhsueh001 Quote
RudeYute Posted December 15, 2004 Author Posted December 15, 2004 Hi mhsueh001, 1 - Correct, but I have buttons which Add these items into the listbox. Ie, I press one button and "up" is inserted, another button then "normal" is inserted". As third button adds "down". 2 - The list box will contain them 3 texts upto 20 times in random orders (depending on users clicks). I want the final button click to go through the list items one by one and do certain things depending on each item. Quote
coldfusion244 Posted December 16, 2004 Posted December 16, 2004 Hi mhsueh001, 1 - Correct, but I have buttons which Add these items into the listbox. Ie, I press one button and "up" is inserted, another button then "normal" is inserted". As third button adds "down". 2 - The list box will contain them 3 texts upto 20 times in random orders (depending on users clicks). I want the final button click to go through the list items one by one and do certain things depending on each item. Yes, the code that mhsueh001 so nicely wrote in VB for you should do the trick. Quote -Sean
mhsueh001 Posted December 16, 2004 Posted December 16, 2004 Hi RudeYute 1 - Correct, but I have buttons which Add these items into the listbox. Ie, I press one button and "up" is inserted, another button then "normal" is inserted". As third button adds "down". If this is the case, then my first piece of code should do the trick. It goes through each item in the list box and performs the action it finds. 2 - The list box will contain them 3 texts upto 20 times in random orders (depending on users clicks). I want the final button click to go through the list items one by one and do certain things depending on each item. It doesn't matter what what is in the list box. The code I wrote basically starts from the top of the list box and moves down through the list. Each time it goes through and finds the action that it must perform through the if...then statement. What you must do is code an action that is added to the list. If your list has Up, down, left, right. Then you must have a corresponding if...then for each of the actions. If you don't have an if...then statement for that action, that action will be ignored. Basically my first code listing does this. 1. Step through each item in the listbox. (do while ...) 2. Analyze the action that we are asked to perform (if...then) 3. Perform that action (the block within the if then ... end if) 4. Retrieve the next item on the list and go back up to 1. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.