About Casting in a Class property in VB.Net - vb.net

Is it possible to cast a sting in a class property?
what i want to do is that from 'My' class i can fetch the Form's Text properties. but for every form i have to provide the form name statically. I want to do it dyanamically. What i tried is here
Dim frmName As New Object
frmName = Name.ToString()
Dim frmProperty As String
frmProperty = "My.Forms." & frmName & ".Text"
frmNameLabelControl.Text = frmProperty
but at the Front-End it displays 'My.Forms.Form1.Text'

I think that all you need is the following code. But you need to create the equivalent of frmNameLabelControl in the same order in every form. For example to be the last label you created in every form
Dim FormsCount As Integer = My.Application.OpenForms.Count
Dim frmProperty As String
Dim lblContrIndex As Integer = 0 ' Number of label creation in descending order
For i As Integer = 0 To FormsCount - 1
frmProperty = My.Application.OpenForms.Item(i).Text
My.Application.OpenForms.Item(i).Controls.OfType(Of Label).ElementAt(lblContrIndex).Text = frmProperty
Next

Related

Getting data from dataset

I have got a problem, I have got a filled dataset, but now i need to get a Column value from it and store it into a variable. On Button 2 click i fill it with this:
tbaGridview.Fill(BlGridview1.vwBLcontainerCargo, Bookingnumber)
Now i want to get the data from it that is inside. I have got a for loop:
For i As Integer = 0 To GridView1.DataRowCount - 1
Dim OriginSealNumber As String = BlGridview1.vwBLcontainerCargo.Tables("SEALNUMBER").Rows(i).Item(0)
Next i
But it says Tables is not a member of windowsapplication1.blgridView.vwBLContainerCargoDataTable. How do i get the data for each column??
To reference the GridView's table use this sintax:
((DataRowView)GridView1.Rows[0].DataBoundItem).DataView.Table
But I recommend that you use a BindingSource object and iterate through it.
I fixed it myself by using this:
For i As Integer = 0 To GridView1.DataRowCount - 1
Dim OriginSealNumber As String = BlGridview1.Tables(0).Rows(i)("SEALNUMBER").ToString()
Dim OriginContainerNumber As String = BlGridview1.Tables(0).Rows(i)("CONTAINERNUMBER").ToString()
Dim OriginContainerType As String = BlGridview1.Tables(0).Rows(i)("CONTAINERTYPE").ToString()
Dim OriginQuantity As String = BlGridview1.Tables(0).Rows(i)("QUANTITY").ToString()
Dim OriginPackageType As String = BlGridview1.Tables(0).Rows(i)("PACKAGETYPE").ToString()
Dim OriginDescription As String = BlGridview1.Tables(0).Rows(i)("DESCRIPTION").ToString()
Dim OriginWeight As String = BlGridview1.Tables(0).Rows(i)("WEIGHT").ToString()
Next i

Get ValueMember of Selected item in ListBox

I've seen a couple of posts asking a similar question but I have not been able to duplicate the answers in my code successfully.
The following code adds items and their value member to a list box.
Public Shared Sub ListFiles(hTab As Hashtable)
Debug.Print("create file and key" & Now)
Dim Enumerator As IDictionaryEnumerator
Enumerator = hTab.GetEnumerator()
Dim MyKeys As ICollection
Dim Key As Object
MyKeys = hTab.Keys()
If (hTab.Count > 0) Then
For Each Key In MyKeys
Dim sfileName As String = hTab(Key)
Dim first As Integer = sfileName.IndexOf("_")
Dim last As Integer = sfileName.LastIndexOfAny("_")
Dim first2 = (first + 1)
Dim splitFile = sfileName.Substring(first2)
frmViewFiles.ListBox1.Items.Add(splitFile)
frmViewFiles.ListBox1.ValueMember = Key
frmViewFiles.ListBox1.SelectedValue = Key
Next
End If
End Sub
When I run my code to get the selected items value member
Dim file = ListBox1.ValueMember.ToString()
I can acess the first item I choose but subsequent selections dont change the value member to that of the selected item.
Please direct me.
Thank you for your answers. this is my new code:
Public Shared Sub runListFiles(CustomerId As String)
Dim cfp As New CloudFilesProvider(cloudId)
Dim containerObjectList As IEnumerable(Of ContainerObject) = cfp.ListObjects(container:="EstherTest", identity:=cloudId, prefix:=CustomerId & "_")
For Each file As ContainerObject In containerObjectList
Dim sFullFileName As String = file.Name
Dim first As Integer = sFullFileName.IndexOf("_")
Dim first2 = (first + 1)
Dim splitFile = sFullFileName.Substring(first2)
'frmViewFiles.ListBox1.Items.Add(splitFile)
'frmViewFiles.ListBox1.ValueMember = sFullFileName
Dim fb = New myFile
fb.FileName = splitFile
fb.FullPath = sFullFileName
frmViewFiles.ListBox1.Items.Add(fb)
frmViewFiles.ListBox1.DisplayMember = fb.FileName
frmViewFiles.ListBox1.ValueMember = fb.FullPath
This is my class:
Public Class myFile
Public Property FileName As String
Public Property FullPath As String
Public Sub New(f As String, b As String)
FileName = f
FullPath = b
End Sub
End Class
Please see my comment below and assist
ValueMember is supposed to indicate the property name of an object added to the Items collection: the property to use as the actual value for the items in the ListControl.
You are not adding objects to the control, so Key from the hashtable is meaningless as the ValueMember. Your post references a file variable in passing, so I will assume this revolves around showing the filename while wanting to get the full pathname when selected/clicked. WebForms/Winforms/WPF was not indicated, I am assuming WinForms:
Public Class myFile
Public Property FileName As String
Public Property FullPath As String
Public Property FileSize As Int64 ' just so there is something else
Public Sub New(f as String, p as String, s as Int64)
FileName = f
FullPath = b
FileSize = s
End Sub
End Class
Lets say we want to add some of these to a ListBox, for each item added we want FileName to display as the text, but want to get them back by FullPath:
Dim f As myFile
' assume these come from a fileinfo
For Each fi as FileInfo in DirectoryInfo.GetFiles(searchFor)
f = New myFile
f.FileName = fi.Name
f.FullPath = fi.FullPath
f.FileSize = fi.Length
' myFile accepts all the prop values in the constructor
' so creating a new one could also be written as:
' f = New myFile(fi.Name, fi.FullPath, fi.Length)
myListBox.Items.Add(f)
Next n
If the myFile objects were stored to a List(of myFile) rather than adding them to the control, we can bind the List as the DataSource and not have to iterate or copy:
mylistBox.DataSource = myFileList
Either way, Display- and ValueMember refer to the property names we wish to use:
myListBox.DisplayMember = "FileName" ' indicate property name of obj to SHOW
myListBox.ValueMember = "FullPath" ' prop name of object to return
When you select a listbox item, myListBox.SelectedValue would refer to the FullPath of the myFile object clicked on. The SelectedIndex would still refer to the index of the item in the list.
tl;dr
ValueMember and DisplayMember refers to the Property Names of Objects represented in the list.
Note:
I know this is many years later, but still relevant information.
It took me a while to parse what was said above, until I grokked it fully, so I thought it might help if I restated it slightly.
When you select a listbox item,
myListBox.SelectedValue is the contents of the field, myListBox.ValueMember. ValueMember contains the Field Name, SelectedValue contains the contents of the field.
myListBox.SelectedItem is the contents of the field myListBox.DisplayMember. DisplayMember contains the field name and SelectedItem contains the value of the field.
The SelectedIndex refers to the index of the item in the list. To see which item is selected, reference myListBox.SelectedIndex. You can, for example, change the selection to the last item in the list by using myListBox.SelectedIndex = myListBox.Items.Count - 1
If you want to display the values, then
Console.WriteLine("The value of {0} is {1}",myListBoxDisplayMember,myListBox.SelectedItem)
Console.WriteLine("The Value of {0} is {1}",myListBox.ValueMember,myListBox.SelectedValue)

how to populate listbox with array items

Im trying to populate a listbox with items inside the array, i have declared an array and assigned strings which it holds but im not sure if i even done that correctly, i want to use that strings that are in the array to populate a list box, here is the code i have already done, how do i do that, could anyone give me code that i could use to populate listbox with those strings.
Dim NewDefinition As String
NewDefinition = InputBox(" Please enter definition in the box and click OK. " & " The definition entered will be added to the list. ", " Add Definition")
lstDefinitions.Items.Add(NewDefinition)
Dim NewDefinition1 As String = lstDefinitions.Items(0).ToString
Dim NewDefinition2 As String = lstDefinitions.Items(1).ToString
Dim NewDefinition3 As String = lstDefinitions.Items(2).ToString
Dim NewDefinition4 As String = lstDefinitions.Items(3).ToString
Dim NewDefinition5 As String = lstDefinitions.Items(4).ToString
Dim NewDefinition6 As String = lstDefinitions.Items(5).ToString
Dim NewDefinition7 As String = lstDefinitions.Items(6).ToString
Dim NewDefinition8 As String = lstDefinitions.Items(7).ToString
Dim NewDefinition9 As String = lstDefinitions.Items(8).ToString
Dim NewDefinition10 As String = lstDefinitions.Items(9).ToString
Dim NewDefinitions(10) As String
NewDefinitions(0) = NewDefinition1
NewDefinitions(1) = NewDefinition2
NewDefinitions(2) = NewDefinition3
NewDefinitions(3) = NewDefinition4
NewDefinitions(4) = NewDefinition5
NewDefinitions(5) = NewDefinition6
NewDefinitions(6) = NewDefinition7
NewDefinitions(7) = NewDefinition8
NewDefinitions(8) = NewDefinition9
NewDefinitions(9) = NewDefinition10
Listbox will accept any object and will display whatever the objects ToString method will display. Therefore you can populate the listbox with the objects directly. See if this works for you:
ListBox1.DataSource = lstDefinitions.Items
If the objects in question are from a custom class you can override the ToString method to display the information you desire.

How to get a value back out from a property in a class

I have the following code in my Form1 wherein I will set Value to a property:
Dim login As New logInSession
Dim k As String
login.saveLogInfo = unameTB.Text
and this is my code from my logInSession class where I will store the value:
Private logInfo As String
Public Property saveLogInfo() As String
Get
Return logInfo
End Get
Set(ByVal value As String)
logInfo = value
End Set
End Property
Now, I want to get back out the value to my Form2. The problem is it doesn't return any value. The following is my code:
Dim login As New logInSession
Dim k As String
k = login.saveLogInfo
MsgBox(k)
Can you tell me what's the problem with my codes?
Dim login As New logInSession // HERE
Dim k As String
k = login.saveLogInfo
MsgBox(k)
This code creates a new instance of the logInSession class. Therefore, each time you are trying to access the login.saveLogInfo property, it is at it's default value (empty string). You need to use the original object you created here:
Dim login As New logInSession
Dim k As String
login.saveLogInfo = unameTB.Text
That's because you have two different instances of logInSession class. One in Form1 and another in Form2. Here is an illustration :
Dim login As New logInSession
'you have 1 logInSession instance here, and set it's property
login.saveLogInfo = "Some Text"
Dim anotherLogin As New logInSession
'but later you check property of another instance of logInSession
Dim k = anotherLogin.saveLogInfo
'here you get empty string
Console.WriteLine(k)
'you need to, in some way, pass the first instance instead of creating new instance
Dim referenceToFirstInstance As logInSession = login
k = anotherLogin.saveLogInfo
'here you get "Some Text"
Console.WriteLine(k)
Check this reference on how to pass data between forms

Using Value of String variable to call an instance in vb.net winform

I have UserControl named for example 'aaa'
then i have variable:
Dim a as String = "aaa"
Now, i declare
Dim uc as UserControl = new aaa
my question is, can i write declaration above using value of variable a like below
Dim uc as UserControl = new a
You can do this using reflection (in the System.Reflection) namespace. For instance:
Dim t As Type = Assembly.GetExecutingAssembly().GetType("namespace.aaa")
Dim o As Object = Activator.CreateInstance(t)
Notice that you will need the full type name, including the namespace, so you may need to concatenate that to your string, for instance:
Dim namespace As String = "MyNamespace"
Dim t As Type = Assembly.GetExecutingAssembly().GetType(namespace & "." & a)
Dim o As Object = Activator.CreateInstance(t)