|
-
Aug 19th, 2012, 04:16 PM
#41
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 Originally Posted by AceInfinity
Me.Controls("TextBox" & (i + 1).ToString).Text = stock(i)
I was going to suggest something like that.. but using a tag for the textbox corresponding to its place in the array....
I usually try to name my controls something that describes its purpose...
-
Aug 19th, 2012, 04:18 PM
#42
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 Originally Posted by Valhalla's Wrath
Could you please. The ".fists(function(objarr) objarr(0)."... Part in particular.
How could I use this to say for instance an item in an array is now equal to one from a textbox?
Edit: Thanks!
Okay... Here we go:
vbnet Code:
stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
First you need to understand that stockArray is what we call a "jagged array". This means it is an array of an arrays.
So each element in stockArray is an array: The array returned by the Split() function you used on each line, which returns an array of strings.
Example: Inside stockArray(0) which is the very first element of stockArray, we have a string array. This string array is the first line split by "|" which is an array that the Split function returns. Hope this is clear.
Now... When we query data from stockArray, we're essentially looking at each string array value (the strings split by "|" for each line as a string array element) inside stockArray at first.
The First keyword I have there, means we're trying to find the First something based on a condition we're using.
So example: Say we have a class full of students. There is only 1 girl. If our condition is to find the First girl, then that's what we're doing here essentially, only comparing something with the ListBox.SelectedItem as a string value.
Function(objArr) - Where objArr is basically like our temporary variable name (it can be any name you want). Holds THE value of the String array in question; THE string returned from the Split() function for a particular line that you split by ("|"). Right now it doesn't matter which line, just know that we're evaluating SOME line from stockArray at this point in time.
Now from that String array in question (represented by objArr), which holds a collection of strings separated by "|", we're referencing the first element of that string array when we use objArr(0), and if it EQUALS ListBox1.SelectedItem.ToString. Then we know we found the particular split string array, which holds the strings we want to use in the TextBoxes.
(Tried my best to explain it.)
How could I use this to say for instance an item in an array is now equal to one from a textbox?
Easy, but which array do you want to use here? Because remember stockArray is an array of an array, we need to reference at least one string array from INSIDE stockArray to compare with the values from the TextBox.
 Originally Posted by elielCT
I was going to suggest something like that.. but using a tag for the textbox corresponding to its place in the array....
I usually try to name my controls something that describes its purpose...
That's VERY good It's a nice habit in my opinion. Because if you have lots of controls, things get convoluted fast.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 04:45 PM
#43
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
AceInfinity
your way of finding the selected item does work pretty good... alot less code and no loop...
:Thumbs up:
-
Aug 19th, 2012, 05:25 PM
#44
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 Originally Posted by AceInfinity
Okay... Here we go:
vbnet Code:
stockArray.First(Function(objArr) objArr(0) = ListBox1.SelectedItem.ToString)
Easy, but which array do you want to use here? Because remember stockArray is an array of an array, we need to reference at least one string array from INSIDE stockArray to compare with the values from the textbox
Is a 'jagged array' what you would call a multidimensional array. In this case two dimensonal?
For to referance somehing such as a name in a textbox I've been using Stockarray('meant to be increasing index')(0) to referance the first item from the split item on each lin e that corresponds to the index. So in all cases pretty much I reer to the first array as a varying integer and the second as the position in the second array of what I wish to change. I hope that's what you wanted to know.
I will also try out this code as soon as I can and post back with results. You explanation was very easy to understand. Thank You!
-
Aug 19th, 2012, 05:51 PM
#45
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 Originally Posted by elielCT
AceInfinity
your way of finding the selected item does work pretty good... alot less code and no loop...
:Thumbs up:
Thankyou elielCT! 
 Originally Posted by Valhalla's Wrath
Is a 'jagged array' what you would call a multidimensional array. In this case two dimensonal?
For to referance somehing such as a name in a textbox I've been using Stockarray('meant to be increasing index')(0) to referance the first item from the split item on each lin  e that corresponds to the index. So in all cases pretty much I reer to the first array as a varying integer and the second as the position in the second array of what I wish to change. I hope that's what you wanted to know.
I will also try out this code as soon as I can and post back with results. You explanation was very easy to understand. Thank You!
No, a jagged array is NOT a multi-dimentional array. A jagged array is an array of an array. A Multidimentional array is not that.
For the demonstrations below, each pair of "{" "}" represent an array. When I put "ARRAY" I mean the element is an actual Array of elements.
Jagged Array Structure:
Code:
{{ARRAY}, {ARRAY}, {ARRAY}}
Multi-Dimentional Array Example:
Code:
{{{Object, Object}, {Object, Object}}, {{Object, Object}, {Object, Object}}
Multi-Dimentional array's are declared like so, for instance:
Code:
Dim arr As String(,,)
Jagged arrays are declared differently:
Code:
Dim arr As String()()
For to referance somehing such as a name in a textbox I've been using Stockarray('meant to be increasing index')(0) to referance the first item from the split item on each line that corresponds to the index. So in all cases pretty much I reer to the first array as a varying integer and the second as the position in the second array of what I wish to change. I hope that's what you wanted to know.
This is what my LINQ does with no loop. I knew this is what you wanted to do because this is what that LINQ does.
Last edited by AceInfinity; Aug 19th, 2012 at 05:54 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 09:49 PM
#46
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Can't seem to implement the code properly. Any I think I've just forgotten everything. Can someone tell me again where I should put this code and what it's replacing in my existing code so that everything works.
-
Aug 19th, 2012, 09:50 PM
#47
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
vbnet 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("TextBox" & (i + 1).ToString).Text = stock(i) Next End If End Sub
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 09:56 PM
#48
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
interesting.. when i first heard of LINQ i had assumed it was solely as a new way to query a db... So i wasnt interested at the moment & figured i'd get to it at some point..
So i made it a point it was definitely time...
Very interesting... A query all language......
-
Aug 19th, 2012, 10:09 PM
#49
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Ok but once I change an item after editing something my edit will not stay there. I change text and it doesn't stay changed.
-
Aug 19th, 2012, 10:17 PM
#50
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
oh i meant to tell u.. uR using the ontextchanged method..
that is being executed whenever the form is loaded, and you type a character into the textbox..
try the lostfocus method..
it seemed to be working fine on my end...
in the original file you sent me tags was index number 8.. in ur new code it's 6...
did you change it?
-
Aug 19th, 2012, 10:20 PM
#51
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 Originally Posted by elielCT
interesting.. when i first heard of LINQ i had assumed it was solely as a new way to query a db... So i wasnt interested at the moment & figured i'd get to it at some point..
So i made it a point it was definitely time...
Very interesting... A query all language......
I enjoy it, and it is very useful and fast for performance as well. I've come up with some pretty monsterous LINQueries from time to time.
Take a look at this example: http://www.vbforums.com/showthread.p...=1#post4199510
If you follow along with the thread, you'll see what that LINQ does, and I think it's pretty impressive the kind of things you can do with LINQ with a bit of creativity.
 Originally Posted by Valhalla's Wrath
Ok but once I change an item after editing something my edit will not stay there. I change text and it doesn't stay changed.
You're editing the TextBox Text values after the data is parsed into them?
It won't stay there because you haven't saved that new data to the file. That's something different though. I can help you with saving the data to the file too.
I can come up with more LINQ to do that as well unless you're tired of it already? lol
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 10:21 PM
#52
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
EDIT: No idea why my post went through twice, that's happened 2 times now just this weekend.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 10:25 PM
#53
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
i thought he meant it wasnt staying in the array... i didnt see a mention of closing the form & having the issue...
once the form is loaded it doesnt reference the actual file anymore.. just the array...
at least from what i saw.. unless i missed something...
i thought he mightve been calling the wrong index num...
-
Aug 19th, 2012, 10:26 PM
#54
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
If that is possible that would be excellent. To be honest I don't really understand the LINQ so well.
But I only have 3 hours to get this working so any help with your more efficient method is greatly appreciated.
The only thing that it really need to so is just updates the vales within the array(file?) so that they stay there stay there after they are changed and only are saved to the stock.tct file once I press save which it currently already saves. But without being able to make alterations that's kind of pointless.
-
Aug 19th, 2012, 10:39 PM
#55
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
vbnet Code:
Dim pathToFile As String = "D:\Path\input.txt" '??? Whatever your last textbox value is for the last string to be put in, put the number below Dim lastTB As Integer = 7 'Example: TextBox7 = 7 'I don't know how many textboxes you have... Dim NewLineValue = Enumerable.Range(1, lastTB).Select(Function(i) Me.Controls("TextBox" & i.ToString).Text).Aggregate(Function(t1, t2) String.Format("{0}|{1}", t1, t2)) Dim FullText As String = String.Join(Environment.NewLine, File.ReadAllLines(pathToFile).Select(Function(s) If(s.Split("|"c)(0) = ListBox1.SelectedItem.ToString, NewLineValue, s))) File.WriteAllText(pathToFile, FullText)
Here's to overwriting the file with the data from the TextBoxes on the specific line indicated by the selected item in the ListBox.
pathToFile - The path to the file you're using for the input data
LastTB - I don't know how many textboxes you have, but the number at the end of the name of the LAST textbox being used for showing the value from the line you would put that number in there for that variable.
So if I have TextBox1, TextBox2, TextBox3, TextBox4 for example, which displays the data from my original LINQ I gave you above, that value should be 4.
Last edited by AceInfinity; Aug 19th, 2012 at 10:54 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 10:49 PM
#56
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Essentially what we're doing above is this:
1) For the NewLineValue variable, we're 'selecting' the value of each TextBox's .Text value. Once that is done we have an IEnumerable(of String) object. From there, the Aggregate function combines each string in the IEnumerable(of String) collection adding the "|"s where necessary to have it in the format that the rest of your lines in that file are in.
So basically this could have also been done by doing something like:
Code:
TextBox1.Text & "|" & TextBox2.Text & "|" & TextBox3.Text & "|" & .... Ect...
So now we have the string value that we want to go into the file to replace the old one that we originally got the values from.
2) For the FullText variable, we're reading through each line in the file, and if the value of the first string element for that line being split by the "|" char is equal to the value of the text in the SelectedItem for the ListBox, instead of retrieving the line from that file, we select the NewLineValue instead, and joining this IEnumerable(of String) collection by Environment.Newline.
3) Then we write that FullText to the file, overwriting the pre-existing data, but really the only modified data was that one line.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 10:56 PM
#57
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
I already have
Code:
Private Sub btnupdate_Click(sender As System.Object, e As System.EventArgs) Handles btnsave.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("..\..\Stock.txt", False)
Dim i = 0
Dim text As String
While stockArray(i) IsNot Nothing
text = stockArray(i)(0) + "|" + stockArray(i)(1) + "|" + stockArray(i)(2) + "|" + stockArray(i)(3) + "|" + stockArray(i)(4) + "|" + stockArray(i)(5) + "|" + stockArray(i)(6) + "|" + stockArray(i)(7)
file.WriteLine(text)
i += 1
End While
file.Close()
End Sub
This to write the file. But before the file it written how can I make the data in the program stay there when I change between selected items? Unless your code actually does that as well? I will take a good look at all of this tonight, I promise. But I just need to get it working right now. :/
Also where was that code supposed to go?
-
Aug 19th, 2012, 11:11 PM
#58
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
how can I make the data in the program stay there when I change between selected items
RemoveHandler for the ListBox1.SelectedIndex changed event.
Also where was that code supposed to go?
Basically where you've just posted that code above. In the btnupdate_Click event.
If you want to not have the textbox text value's change after you save to the file, then you'll have to call to remove the handle for the SelectedIndex_Changed event after you're done writing to the file. So it would be the very last task of btnupdate_Click essentially.
vbnet Code:
RemoveHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 11:13 PM
#59
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
If you don't understand anything please ask.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 11:17 PM
#60
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
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?
-
Aug 19th, 2012, 11:20 PM
#61
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 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
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 19th, 2012, 11:43 PM
#62
Thread Starter
Member
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?
-
Aug 19th, 2012, 11:43 PM
#63
Thread Starter
Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
-
Aug 19th, 2012, 11:51 PM
#64
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 20th, 2012, 12:13 AM
#65
Thread Starter
Member
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.
-
Aug 20th, 2012, 01:17 AM
#66
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 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..
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 20th, 2012, 07:14 AM
#67
Thread Starter
Member
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.
Cannot edit name of item.zip
Last edited by Valhalla's Wrath; Aug 20th, 2012 at 09:22 AM.
-
Aug 20th, 2012, 08:31 AM
#68
Thread Starter
Member
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.
Last edited by Valhalla's Wrath; Aug 20th, 2012 at 08:49 AM.
-
Aug 20th, 2012, 03:59 PM
#69
Thread Starter
Member
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."
-
Aug 20th, 2012, 07:08 PM
#70
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
"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
-
Aug 20th, 2012, 08:09 PM
#71
Thread Starter
Member
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.
-
Aug 20th, 2012, 08:11 PM
#72
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 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.
Cannot edit name of item.zip
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.
Last edited by AceInfinity; Aug 20th, 2012 at 08:19 PM.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 20th, 2012, 08:36 PM
#73
Lively Member
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>
-
Aug 20th, 2012, 08:51 PM
#74
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 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
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 20th, 2012, 08:53 PM
#75
Lively Member
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?
-
Aug 20th, 2012, 09:06 PM
#76
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 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
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 20th, 2012, 09:06 PM
#77
Lively Member
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..
??
-
Aug 20th, 2012, 09:10 PM
#78
Lively Member
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...
-
Aug 20th, 2012, 09:21 PM
#79
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
 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...
 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).
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Aug 20th, 2012, 09:31 PM
#80
Lively Member
Re: VB 2010: Array displayed in listbox to textboxes for editing then back to array&.
Thanx.....
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|