-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
Valhalla's Wrath
Also that btnupdate sub should be called btnsave or something. I want changes to stay without pressing a button if possible?
Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Remove the handles from this?
When do you want the changes to stay, because after you remove the handler you'll have to use AddHandler to make it change the textbox values when you select items in the ListBox again.
I showed you how here: http://www.vbforums.com/showthread.p...=1#post4219861
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Well I really just want the changes in the text boxes to update in the array and stay there once text is changed. could that be done under a sub with a text changed handle? How?
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
You want to update them in the array? What array? We're reading from a file here, and parsing our own Array object from the values in the file. I think that's the misunderstanding here.
If you want to keep and use an array with the elements, you'll have to create a member variable, as all these local variables which are being used to store the split() string values and all that within Subs aren't going to be 'remembered' once that Sub's done doing what it needs to do.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Can't get the code working right now. It's not liking file.writealltext or file.readalllines for some reason.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
Valhalla's Wrath
Can't get the code working right now. It's not liking file.writealltext or file.readalllines for some reason.
You have to be more specific so I can help. Why isn't it working?
What error does it give you? And depending on the error try using breakpoints to see what's going on..
-
1 Attachment(s)
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I have to say a big thank you to every ones help. You have both helped me so much elieICT and Ace that it is amazing. You have also been so patient with me for which I am very appreciative of. Also I have to say a thank you to everyone who has even been bothered to read everything I have to say even if they couldn't help.
However I just have one very, very, very last problem. (I got a 1 day extension to fix up everything in this)
I couldn't get that last bit of code you pasted working. I can't remember the exact errors now but I didn't really understand everything it was doing. However I managed to make my own sort of work around which will allow me to edit item details in the text boxes and keeps them changed after the item has been deselected but doesn't add the item to a text file until I click save. Which is what I wanted it to do. The only problem is, while this works for every other text box. It does not work for my text box holding item names.
The code for which I am using is
Code:
Private Sub txt1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txt1.TextChanged
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(0) = txt1.Text
Search("")
End Sub
The code for all my other textchanged events on other text boxes is the same but for some reason or another it just doesn't like it when I have this in there. It crashes with the error "Index was outside the bounds of the array." and the line " stockArray(itemIndex)(1) = txt2.Text" highlighted in another sub.
Now I know all of that could probably be done with LINQ or how you've been trying to explain things to me. But can we make it work this way just this once.
I'll attach the current version of my program in a second.
Edit: Here it is. I can't remember if I did or didn't 'comment' the last sub of code "txt1_TextChanged" but when this is active that is when problems start occurring.
Attachment 90711
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I really am sorry for all of these consistent request for help I have been having. But getting this to work is something really important to me. In half an hour I need to go to sleep and if I can't work this one out I'll only have about 45 minutes to look at it again (6 hours from now) tomorrow before times up.
If anyone could give me one last push and possibly not confuse me with to much new or altered code they would literally be a life saver. I hate to admit it but at the moment I am feeling kind of desperate and very anxious that I won't be able to resolve this.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Ok well I'm almost using changing my name values upsets this now.
Code:
Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
As it is getting highlighted in yellow and gives this error "Object variable or With block variable not set."
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
"Index was outside the bounds of the array." and the line " stockArray(itemIndex)(1) = txt2.Text"
I tried to explain this before...
Change the event handler for your textboxes to
..............................................................
Code:
"Object variable or With block variable not set."
is probably due to a change elsewhere..
Only time I could reproduce that was if the listbox was empty..
the problem I'm sure is not in that line of code itself.. But rather it's hanging up on that line because its trying to do something, and one of the components it relies on is missing (like when i had no items in the list box)
on a separate note.. I noticed the sub ListBox1_SelectedIndexChanged will throw an out of bounds exception if you click the listbox where there is no item. easy fix...
first line of code could check for a non-item index. So:
Code:
If sender.SelectedIndex < 1 Then Exit Sub
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
So editing the name field works fine for you with that?
I'll give it a go. But I only get one last chance to make things work once I go to submit.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
Valhalla's Wrath
I have to say a big thank you to every ones help. You have both helped me so much elieICT and Ace that it is amazing. You have also been so patient with me for which I am very appreciative of. Also I have to say a thank you to everyone who has even been bothered to read everything I have to say even if they couldn't help.
However I just have one very, very, very last problem. (I got a 1 day extension to fix up everything in this)
I couldn't get that last bit of code you pasted working. I can't remember the exact errors now but I didn't really understand everything it was doing. However I managed to make my own sort of work around which will allow me to edit item details in the text boxes and keeps them changed after the item has been deselected but doesn't add the item to a text file until I click save. Which is what I wanted it to do. The only problem is, while this works for every other text box. It does not work for my text box holding item names.
The code for which I am using is
Code:
Private Sub txt1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txt1.TextChanged
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(0) = txt1.Text
Search("")
End Sub
The code for all my other textchanged events on other text boxes is the same but for some reason or another it just doesn't like it when I have this in there. It crashes with the error "Index was outside the bounds of the array." and the line " stockArray(itemIndex)(1) = txt2.Text" highlighted in another sub.
Now I know all of that could probably be done with LINQ or how you've been trying to explain things to me. But can we make it work this way just this once.
I'll attach the current version of my program in a second.
Edit: Here it is. I can't remember if I did or didn't 'comment' the last sub of code "txt1_TextChanged" but when this is active that is when problems start occurring.
Attachment 90711
If you can't deal with the RemoveHandler and AddHandler or that is too much of a hassle, the EASIEST way to work around this would be to use a Button Click event to put the Selected ListBox Item values into the TextBoxes. That way, when you select a new item, the values don't change. It's based on something more significant and more purposely initiated when you use a button instead of the selectedindex changed event...
Please, do not use the Text_Changed event for this though... I wouldn't recommend doing that. My reasoning for that is because for every text change in the textbox, that event fires, even if you don't mean to have it run through that Sub's code. If you are happy with it though (due to the limited time you have especially taken into consideration), then it'll do, but you need to first make sure that it's all built solid, because with an event like that being used to execute certain code, sometimes there's huge possibilities of bugs that can result; in the form of many Exceptions, and undefined behaviors.
But as elielCT mentioned, if you don't have anything selected in the ListBox, the selectedindex will return -1. Although there is an error in what he's suggested to you (just to point this out):
Code:
If sender.SelectedIndex < 1 Then Exit Sub
0 is less than one, BUT, is still a valid index.
Use < 0 instead. Also what I see there is some implicit casting. sender is an Object, but in reality, it's an Object that represents something else usually.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
LMGDAO!!!
Nice Catch!!
dont know how i missed that... smh
>>>>
I suspect the TextChanged event handler may very well be causing the other error..
Hes pacified it, but its still being fired.. he can have a pseudo onTextChanged event fire with the LostFocus... Or with a button.. Which does seem to be the correct way of updating something like this.. I did mention all this before...
________________
<offTopic>I've been reading into those lambda functions... Pretty interesting.. Already started implementing some on a project i've been working on... </offTopic>
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
LMGDAO!!!
Nice Catch!!
dont know how i missed that... smh
>>>>
I suspect the TextChanged event handler may very well be causing the other error..
Hes pacified it, but its still being fired.. he can have a pseudo onTextChanged event fire with the LostFocus... Or with a button.. Which does seem to be the correct way of updating something like this.. I did mention all this before...
________________
<offTopic>I've been reading into those lambda functions... Pretty interesting.. Already started implementing some on a project i've been working on... </offTopic>
I would be in agreement with a button, but that's my opinion :)
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
i've never ran into an issue referencing sender or e... If there is something I should be aware of please share..
Code:
Private Sub CCCToolStipItems_Click(sender As System.Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
The way it seems to me, the procedure is handling an event fired by an item being clicked in the toolstrip.. So sender is always going to be the object ToolStrip1.. The event is always going to be a toolstripitemevent argument belonging to toolstrip1 items...
these items have properties, just as sender does.. we know who and what the sender is... i'd understand the error in using these parameters outside of a controlled event handler, where the typeof sender isn't guaranteed... in which case a good description of what objects are valid objects to be passed is in order...
in this scenario this event has a property of .ClickedItem...
What is your logic in being against this?
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
i've never ran into an issue referencing sender or e... If there is something I should be aware of please share..
Code:
Private Sub CCCToolStipItems_Click(sender As System.Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
The way it seems to me, the procedure is handling an event fired by an item being clicked in the toolstrip.. So sender is always going to be the object ToolStrip1.. The event is always going to be a toolstripitemevent argument belonging to toolstrip1 items...
these items have properties, just as sender does.. we know who and what the sender is... i'd understand the error in using these parameters outside of a controlled event handler, where the typeof sender isn't guaranteed... in which case a good description of what objects are valid objects to be passed is in order...
in this scenario this event has a property of .ClickedItem...
What is your logic in being against this?
Code:
sender As System.Object
sender is a type of System.Object. It doesn't have a property of .ClickedItem because Object doesn't have those properties. Turn on Option Strict and see for yourself :) It IS an Object by your example that represents a ToolStrip, but that's not the current type at runtime. What you're doing is a phenomena called unboxing (implicitly).
System.Object does not have those properties: http://msdn.microsoft.com/en-us/libr...em.object.aspx
ToolStripItem does.
Example for a Button_Click:
vbnet Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(sender.GetType().Name)
End Sub
It won't show any form of System.Windows.Forms.Control type. And you won't be able to reference the Button's Text property or anything, because System.Object doesn't have those properties.
You have to cast it:
vbnet Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
DirectCast(sender, Button).Text = "value"
End Sub
vbnet Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(DirectCast(sender, Button).GetType().Name)
End Sub
~Ace
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
or are you saying as good practice goes, sender should be directly cast into a type toolstrip before using his methods or properties?
If --- that is the case, directly declaring toolstrip type in the parameter would seem to make more sense..
??
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
oh ok... (just to correct u -> it was e that had those properties, but its because that wasnt declared as a generic type_).... I do catch ur drift, and I did notice sender doesn't have those available until runtime...
I should've caught that bad habit.. It makes perfect sense...
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
or are you saying as good practice goes, sender should be directly cast into a type toolstrip before using his methods or properties?
If --- that is the case, directly declaring toolstrip type in the parameter would seem to make more sense..
??
No, not really because then it wouldn't have a signature that would properly match the delegate sub for an EventHandler...
Quote:
Originally Posted by
elielCT
oh ok... (just to correct u -> it was e that had those properties, but its because that wasnt declared as a generic type_).... I do catch ur drift, and I did notice sender doesn't have those available until runtime...
I should've caught that bad habit.. It makes perfect sense...
Option Strict On at the top of every project source code you write will straighten out all bad habits with improper or implicit type casting (hopefully). :)
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
elielCT
I tried to explain this before...
Change the event handler for your textboxes
to
LostFocus is a far better idea. Requires less hacks and is very easy to implement given you don't have time for a rewrite.
So, with your code from post#67:
First, remove TextBox2 through TextBox10 from the designer and remove all references to them from the User form code. Also remove all the .Hide and .Show code.
Next, you have an error in your ListBox1_SelectedIndexChanged sub. You have the End If in the wrong place. It should be where you have the commented out End If. Also, get rid of the Exit Sub.
vb.net Code:
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex > -1 Then
Dim stock As String() = stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
For i As Integer = 0 To stock.Length - 1
Me.Controls("Txt" & (i + 1).ToString).Text = stock(i)
Next
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
txtreorderamount.Text = IIf(stockArray(itemIndex)(5) - stockArray(itemIndex)(2) < 0, 0, (stockArray(itemIndex)(5) - stockArray(itemIndex)(2)))
If stockArray(itemIndex)(6).ToString.ToLower.Contains("dangerous") Then
panalerts.BackColor = Color.Red
lblAlerts.Text = "Dangerous"
Else
panalerts.BackColor = Color.Green
lblAlerts.Text = "Safe"
End If
End If
End Sub
For textboxes txt2 through txt8 and txtreorderamount, switch from handling the TextChanged event to handling the LostFocus event as suggested by elielCT, e.g.
vb.net Code:
Private Sub txt2_LostFocus(sender As System.Object, e As System.EventArgs) Handles txt2.LostFocus
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(1) = txt2.Text
End Sub
Similarly for txt1 textbox (the Name textbox), but also rewrite the value to the ListBox, otherwise the stock array and the listbox will be out of synch, your code will no longer find the listbox item in the (updated) stock array, and you'll get the errors you have been seeing:
vb.net Code:
Private Sub txt1_LostFocus(sender As System.Object, e As System.EventArgs) Handles txt1.LostFocus
Dim listBox1Item As String = ListBox1.SelectedItem
Dim itemIndex As Integer = getItemArrayIndex(listBox1Item)
stockArray(itemIndex)(0) = txt1.Text
ListBox1.Items(ListBox1.SelectedIndex) = txt1.Text
End Sub
There is lots of room for improvement (depending on how much time you have left) such as the use of buttons to update the array from the values in the textboxes as mentioned by both elielCT and AceInfinity.
You could reduce the amount of repeated code if you so wished, and also give your subs decent names (e.g. NameText_LostFocus instead of txt1_LostFocus and btnAppFilter_Click instead of Button1_Click). Start all your sub and function names with a capital letter (Pascal casing) or at least be consistent.
A few minor observations:
You don't close and dispose the StreamReader object in sub readFile (you could use a Using reader As... End Using block instead of the Dim, and then you don't need to worry about the closing/disposing of the StreamReader).
You should clear the textboxes, the restock label and the alerts label just after the call to ListBox1.Items.Clear() in your Search sub . At the moment, filtering repopulates the listbox, doesn't select an item in it, but leaves the textboxes populated from any previously selected listbox item.
Two of your labels display "Null" when the app is first started.
Clicking the "Apply" button while the search textbox still contains "Touch Here To Search" gave an unexpected result. You could disable the button until that message has been removed.
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I don't feel like cluttering up this post with quotting what Inferrd mentioned, but as for the StreamReader and even inclusive of StreamWriter, If you don't want to manually deal with Close/Dispose, then that's a good time to be using the Using statement :)
I find it makes things a lot cleaner in most cases.
~Ace
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Everything works like a charm. :bigyello:
I didn't have time to clean up some things such as the hiding of the text boxes. But that doesn't cause issues.
I do have to say a massive thank you to everyone that has helped. You have all been so patient and have taken the time to explain so many things to me. In fact I've already started to share some of that information around such as the difference between jagged arrays and multidimensional ones. I learnt off of them that what I was using was a multidimensional array and learnt how to use one and they were quite surprised to learn that they have been mistaken for so long also.
Thank You!!! :)
-
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Quote:
Originally Posted by
Valhalla's Wrath
Everything works like a charm. :bigyello:
I didn't have time to clean up some things such as the hiding of the text boxes. But that doesn't cause issues.
I do have to say a massive thank you to everyone that has helped. You have all been so patient and have taken the time to explain so many things to me. In fact I've already started to share some of that information around such as the difference between jagged arrays and multidimensional ones. I learnt off of them that what I was using was a multidimensional array and learnt how to use one and they were quite surprised to learn that they have been mistaken for so long also.
Thank You!!! :)
Well it's good that the explanation was given! No more misunderstandings :)
This is what I like to hear from people that I try to help out. It seems you're progressing, and if the interest is there, you can continue to progress from the things you've learned just from this one project.
Cheers