How to retrieve a VB Control instance, given its Name and Index? - arrays

If I have a string "cboEnoughMoney(0)", is it possible to retrieve the control from a control array that I have in my form, cboEnoughMoney?
In my code, I try to refer to the control in this way:
frm.Controls(sControlName).ListIndex = -1
sControlName in this case is "cboEnoughMoney(0)" and I get an error that the control was not found.
EDIT: One of the issue's i'm having is trying to as above set the .listindex to -1, another one is where I actually try to get and set value to the combobox. In a function - i pass form name as parameter called frm.
In the code what has been done is this....
dim ctlData as string
If Frm.Controls(RS!CTRLname).ListIndex > 0 Then
CtlData = Frm.Controls(RS!CTRLname).Value
Else
CtlData = ""
End If
Any idea how I'd be able to do that with cboEnoughMoney(0)....
I assume I could follow your example and check
if instr(1,RS!CTRLname, "(")
but if there is, how would I refer to that particular control in frm

It is just enough to look in the VB Properties Window: if you search "cboEnoughMoney(0)" you will find "cboEnoughMoney"(0) (without double quotes). Still the name is the same as a non-array control.
So, it is perfectly legal to refer to a VB Control with Name and Index, no need for a controls loop here:
If isControlsArray Then
Set ctrl = Me.Controls(Name)(Index) '// array
Else
Set ctrl = Me.Controls(Name) '// non array
End If

You cannot use the (index) part in a lookup via .Controls.
You can loop them manually & given that you can safely assume any control name with a ( is an array member and thus will have an Index property match on that:
Private Function GetControl(ByVal name As String) As VB.Control
Dim pos As Long: pos = InStr(name, "(")
If pos = 0 Then
Set GetControl = Me.Controls(name) '// non array
Else
Dim index As Long
index = Val(Mid$(name, pos + 1)) '// get index #
name = Left$(name, pos - 1) '// get base name
For Each GetControl In Me.Controls
If (GetControl.name = name) Then
If (GetControl.index = index) Then Exit Function
End If
Next
Set GetControl = Nothing
End If
End Function
And then:
GetControl("cboEnoughMoney(1)").ListIndex = -1

Related

SSRS Median and drill down reports

I have a SQL Server Reporting Services report in which I calculate the median using an ArrayList in a custom code block as median is not a built in function in SSRS. The median and average are displayed in a column chart. The report includes a drilldown to a detail report. If I drill down to the detail report and then hit the back arrow in the browser to navigate back to the master report, the median on the master report is not being calculated. That column of the column chart is empty while the average is displayed as it should.
Does anyone have any thoughts on why this is happening or how I can fix it?
The code is below. AddValue() first creates a new SortedList of it doesn't already exist. It then adds the newValue to an ArrayList keyed by the string passed to the function, creating one if it didn't previously exist. Each ArrayList represents a different set of values for which we would want to calculate the median.
GetMedian() calculates the median for the ArrayList identified by the passed string. GetMedian() is used in the value for the column in the chart.
Public Dim ClearanceList As System.Collections.SortedList
Function AddValue(ByVal whichList As String, ByVal newValue As Integer) As Decimal
Dim thisList As System.Collections.ArrayList
If (ClearanceList is Nothing) Then
ClearanceList = New System.Collections.SortedList
End If
If (ClearanceList.ContainsKey(whichList)) Then
'Get a reference to the desired ArrayList and add the new value to it.
thisList = ClearanceList.GetByIndex(ClearanceList.IndexOfKey(whichList))
Else
thisList = New System.Collections.ArrayList()
' Create a new element in the SortedList
ClearanceList.Add(whichList, thisList)
End If
thisList.Add(newValue)
AddValue = thisList.Count
End Function
Function GetMedian(ByVal whichList As String) As Decimal
Dim thisList As System.Collections.ArrayList
Dim count As Integer
thisList = ClearanceList.GetByIndex(ClearanceList.IndexOfKey(whichList))
count = thisList.count
If (count > 0) Then
thisList.Sort()
If count Mod 2 = 1 Then
GetMedian = thisList((count - 1) / 2)
Else
GetMedian = (thisList((count / 2) - 1) + thisList((count / 2))) / 2
End If
Else
GetMedian = -1
End If
End Function

How to loop through document and find words from predefined array and replace each word with words from another array

I want to loop word document and find words from arrayOne.
for example
arrayOne = ["john", "jack", "dog"]
and each word that matches to replace with words from arrayTwo
arrayTwo = ["ana", "tanja", "cat"]
You have not supplied any code with issues so there is nothing to fix, however you have asked 'How to find words and replace them'
Below are the steps you will need to code to achieve this: -
Build your array
Dim AryFNR(1,2) As String
AryFNR(0,0) = "john"
AryFNR(1,0) = "ana"
AryFNR(0,1) = "jack"
AryFNR(1,1) = "tanja"
...
Connect to the document
Dim WdDoc As Document
Set WdDoc = ThisDocument
...
Set WdDoc = Nothing
Connect to the selection ensuring it is in the connected document
Dim WdSlct As Selection
WdDoc.Content.Select
Set WdSlct = Selection
WdSlct.SetRange 0, 0
....
Set WdSlct = Nothing
Connect to Find and Replace and clear previous settings
Dim WdFnd As Find
Set WdFnd = WdSlct.Find
WdFnd.ClearAllFuzzyOptions
WdFnd.ClearFormatting
...
Set WdFnd = Nothing
Process your array using WdFnd on each item
Dim LngCounter As Long
For LngCounter = 0 To Ubound(AryFNR,2)
With WdFnd
.Execute FindText:=AryFNR(0, LngCounter), ReplaceWith:=AryFNR(1, LngCounter)
End With
Next
Some points you'll want to watch out for: -
Footnotes and Endnotes are not processed unless the selection in the Footnote/Endnote.
There are extra values on the .Execute command that you will want to set, depending on how you code it .Found may be useful.
That is enough information for you to start your own code to achieve your goal, enjoy!

reference dictionary within array output to listbox

Title kind of states my problem.
I'm using an Api (For Vertical Response, an email list manager) which works fine. But for a particular method returns an Array where the information I need to reference is within a Dictionary inside that Array.
Array[list_id, list_name, list_type, member_data] <- member_data being the dictionary housing all my goodies.
Best I've managed to get is the listbox outputting "com.verticalresponse.api.NVPair[]" for each member.
Code
Protected Sub GetBounces()
Dim listID As Integer = 284662333
Dim isMember As Boolean = False
Dim newSession As New loginArgs()
newSession.username = username
' Your VerticalResponse username
newSession.password = password
newSession.session_duration_minutes = "120"
Dim VRUser As New VRAPI()
Try
sessionID = VRUser.login(newSession)
Dim GetMembers As New getListMembersArgs()
Try
GetMembers.session_id = sessionID
GetMembers.list_id = listID
GetMembers.max_records = 8
Try
Dim listmembers As Array = VRUser.getListMembers(GetMembers)
lstBounces.DataSource = listmembers
lstBounces.DataValueField = ("member_data").ToString()
lstBounces.DataBind()
Catch ex As Exception
lstBounces.Text = "One: " + ex.ToString()
End Try
Catch listex As Exception
lstBounces.Text = "Two: " + listex.ToString()
End Try
Catch ex As System.Exception
lstBounces.Text = "Three: " + ex.ToString()
End Try
End Sub
Edit
I have taken the suggestion of Keith Mifsud and added a breakpoint just before the Databind. It shows me that "listmembers" has eight indices (currently only testing by searching for 8 (total list will be close to 8000, of which about 1900 are needed.))
Each of those 8 indices contains the 4 columns I am looking at, so when I use listmembers(3) as the datasource I'm really only searching the fourth result, not the member_data column...
Is there a correct way to reference the column of results?
Something like:
lstbounces.DataSource = listmembers( ,3)
But instead of that, a correct one?
I think you should set the datasource as the dictionary not the array holding it
Try
Dim listmembers As Array = VRUser.getListMembers(GetMembers)
lstBounces.DataSource = listmembers(3)
'As the datasource is a dictionary, I don't think you have to set up the display and value fields
'lstBounces.DataValueField = ("member_data").ToString()
lstBounces.DataBind()
Catch ex As Exception
lstBounces.Text = "One: " + ex.ToString()
End Try
UPDATE
Based on what the collection type (dictionary within every element of an array) I would recommend you use the .net DataView with allows for expandable rows. There are some very good tutorials which can guide you to create your view

VBasic array of labels, name auto settings?

greets,
I'm trying to create multiple labels as an array but so far nothing works.
e.g. I took this code and put it into Form1_Load, it works and creates one label at run time:
Dim vulabel1 As New Label()
vulabel1.Size = New Size(100, 20)
vulabel1.Location = New Point(25, 25)
vulabel1.Name = "textBox1"
Me.Controls.Add(vulabel1)
vulabel1.Text = "vu label 1"
When I change it to a for loop it ceases to work:
Dim vulabel() As Label
For n As Byte = 0 To 2
vulabel(n).Size = New Size(100, 10)
vulabel(n).Location = New Point(n * 10, n * 10)
vulabel(n).Name = "label " & n.ToString
Me.Controls.Add(vulabel(n))
Next
I thought this should place two labels on the Form1 at runtime.
The reason I need a quick way to create labels is that I need a matrix of 8x8 labels. At least if I could change the default label name so e.g. I would create a label and change the name to vu_label1, copy and then paste other labels named vu_label2, vu_label3, unfortunately Visual Studio keeps changing the label name back to Label1 when I do copy and paste.
Another thing is that you can't declare an array element as the name of a label, e.g. vu_level(1).
thanks for any input
You have to specify the maximum index when declaring an array. Based on the For loop, looks like the maximum index is 2, so this is how you declare the array
Dim vulabel(2) As Label
The next step is creating a new instance of Label at each iteration as below
vulabel(n) = New Label()
Here's the complete modified code
Dim vulabel(2) As Label
For n As Byte = 0 To 2
vulabel(n) = New Label()
vulabel(n).Size = New Size(100, 10)
vulabel(n).Location = New Point(n * 10, n * 10)
vulabel(n).Name = "label" & n.ToString
Me.Controls.Add(vulabel(n))
Next

Remove duplicate items from processes array using VB .net

I have to code to get all accessible process, but I need to remove duplicated items on this array and show only one time each process.
How is the best method to do this, because I think processes array is not like a normal array.
My code:
For Each p As Process In Process.GetProcesses
Try
'MsgBox(p.ProcessName + " " + p.StartTime.ToString)
Catch ex As Exception
'Do nothing
End Try
Next
Thanks in advance
The Process.GetProcesses() method returns an array. You can use the Distinct method, providing an IEqualityComparer to it.
An example would be as comparer:
Public Class ProcessComparer
Implements IEqualityComparer(Of Process)
Public Function Equals1(p1 As Process, p2 As Process) As Boolean Implements IEqualityComparer(Of Process).Equals
' Check whether the compared objects reference the same data.
If p1 Is p2 Then Return True
'Check whether any of the compared objects is null.
If p1 Is Nothing OrElse p2 Is Nothing Then Return False
' Check whether the Process' names are equal.
Return (p1.ProcessName = p2.ProcessName)
End Function
Public Function GetHashCode1(process As Process) As Integer Implements IEqualityComparer(Of Process).GetHashCode
' Check whether the object is null.
If process Is Nothing Then Return 0
' Get hash code for the Name field if it is not null.
Return process.ProcessName.GetHashCode()
End Function
End Class
And you can use it like this:
Sub Main()
Dim processes As Process() = Process.GetProcesses()
Console.WriteLine(String.Format("Count before Distinct = {0}", processes.Length))
' Should contain less items
Dim disProcesses As Process() = processes.Distinct(New ProcessComparer()).ToArray()
Console.WriteLine(String.Format("Count after Distinct = {0}", disProcesses.Length))
Console.ReadLine()
End Sub
You probably have to refine the Comparer to your specifications and for the situation you are going to use it.

Resources