FileListBox to ListBox To Array - arrays

I am trying to create a file picker for a system I am doing - Please note that I am uing Indusoft Web Studio that has access to various DOTNET and activex controls.
So far I have managed the following:
Using a FileListBox to display contents of a folder
Then I can Click (Or Double Click) on it to shift the values into an array (And then some script incrementing the array index)
HOWEVER - I would like to have this more visual. - I would like to display the picked files in a ListBox - However I have NO IDEA how to go about this.
So my requirements:
Create a "Click to Add" "Click to Remove" functionality - See attached JPEG.
The list box values must also be loaded into a string array. - This is becaue this array in turn is linked to a TwinCat system which must process the file names.
Please guys - any help will be greatly appreciated.!
Image showing exactly what I need

This logic will take your existing array of files and put them into a ListBox and a tag String Array. You can put this sub in the screen script and then trigger it via a MouseUp (or similar) event on the FileListBox object so it updates the ListBox automatically. Alternatively, you could put it in a command if you want to wait for the user to finish selecting files before writing out to the String array tag.
Sub UpdateListBox()
' SelectedFiles = Your existing array mentioned in Step 2 of the question
' Clear all items from the ListBox - otherwise the values will be appended and
' the list will grow each time items are added
$XRun("ListBox1", "Items.Clear")
For i = 0 To $SelectedFiles->Size
$XRun("ListBox1", "Items.Add", $SelectedFiles[i])
$StringArray[i] = $SelectedFiles[i]
Next
End Sub

Related

Creating an array of Command Buttons

I am in a situation Where I need to Create multiple Command buttons and assign a single event handler to these buttons using following Event Handler.
Private Sub Digits_Click(Index as Integer)
Display.Caption=Display.Caption+Digits(Index).Caption
End Sub
I created first button and copy paste it to the userform but it did not Prompt:
"You already have a control Name XXX. Do you want to create a control array?"
VBA doesn’t allow us to to create Control Array like in VB6 and VB.Net. My Question is Can we still create Control Array in VBA??
I am new to this Topic Please help
You might want to look at creating a loop and dynamically adding the command buttons
Every cmd should have a unique name.. It can call the same sub procedure. But every button should be unique in its own sense.

VB.Net ArrayList

I'm trying to wrap my head around array lists in vb.net. I am self teaching via the internet but can't seem to figure it all out. Some points i'm having trouble connecting the dots to:
How do i make the array list universal so it's not stuck in a subroutine and I can allow any sub to access the list.
Allowing the list to be added to or removed from from another control on the form.
Saving this array list so the program will populate the list box with it on startup.
Here is an image of the basic concept for the visual:
https://imgur.com/lBbopD8
Up to question 3, using a List(Of T) was the way to go. It may still be but certainly not completely and maybe not at all. Before the advent of the List(Of T), Microsoft recognised that storing Strings in a collection was the most common requirement so, to provide type-safety in that case, they provided the StringCollection class. You say that you want to persist your list of values between sessions so that probably means using My.Settings and it is actually possible to create a setting of type StringCollection.
I would suggest that you open the Settings page of the project properties and add a setting of type StringCollection. Once added, that list will be automatically loaded at startup and saved at shutdown, with no code required from you. You can access it anywhere in the app via My.Settings and you can call Add and Remove or index it or loop over it in exactly the same way as you would an ArrayList or List(Of String).
There is one small gotcha with a StringCollection in settings though. It will actually be Nothing by default. The trick to avoiding that is to edit its Value on the Settings page to add an item, commit that, then edit it again to remove the item. You'll see that, instead of the Value field being empty, it will then contain a snippet of XML. It's that that creates the StringCollection object in the settings file.
As I said, if you want to persist this list between sessions then I strongly recommend using settings this way. Just note that, in order to edit settings, they have to be User-scoped rather than Application-scoped. What that means is that each separate Windows user will have their own copy of the setting and thus their own value. If you only log into Windows with one account then it's of no consequence. If multiple Windows users use the app then it may be considered beneficial in most cases but may be a problem if you want universal settings that can be edited. If it's a problem, you will need to handle persistence yourself but be aware that a standard Windows user (as opposed to an admin) won't have access to write data everywhere, which is exactly why User-scoped settings work the way they do.
Also, while you must use a StringCollection for persistence in settings, you may or may not want to use the same collection in the rest of your code. You might access the collection directly all the time or you may choose to copy the collection to a List(Of String) at startup and then copy the data back at shutdown. Unless you want to avoid committing items until shutdown, I wouldn't bother with the extra collection.
So an important thing to know is that you can directly populate and edit the listbox without having an additional ArrayList. You would use the example code below as follows:
'addTb is the text box you had in the image; this will run on button press event
ListBox1.Items.Add(addTB.Text)
If you are looking to dump ArrayList data into the list box use something like this:
'creates new arraylist and adds items to it
Dim listStuff As ArrayList = New ArrayList
listStuff.Add("Hi")
listStuff.Add(2)
'makes listStuff the datasource for your list box
ListBox1.DataSource = listStuff
Finally, if you are wanting to loop through ArrayList items use something like this:
'remember to do count - 1 or you will receive error since index will be out of range
For i = 0 To listStuff.Count - 1
If listStuff.Item(i) = "" Then
'do stuff here
End If
Next
Hopefully that helps. Let me know if I need to be more clear since this is my first stack overflow answer :)
You are using a ListBox control to visualize a collection of presumably String values from a TextBox control. The ListBox exposes the visualized collection via the Items property.
How do i make the array list universal so it's not stuck in a subroutine and I can allow any sub to access the list.
Because the ListBox control resides on the Form, you can access the Items property through any access level in your Form's code.
Allowing the list to be added to or removed from from another control on the form.
From the Items property, you can use the Add method to add a single value, the AddRange method to add multiple values via an array or another ListBox collection, the Insert method to insert a value at a given index, the Remove method a specific item, and the RemoveAt method to remove an item at a given index.
So in your case, since you're presumably adding the value from the Text property of the TextBox to the ListBox, it is as simple as:
ListBox1.Items.Add(TextBox1.Text)
Saving this array list so the program will populate the list box with
it on startup.
You have a few options, but generally the idea is to is write each value in the Items property to its respective line at a given file when the application closes and then load each value back by reading each line from the same file. Another option is to use My.Settings, though I think with your level of expertise, it would probably be better to stick with the read/write to a file option so you don't have to worry about some pitfalls associated with this option. Here would be a quick example of reading/writing the items to a file:
'Write the items to the file
Dim items(ListBox1.Items.Count - 1) As String
ListBox1.Items.CopyTo(items, 0)
IO.File.WriteAllLines("file.txt", items)
'Read the items to the file
ListBox1.Items.AddRange(IO.File.ReadAllLines("file.txt"))

Vb.net Wpf Set Rectangle Fill in Listview using Binded Value

Using a rectangle or some sort of shape I'd like to display the status of a folder in a listview.
Green for if the folder is Active / Exists
And Red if the folder is inactive / Doesn't Exist
First, each item in the list view is provided using a binding for each value (Extension ID, Sent Location), and those values come from a dataset getting it's data from a local .mdf file.
Initially i thought of achieving this by using a for loop and looping through each object and determining if each folder exists or not using the System.IO.Directory.Exists command. Honestly i find this to be a bit tedious but might be the best way to go about it so far. I'm also curious if there is a way to achieve this using a data-trigger or something along those lines to make this a bit easier.

An item with the same key has already been added error

I know the error is a common one but i'm not sure how to solve this.
my scenario is this:
click button on window 1, window 2 will show (populates text boxes).
finish transaction on window 2 (save data).
click BACK button on window 2 (closes window 2, opens window 1).
again, click button on window 1 to open and populate data on window 2.
error fires. An item with the same key has been added.
It sounds like you are adding data to something in Window 2 that is shared across the entire application, or all instance of Window 2. When you enter Window 2 for a second time and populate your data, you are likely adding the data into a Dictionary that has already been added. Hence the message "An item with the same key has already been added".
My advice: put a break point in the code where you populate the data, and check the values contained within the Dictionary (if you have used one and have access to it). Then check the data you are adding and you should find the replication.
Alternately, there are various ways to prevent duplicate entries being added.
Clean the Dictionary when you have finished with it the first time - this way you only add the data in once (unless you have duplicate entries in your source data).
See if the key exists within the Dictionary before adding the entry. You can do this by using if (mSomeDictionary.ContainsKey(someEntryKey))/
If you simply want to use the latest values, you can just override the data keyed with a certain object. You can do this by using something like: mSomeDictionary[someEntryKey] = someValue;. If the entry key doesn't already exist, a new entry will automatically be added.
If none of the above helps you at all, post the code where the error occurs and include the Stack Trace from within the thrown exception and we can look into it further.

WPF Bind DataTable to repeated user controls

I'm in the process of teaching myself WPF, and I have run into a small issue that I can't find the answer to.
My test app allows image files to be dropped into a StackPanel. Once an image is dropped, a new user control is added to the stack, and displays some meta-data about the file. All is working correctly, and I can iterate through the child controls to retrieve the values.
What I'd prefer to be able to do is allow the user to persist this data to a file, so they can suspend working on the data. The obvious way for me to do this is to store the data in a DataTable and serialise/deserialise it to xml. However, I don't know how to drive the collection of user controls from a DataTable or DataSet object - in fact, I don't even know if this is the right way to go about it in a WPF app. I am more than willing to admit my ignorance here and take better suggestions if there are any.
Summary of the app logic.
1) File is dropped (from Win explorer) onto a StackPanel
2) File triggers creation of a new user control, which is added to the StackPanel
3) Data is populated in the user control
4) Processing data involves iterating through the control collection.
What I'd like
1) File is dropped (from Win explorer) onto a StackPanel
2) File data is inserted into some persistable object (data table?)
3) updated data table drives the generation of the user control to be added to the displayed collection.
4) save / load functionality persists the data for re-use later.
Thanks in advance
You're on the right track with the second approach, what you need to look at is the ItemsControl - that's a thing which can have items added to it. It's the base for ListBox etc, and you can template it to work as you require. Then there's the DataTemplate which handles which controls are displayed and data binding to those controls when an item is added to the underlying data structure. There are quite a few examples around on the net, try Dr WPF.
In order to make everything work the underlying data structure must support change notification. As everything happens automagically, once the Xaml is setup, you can find yourself in an odd situation. You've added data to a data structure, which in turn has caused controls and data to appear in your ItemsControl. How do you link the data items and their visual controls. The answer is to use some built in static methods ItemFromContainer which links from the graphic to your underlying data item, useful to handle click events, and ContainerFromItem which does the reverse.

Resources