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.
Related
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"))
Was wondering if it is possible to use a control's name in its own event sub without specifically hard coding it's name, so it can be used on other control's events, of the same type, with the exact same syntax.
for example:
Private Sub Command1_Click()
Me.Caption = "Hello"
End Sub
Will set the containing form's caption to "hello".if i copy the same line of code to another form, it will set its caption to "hello, and so on.
i was wondering if there's another keyword for the control Command1 itself, so i'll be able to to copy-paste the same code to another command button without hard coding the name of the control.
after years of writing code in vb6, I'm 90% sure it can not be done, but it's worth the shot.
One way to do this is with the Microsoft Script Control -- add one to your form, and then you can do, as per your example:
Private Sub Command1_Click()
ScriptControl1.AddObject "me", ActiveControl, True
ScriptControl1.ExecuteStatement "me.Caption = ""Hello"""
ScriptControl1.Reset
End Sub
If you don't already have the Script Control -- check Project/Components for Microsoft Script Control 1.0 (which is msscript.ocx) -- you can download it here:
http://www.microsoft.com/en-us/download/details.aspx?id=1949
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
I am trying to build a basic front-end to link information together that would be accessible by clicking a text box (or a button) on the front page of the GUI. Basically, I have a number of text boxes for the different "functions" I want to show information on.
How do I make it so that on click, that box will open up a new window that I've already designeD? Within the solution (WPApplication,) I have all of the .xaml's set up (that are the seperate windows) in the solution already- I just want to call the on-click to open them. I figured it'd just be Show('Window Name') after private/as/handles, but it gives me an expression. Here's what I have:
Private Sub PO_info_MouseDown(Sendar As Object, e As MouseButtonEventArgs) Handles PO_Info.Mousedown
Show('PO Information')
End Sub
End Class
To flesh out my comment, the new window objects that you created are classes, you need to create an instance of the class before the Show Method will work. Lets assume that your PO Information forms class is POInfo you would need to do something like this.
POInfo POInformation = new POInfo;
POinformation.Show(); //This is where your show method is
Since your code looks like vb.net it would look like this in VB
Dim POInformation as New PoInfo
POInformation.Show()
First some explanation:
I am attempting to make (what should be) a simple offline "character generator" for a game I am working on. I am using VB for this and I am still a beginner. I have created a WinForms application in VS 2012 and created a local database object to hold various data. Within the database I have a 'Players' table to hold all the data. I have also added a BindingNavigator to the form, yet I removed all but the add and delete buttons as navigation is controlled by a List control which is bound to the PlayersBindingSource. I also added a save button to the BindingNavigator.
I manually populated the 'Players' table with two example entries to work with and, when the form loads, the listBox displays the two entries by their 'Player Name' field correctly. When one is selected all the controls representing each field change accordingly. If I change any values and hit the save button, it seems to work with the code I'm using.
Now the problems:
When the add button is pressed, it makes a new entry in the ListBox, yet none of the default values specified are showing up. If I edit this entry and then hit save it seems to update fine. However, if I make any other changes to any of the entries and try to save, it throws an exception.
When the delete button is pressed, the entry disappears from the list as expected, but again, if I try to save, I get an exception.
here is the code that is in my form thus far (not much):
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.PlayersTableAdapter.Fill(Me.DatPlayerDataSet.Players)
End Sub
Private Sub SaveToolStripButton_Click(sender As Object, e As EventArgs) Handles SaveToolStripButton.Click
Me.Validate()
Me.PlayersBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DatPlayerDataSet)
End Sub
End Class
Also, if I look at the data contained in the 'Players' table, the two example entries remain unchanged, and no additional entries are added, so I don't think the edits were never actually committed to the table.
If anyone can help me with this, or could even simply provide a link to some tutorials that would help me, I would be highly appreciative. I have browsed this site, and microsofts msdn library, searching for information about data binding and navigation, yet I couldn't seem to find anything that helped.
You want do one more think in "btnNewPlayer_Click" sub after insert row, clear the list and re load the list items or call the function which you are using to fill the list items.
Try refreshing the page, the list might get updated.