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
Related
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
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
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!
I'm trying to write a basic function that takes the values of 5 textboxes. I want to double the value of the first 2 Boxes to replace their former values with them and save the other 3 inside of an array. Then I want a for-loop to take the values inside of the array and get their sum in a new variable, which I want to display in a seperate TextBox.
Sorry for asking such an odd question but at the job I've just started it's tradition for apprentices to solves a bunch of these problems in FoxPro.
You could paste the following code into a new PRG, e.g. by typing Modify Command into the Command Window, and then paste it there.
LOCAL oForm as Form
oForm = CREATEOBJECT("TestForm")
oForm.Show(1)
RETURN
DEFINE CLASS TestForm as Form
AutoCenter = .T.
ADD OBJECT TextBox1 as TextBox WITH Left = 20, Top = 20, Value = 1
ADD OBJECT TextBox2 as TextBox WITH Left = 20, Top = 50, Value = 2
ADD OBJECT TextBox3 as TextBox WITH Left = 20, Top = 80, Value = 3
ADD OBJECT TextBox4 as TextBox WITH Left = 20, Top = 110, Value = 4
ADD OBJECT TextBox5 as TextBox WITH Left = 20, Top = 140, Value = 5
ADD OBJECT theOtherTextBox as TextBox WITH Left = 200, Top = 20
ADD OBJECT cmdTest as CommandButton WITH Left = 200, Top = 80
PROCEDURE cmdTest.Click
Thisform.TextBox1.Value = Thisform.TextBox1.Value * 2
Thisform.TextBox2.Value = Thisform.TextBox2.Value * 2
LOCAL ARRAY laTest[3]
STORE Thisform.TextBox3.Value TO laTest[1]
STORE Thisform.TextBox4.Value TO laTest[2]
STORE Thisform.TextBox5.Value TO laTest[3]
LOCAL lnSum, lnValue
lnSum = 0
FOR lnValue = 1 TO ALEN(laTest)
lnSum = m.lnSum + laTest[m.lnValue]
ENDFOR && or alternatively
lnSum = 0
FOR EACH lnValue IN laTest
lnSum = m.lnSum + m.lnValue
ENDFOR
Thisform.theOtherTextBox.Value = m.lnSum
ENDPROC
ENDDEFINE
FWIW, the code-as-text presentation is just for the browser demo - in a real VFP project, you'd probably rather design "visual" things in the visual Form or Class designers, good luck John Doe
i have 50 checkboxes for 50 american states. The user can choose all 50 or only 1( so basically any number he wants). Based on his choice, I want to insert or update the table in sql server 2008. e.g-
Color = blue and chk1=check, chk2= check and chk3 = check (chk = checkbox). now the user wants to ad 10 more states to this or remove these 3 and add 5 more. so u basically get the idea. the table in database looks like this - ID Color State_id there is a table called states, so stateid shall come from there. so how do i do a loop insert or update in vb.net?
I would use a data source and a checkboxlist. You already have your states in a datatable, populate a checkboxlist with a databound selection from a SqlDataSource (or datatable of your own choosing). Then when you click your button just iterate through the following loop:
Dim dt as New myTypedDataTable ' constructed from datasource
Dim color as String = "Blue" ' Filled however you set color
For Each item As ListItem In Me.CheckBoxList1.Items
If item.Selected Then
Dim row as myTypedDataTableRow = dt.NewmyTypedDataTableRow
row.Color = color
row.State_id = item.Value
dt.Rows.Add(row)
End If
Next
Once you've got the datatable filled with the rows in question you could either use the SqlDataSource to perform an insert operation or perform the insert operation atomically. There are several ways to accomplish it, but this is probably the simplest way to iterate through the items given the data structure you described.
In this context, I sometime go the easy way and delete all the user items store in the database and then only do insert.
This can be a problem if you have for example a insert_date. In this case, you'll need to list of user selected options. Loop in the new list, if the item is not found in the old list then it's an insert. Loop in the old list, if the item is not found in the new list then it's a delete.
I would use bitwise operation and a long variable in .net (mixed with an enum for the flag)
one field in the db and way easier to play with what the user select
small sample
Enum state As Long '64 enum maxium since long = 64 bits
ALABAMA = 1
ALASKA = 2
NEVADA = 4
ARIZONA = 8
ARKANSAS = 16
CALIFORNIA = 32
COLORADO = 64
CONNECTICUT = 128
DELAWARE = 256
'etc etc etc
End Enum
Module Module1
Sub Main()
Dim userselect As state = 0
Console.WriteLine("your checked box state")
Console.WriteLine("in this case im using the order of the enum for selecting")
Dim checkbox = New Boolean() {True, False, False, True, False, False, True, False, False}
For i = 0 To checkbox.Length - 1
userselect = CType(userselect + If(checkbox(i), (2 ^ (i + 1)), 0), state)
Next
For Each s As state In [Enum].GetValues(GetType(state))
If (userselect And s) > 0 Then
Console.WriteLine("selected " & s.ToString)
End If
Next
Console.WriteLine("Value of userselect is " & userselect.ToString)
Console.ReadKey()
End Sub
End Module
OUTPUT:
selected NEVADA
selected ARIZONA
selected COLORADO
Value of userselect is 76