VB declaration of table - vb.net

I have a little problem: I want to make some app in VB and I make two CheckedListBoxs and I have some idea: if I chose something in CheckedListBox1 I want to show some date inside CheckedListBox2.
I have a problem with declaration - I make something like this:
Dim model3 = {"A", "B", "C"}
But I have only one information inside CheckedListBox2: 'String[]'
If CheckedListBox1.CheckedItems.Count <> 0 Then
If CheckedListBox1.SelectedItem.ToString = "GWW" Then
Marka.Items.Add(model1)
ElseIf CheckedListBox1.SelectedItem.ToString = "AWW" Then
Marka.Items.Add(model2)
ElseIf CheckedListBox1.SelectedItem.ToString = "ZWW" Then
Marka.Items.Add(model3)
End If
Else
Marka.Items.Clear()
End If
Could you give me some prompt? I do not have too much experience so if I could asked as simple as possible :)

That's because you're only adding one item - the array itself - and the CheckedListBox will call its ToString method to get text that it can display, which is what you see. If what you actually want to do is add all the elements in the array into the CheckedListBox then you need to call AddRange rather than Add.

Related

Saving Custom Document Properties in a Loop

I'm trying to save the values of data that have been input into my form. There are a total of about 50 different fields to save across 5 different agents, so I loaded the data into arrays.
I've tried saving the fields in a loop, but it doesn't seem to work in a loop, only if each field has a separate line, which is a lot of code and messy. The Ag1Name, Ag2Name and Ag3Name are the names of my textboxes that the user enters to populate the form.
Sub LoadAndSaveData()
NumberofAgents = 3
Dim AgentName(3) as String
AgentName(1) = Ag1Name.Value
AgentName(2) = Ag2Name.Value
AgentName(3) = Ag3Name.Value
For Count = 1 To NumberOfAgents
With ActiveDocument.CustomDocumentProperties
.Add Name:="AgentName" & Count, LinkToContent:=False, Value:=AgentName(Count), Type:=msoPropertyTypeString
End With
Next Count
End Sub
The data doesn't get saved to the Custom Document Properties when the code is set up in a loop like the above. Since there are so many values to save and all the data is already in arrays, I would much prefer to use a loop rather than write out a separate line of code for all ~50 of the values. It does seem to work when each field is saved in a separate line of code.
I think this would probably get what you want. You don't really need to count the document properties first, only increment with the ones you want to update. Hopefully the only document properties you want contain the name AgentName in it.
ReDim AgentName(0) As String
Dim P As Long
For Each c In ThisDocument.CustomDocumentProperties
If InStr(1, c.Name, "AgentName", vbTextCompare) > 0 Then
ReDim Preserve AgentName(P)
AgentName(P) = c.Value
P = P + 1
End If
Next c
As a guest I cannot post a comment here, but the code you gave works OK here.
However, there is a problem with creating legacy custom document properties programmatically, because doing that does not mark the document as "changed". When you close the document, Word does not necessarily save it and you lose the Properties and their values.
However, if you actually open up the Custom Document Property dialog, Word does then mark the document as "changed" and the Properties are saved.
So it is possible that the difference between your two scenarios is not the code, but that in one scenario you have actually opened the dialog box to check the values before closing the document and in the other you have not.
If that is the case, here, I was able to change this behaviour by adding the line
ActiveDocument.Saved = False
after setting the property values.
If you do not actually need the values to be Document Properties, it might be better either to use Document Variables, which are slightly easier to use since you can add them and modify them with exactly the same code, or perhaps by storing them in A Custom XML Part, which is harder work but can be useful if you need to extract the values somewhere where Word is not available.
You can make this even easier by looping the controls on the UserForm, testing whether the control name contains "Ag" and, if it does, create the Custom Document Property with the control's value - all in one step.
For example, the following code sample loops the controls in the UserForm. It tests whether the controls Name starts with "Ag". If it does, the CustomDocumentProperty is added with that control's value.
Sub LoadAndSaveData()
Dim ctl As MSForms.control
Dim controlName As String
For Each ctl In Me.Controls
controlName = ctl.Name
If Left(controlName, 2) = "Ag" Then
With ActiveDocument.CustomDocumentProperties
.Add Name:=controlName, LinkToContent:=False, value:=ctl.value, Type:=msoPropertyTypeString
End With
End If
Next
End Sub
I feel a little stupid... I just realized that the reason that the code wasn't working was that the variable NumberofAgents was not being calculated correctly elsewhere in my code. I've got it working now. Thanks for your thoughts!

Is there a way I can assign every single textbox in a form to a single variable?

txtRound.Text
txtDaysWorked.Text
txtPerGallon.Text
txtGasCost.Text
txtMaint.Text
txtParking.Text
txtInsure.Text
I have all these textboxes in a vb form and instead of checking every single one to make sure they are all positive numbers I want a more efficient way of checking them with one variable. Once that variable is declared I'll just go like this
If (dblVariable > 0) Then
A lot easier than a long list of checking
Your form has a Controls properties which is a list of all controls on that form.
You can loop through each control and check it is a TextBox, and if so set it's Text property.
If LINQ is available you could simplify this to
For Each control In myForm.Controls.OfType<TextBox>()
control.Text = "My Value"
Next
Re-read the question, the title is a bit misleading, but I get what you're asking now.
Again, assuming LINQ is available you should use the .All extension.
If Me.Controls.OfType(Of TextBox)().All(Function(x) Convert.ToDouble(x.Text) > 0) Then
' Do something
End If
This is going to be a vague answer, because I'm working from memory, but here's my best attempt.
In short, I don't think so. If you want to check one variable you're going to have to generate that variable yourself. You could do this client-side.
Alternatively, if all of these controls are within a single form or something, you might be able to cycle through each control in a loop to check each one.
You could have a function which takes the list of TextBoxes:
Private Function CheckValues(txt As TextBox()) As Boolean
For Each member In txt
If not String.IsNullOrWhiteSpace(member.Text) Then
Dim number As Integer = CInt(member.Text)
If number < 0 Then
Return False
Next
Return true 'all positives
End Function
And then call it like this:
If CheckValues(new TextBox(){txtRound, txtDaysWorked}) Then
'case true
Else
'case false
End If

VB.Net Dynamically Referencing a Variable or Control

It would take too long to try an explain the actual application I am building.
Lets just say, I have 3 textboxes on my form. I want to set each one of them to a value of the numerical index. This is how I would normally do it.
txt1.Text = "1"
txt2.Text = "2"
txt3.Text = "3"
Now, if I had 100 of these textboxes, I would want to do something more like this.
For i as Integer = 1 to 3
txt[i].Text = i
Next
Is this possible?
This is how I would do it: first, create a list or an array (depending on whether the number of textboxes is fixed or not) and add you textboxes to it:
Dim txtList as new list(of textbox)
txtList.items.add(txtBox1...
This can be done automatically using jmcilhinney's method (with the same caveats):
Dim txtList = New List(Of TextBox)(Me.Controls.OfType(Of TextBox)().ToArray())
Then you can reference it as such:
txtList(i).text
I hope that's what you were asking and that it helps :)

VB - How do you remove "empty" items from a generic list?

I have a VB.NET (2010) project that contains a generic list, and I'm trying to figure out how to remove any "empty" items from the list. When I say "empty", I mean any item that does not contain any actual characters (but it may contain any amount of whitespace, or no whitespace at all).
For example, let's say this is my list...
Dim MyList As New List(Of String)
MyList.Add("a")
MyList.Add("")
MyList.Add("b")
MyList.Add(" ")
MyList.Add("c")
MyList.Add(" ")
MyList.Add("d")
I need it so that if I did a count on that list, it would return 4 items, instead of 7. For example...
Dim ListCount As Integer = MyList.Count
MessageBox.Show(ListCount) ' Should show "4"
It would be nice if there was something like...
MyList.RemoveEmpty
Anyways... I've been searching Google for a solution to this for the past few hours, but haven't been able to turn up anything so far. So... any ideas?
BTW, I'm targeting the .NET 2.0 framework for this project.
Thanks in advance!
You can use List.RemoveAll
MyList.RemoveAll(Function(str) String.IsNullOrWhiteSpace(str))
If you don't use at least .NET 4, you can't use String.IsNullOrWhiteSpace. Then you can implement the method yourself:
Public Shared Function IsNullOrWhiteSpace(value As String) As Boolean
If value Is Nothing Then
Return True
End If
For i As Integer = 0 To value.Length - 1
If Not Char.IsWhiteSpace(value(i)) Then
Return False
End If
Next
Return True
End Function
Note that Char.IsWhiteSpace is there since 1.1.
The post marked as solution didn't works for me.
Try this:
MyList.AsEnumerable().Where(Function (x) Not String.IsNullOrEmpty(x)).ToList
This must returns you a list without empty values.

Vb.net Gridview "pointer"?

I have code that interacts with a gridview, and the code is exactly the same for multiple gridviews. So can I do something like this:
Dim gridViewPointer As GridView
If (gridViewNumber = 1) Then
gridViewPointer = GridView1
ElseIf (gridViewNumber = 8) Then
gridViewPointer = GridView8
...
and then
If (gridViewPointer.DataSourceID = SQLDatasourcetemp.ID) Then
...
Will this work or is there another way to do this?
Edit:
I'm checking to make sure that the data the user is inputting into the gridview is correct. It could be one of 4 gridviews, and the checks are exactly the same, the only parameter that changes in the code is gridview1/gridview2/etc. So if I can use a "pointer" to the correct gridview then I can elimninate all the duplicate code.
Yes that is not a problem at all.
Whenever you assign an object to a variable you are actually assigning a memory reference to the variable. Using that reference you can read, write, and call all properties and methods of the object as if it were there original.
You might want to read up on the differences between value and reference types. This is primarily a concern when passing data through function calls.
http://msdn.microsoft.com/en-us/library/t63sy5hs%28VS.80%29.aspx
In fact I would probably create a new function to call on the gridview...
Private Sub GridOperations(ByVal grid as GridView)
//Do work here.
End Sub
If (gridViewNumber = 1) Then
GridOperations(GridView1)
ElseIf (gridViewNumber =8) Then
GridOperations(GridView8)
...
What you are asking is correct. When you set gridViewPointer = GridView1, you are actually only storing the pointer to the GridView1 object, not copying the object, so any action you perform on gridViewPointer after the set will directly control GridView1.