Why does ListBox not display objects correctly? - vb.net

I have a listbox and a class.
The class is called CTS_Warnings_List
And the listbox is called lbpropWarningFacList
Here is the class:
Public Class CTS_Warnings_List
Public _Warning As String
Public _WarnID As Integer
Public Property Warning() As String
Get
Return _Warning
End Get
Set(ByVal value As String)
_Warning = value
End Set
End Property
Public Property WarnID() As Integer
Get
Return _WarnID
End Get
Set(ByVal value As Integer)
_WarnID = value
End Set
End Property
Sub New(ByVal Warning As String, ByVal WarnID As Integer)
Me.Warning = Warning
Me.WarnID = WarnID
End Sub
End Class
So now I want to add a new item to the listbox. So I tried this:
Dim llist As New CTS_Warnings_List("K", 8)
lbpropWarningFacList.Items.Add(llist)
I also tried this:
lbpropWarningFacList.Items.Add(New CTS_Warnings_List("K", 8))
When I run the app and add these to the listbox, in each case I get this displayed in the listbox:
NameOfApplication.NameOfClass
as the display in the listbox.
What am I doing wrong?
Thank you

Listboxes only knows how to display strings.
Therefor, when you add an item of any type to the listbox, it displays the value it gets by calling the items ToString() method.
You should override the ToString() method or simply enter items as strings in the first place.
In your case, add this to your class:
Public overrides function ToString() as string
return Me.Warning & " " & Me.WarnID.ToString()
End Function

You are sending a CTS_Warnings_List object to a listbox. How does the listbox know what to do with that object? It doesn't know what to do with that object. If you want to send the properties of the object to the listbox, then you have to do something like:
lbpropWarningFacList.Items.Add(llist._WarnID & ": " & llist.Warning)

Thanks for the responses. But I found that if I add
NameOfListBox.DisplayMemeber = "PropertyOfClass"
NameOfListBox.ValueMember = "PropertyOfClass"
By just adding this it now shows the correct data in the listbox.

Related

How to enumerate My.Setting.Connectionstring in custom control Combobox

I am creating a custom control of type Textbox, and I want to create a combobox property that enumerates all the connections available in the project setting "Connection String".
I am using this code. As you can see, I commented non-working code while I was trying.
Private _formatString As String = Nothing
<Category("Display")>
<DisplayName("Connection")>
<Description("Connection string.")>
<DefaultValue("")>
<TypeConverter(GetType(FormatStringConverter))>
Public Property Connection As String
Get
Return _formatString
End Get
Set(ByVal value As String)
_formatString = value
End Set
End Property
Public Class FormatStringConverter
Inherits StringConverter
Public Overrides Function GetStandardValuesSupported(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetStandardValuesExclusive(ByVal context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetStandardValues(ByVal context As ITypeDescriptorContext) As TypeConverter.StandardValuesCollection
Dim list As List(Of String) = New List(Of String)()
'For Each current In Configuration.ConfigurationManager.ConnectionStrings
' list.Add(current.Name)
'Next
'For Each value As Configuration.SettingsPropertyValue In My.Settings.PropertyValues
' 'MessageBox.Show(value.Name & " - " & value.PropertyValue.ToString)
' 'list.Add(value.Name & " - " & value.PropertyValue.ToString)
'Next
list.Add("")
list.Add("Currency")
list.Add("Scientific Notation")
list.Add("General Number")
list.Add("Number")
list.Add("Percent")
list.Add("Time")
list.Add("Date")
Return New StandardValuesCollection(list)
End Function
End Class
I want to enumerate the connection strings in the project I placed the control in.
The custom control is in a separate project, not the same project of the form I am using.
[edit]
I cannot see the expected results at design-time. I want to choose the connection from a combobox same as tableadapter in dataset table adapter connection selection from combobox at design time. So I want to read and set the connection list available in a form belong to the project I placed the custom textbox in at design time.

Filling DataGridView with list that contains another list to populate a Comboboxcolumn. VB.NET

I'm having trouble to fill a Datagrid with a list, that contains another two.
The code I'm using for the list is:
Public Class RegistroComanda
Private _IdPedido As Integer
Public Property IdPedido() As Integer
Get
Return _IdPedido
End Get
Set(ByVal value As Integer)
_IdPedido = value
End Set
End Property
Private _id_Plato As Integer
Public Property id_Plato() As Integer
Get
Return _id_Plato
End Get
Set(ByVal value As Integer)
_id_Plato = value
End Set
End Property
Private _receta As String
Public Property Receta() As String
Get
Return _receta
End Get
Set(ByVal value As String)
_receta = value
End Set
End Property
Private _comentarios As String
Public Property comentarios() As String
Get
Return _comentarios
End Get
Set(ByVal value As String)
_comentarios = value
End Set
End Property
Private _estados As List(Of EstadoBE)
Public Property Estados() As List(Of EstadoBE)
Get
Return _estados
End Get
Set(ByVal value As List(Of EstadoBE))
_estados = value
End Set
End Property
Private _Usuarios As List(Of UsuarioBE)
Public Property usuarios() As List(Of UsuarioBE)
Get
Return _Usuarios
End Get
Set(ByVal value As List(Of UsuarioBE))
_Usuarios = value
End Set
End Property
End Class
What i need to fill with this list is a DatagridView that contains 4 DataGridTextBoxColumn for the first 4 Attributes (id_pedido, id_plato, receta, descripcion), and with the two list of objects remaining(Estados and Usuarios) i need to populate a DataGridComboBox with each (Each one contains a description that i need to show).
Here's the catch: each list inside the RegistroComanda List can contain different amount of objects. For example: If i have two records of RegistroComanda, the first one can contain 5 objects in the "Usuario" list, and the second one can contain only 2.
In that case, what i need to show when i display the combobox for the first row, are the list of 5 users, and when i display the combo for the second row i need to see only the two that corresponds to that RegistroComanda.
I've tried to set the datasource of the grid = List Of RegistroComanda, but that doesnt seem to work.
Any thougts on how i can work with this??
Thanks.

For a combobox with item = a datarow, how to point ValueMember to one of it's columns

I add items to a combobox like this:
For each R as DataRow in MyDataTable.Rows
If R("ID") > 10 then MyCombo.Items.Add(R)
Next
And now I need to set the DisplayMember and ValueMember to a column of the datarow:
MyCombo.ValueMember = R("ID")
MyCombo.DisplayMember = R("Name")
I know it doesn't make sence to to use "R" as it doesn't reference to anything at this point but it's just to make an indication of what I mean ;-)
The documentation for ValueMember says:
"A String representing a single property name of the DataSource property value, or a hierarchy of period-delimited property names that resolves to a property name of the final data-bound object"
I know I can add the rows to a new datatable and set it to the DataSource, but as you can add any object to the combobox items, it would be nice to use the rows directly, just can't figures out how to make a reference the particular column as a string.?
Maybe you cannot use a row object directly. I guess to use Valuemember you need your item objects to be wrapped in a collection which implement an ilist interface.
In the old MS-Access days combobox items had natively Display- and ValueMember properties, I've always missed that in the .Net combobox control.
My work-around is to use this class, which then can be used for all your ComboBoxes:
Class oComboItems
Public items As New List(Of oDVpairs)
Class oDVpairs
Implements IComparable(Of oDVpairs)
Private myDM As String
Private myVM As Object
Sub New(DM As String, VM As Object)
myDM = DM
myVM = VM
End Sub
Public ReadOnly Property DM() As String
Get
Return myDM
End Get
End Property
Public ReadOnly Property VM() As Object
Get
Return myVM
End Get
End Property
Public Function CompareTo(other As oDVpairs) As Integer Implements IComparable(Of oDVpairs).CompareTo
Return Me.myDM.CompareTo(other.myDM)
End Function
End Class
Public Sub AddItems(DisplayMember As String, ValueMemeber As Object)
items.Add(New oDVpairs(DisplayMember, ValueMemeber))
End Sub
Public ReadOnly Property DisplayMember() As String
Get
Return "DM"
End Get
End Property
Public ReadOnly Property ValueMember() As Object
Get
Return "VM"
End Get
End Property
End Class
And now add my datarows(or any other objects) to the ComboBox:
Dim CI As New oComboItems
For Each R As DataRow In DT_U.Rows
If R("medlnr") > 10 Then
CI.AddItems(R("name"), R("ID"))
end if
Next
CI.items.Sort()
MyCombo.DataSource = CI.Items
MyCombo.DisplayMember = CI.DisplayMember
MyCombo.ValueMember = CI.ValueMember

How to convert a list of different data types into string in vb.net

I created a list using a class which has different data types. It is populated as follows:
[1/16/2015 10:30:14 PM] 241.167.2.72:5
[1/17/2015 11:30:06 PM] 100.133.2.55:6
[1/18/2015 12:30:33 PM] 206.140.3.10:7
Now I just want to display this in a textbox on a page. But get this:
ClientWebApp.DedicatedServerApi.Formatted_DDoS_SampleFlows
ClientWebApp.DedicatedServerApi.Formatted_DDoS_SampleFlows
ClientWebApp.DedicatedServerApi.Formatted_DDoS_SampleFlows
I'm using this line of code with the ToString()...but obviously it is not correct.
strSampleFlowLine = formattedDDoSSampleFlow.ToString() & vbCrLf & vbCrLf
Any Ideas?
The code (and classes) that attempts to populate the text box on the page:
Dim ddosDetailsInfoResult As New DedicatedServerApi.DDoSDetailsInfoResult
Dim formattedDDoSSampleFlow As New DedicatedServerApi.Formatted_DDoS_SampleFlows
' Create a list field - an array of the DDoS sample flows.
Dim formattedDDoSSampleFlowsList As New List(Of DedicatedServerApi.Formatted_DDoS_SampleFlows)
If ddosDetailsInfoResult.DDoS_Details.Formattted_DDoS_SampleFlows_List.Count > 0 Then
' Note: had to add .ToList() as it was giving a syntax error: cannot be converted to a 1-dimensional array.
' Does not make sense as the "Formattted_DDoS_SampleFlows_List" variable is defined exactly like the receiving field.
formattedDDoSSampleFlowsList = ddosDetailsInfoResult.DDoS_Details.Formattted_DDoS_SampleFlows_List.ToList()
For Each formattedDDoSSampleFlow In formattedDDoSSampleFlowsList
' Example of entry in the list: [1/16/2015 10:30:14 PM] 241.167.2.72:5
strSampleFlowLine = formattedDDoSSampleFlow.ToString() & vbCrLf & vbCrLf
' Build the sample flow list textbox.
txtGDHDApiSampleFlowList.Text = txtGDHDApiSampleFlowList.Text & strSampleFlowLine
Next
Else
'An empty list.
txtGDHDApiSampleFlowList.Text = ""
End If
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Holds formatted DDoS sample flows settings.
' [1/16/2015 10:30:14 PM] 241.167.2.72:5
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class Formatted_DDoS_SampleFlows
Private _open_bracket_delimeter As String
Public Property Open_Bracket_Delimeter() As String
Get
Return _open_bracket_delimeter
End Get
Set(value As String)
_open_bracket_delimeter = value
End Set
End Property
Private _time_received As Date
Public Property Time_Received() As Date
Get
Return _time_received
End Get
Set(value As Date)
_time_received = value
End Set
End Property
Private _close_backet_and_space_delimeter As String
Public Property Close_Bracket_And_Space_Delimeter() As String
Get
Return _close_backet_and_space_delimeter
End Get
Set(value As String)
_close_backet_and_space_delimeter = value
End Set
End Property
Private _source_ip As String
Public Property Source_IP() As String
Get
Return _source_ip
End Get
Set(value As String)
_source_ip = value
End Set
End Property
Private _colon1_delimeter As String
Public Property Colon1_Delimeter() As String
Get
Return _colon1_delimeter
End Get
Set(value As String)
_colon1_delimeter = value
End Set
End Property
Private _source_port As String
Public Property Source_Port() As String
Get
Return _source_port
End Get
Set(value As String)
_source_port = value
End Set
End Property
End Class
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Holds DDoSDetailsInfoResult.
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class DDoSDetailsInfoResult
Private _message As String
Public Property Message() As String
Get
Return _message
End Get
Set(value As String)
_message = value
End Set
End Property
Private _ddos_details As New DDoS_Details
Public Property DDoS_Details() As DDoS_Details
Get
Return _ddos_details
End Get
Set(ByVal value As DDoS_Details)
_ddos_details = value
End Set
End Property
End Class
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Holds DDoS details.
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Public Class DDoS_Details
Private _formatted_ddos_sampleflows_list As New List(Of Formatted_DDoS_SampleFlows)
Public Property Formattted_DDoS_SampleFlows_List() As List(Of Formatted_DDoS_SampleFlows)
Get
Return _formatted_ddos_sampleflows_list
End Get
Set(ByVal value As List(Of Formatted_DDoS_SampleFlows))
_formatted_ddos_sampleflows_list = value
End Set
End Property
End Class
Your Formatted_DDoS_SampleFlows class need to define what ToString does.
Something like:
Class Formatted_DDoS_SampleFlows
Public Overrides ToString() As String
Return _open_bracket_delimeter & _time_received.ToString() & _close_backet_and_space_delimeter & _source_ip
End Sub
End Class
use CDate to convert the original value into a Date. The ToString is being operated on the entire class. Then use String.Format to output the date into the correct format you want. Should look something closer to this:
strSampleFlowLine = String.Format("MM/dd/yyyy", CDate(formattedDDoSSampleFlow.something)) & vbCrLf & vbCrLf

Variable Value Passing to another Form, VB.NET

I do have Two Public Variables, each are from two different forms..
Form1.VB
Public UserNo As String
Form2.VB
Public MyUserNo As String
On my Form2.VB File, I assign value to the UserNo of Form1.VB
Form1.UserNo = MyUserNo
Whenever I access the Form1.VB, the Value of the MyUserNo got empty, What should I do? Both Forms are not closed.
I also tried to re-assign the value when I need to use it on the Form1.VB
UserNo = Form2.MyUserNo
First Correct is Syntax is:
Form1.VB
Public UserNo As String
Form2.VB
Public MyUserNo As String
In Form1
UserNo=Form2.MyUserNo
Second Thing:
First you should store some value in MyUserNo before storing it into UserNo.
That's why you are getting empty value.
Make the variable static/Shared and try again,it should work.
You can have more than one instance of a form, you know. Forms are objects, just like anything else. You need a variable in each form to hold a reference to the instance of each form that you are using.
If you don't call InitializeComponent(), your complete GUI is not going to render.
...
InitializeComponent()
Form1.UserNo = MyUserNo
...
Try this:
[Form1.UserNo = form2.MyUserNo]
it work
add it to form what you want value in next form
Function getvariable()
Return (VARIABLE)
End Function
In next form to get VARIABLE
VARIABLE2 = Form.getvariable()
Use variable value as Public
For Example
In Form1:
Public Str1 as String
So in Form2 you can use:
Str2=Form1.Str1
'In frmMain i Start frmBase
Dim f As New frmBase
f.Text = "New caption for frmBase"
f.ShowDialog(Me)
'in frmBase i read and write a textbox
Dim str As String = CType(Me.Owner, frmMain).txtRicetta.Text
Console.WriteLine(str)
CType(Me.Owner, frmMain).txtRicetta.Text = "12345"
Console.WriteLine(CType(Me.Owner, frmMain).txtRicetta.Text)
What you need to do is create your variables in a module as private, then generate some assessors for them.
Example:
Module modVariables
Private strUserNoSTR as String = New String(String.Empty)
Public Property getUserNoSTR() As String
Get
Return strUserNoSTR
End Get
Set(ByVal strUserNo As String)
strUserNoSTR = strUserNo
End Set
End Property
Private strMyUserNoSTR As String = New String(String.Empty)
Public Property getMyUserNoSTR As String
Get
Return strMyUserNoSTR
End Get
Set(ByVal strMyUserNo As String)
strMyUserNoSTR = strMyUserNo
End Set
End Property
End Module
After you generate the getter and setter public methods you can notice that your two private variables are within them, when you create the variables they are accessible from any form.
The reason why you keep losing the variables value is because when you try to access its value from another form (basically you're calling it from another class) the compiler has to create a new instance of that variable and when that happened the variable is set back to its original value which is of type empty string. Calling them from a module keeps them from being re-instantiated.
How To Use Them:
To get the value of strMyUserNo you call the getter of strMyUserNoSTR:
TextBox.Text = getMyUserNoSTR
To set the value of strMyUserNoSTR:
getMyUserNoSTR = someValuePlacedInThisVariable 'This sets it's value.
TextBox.Text = getMyUserNoSTR 'Now it's value is someValuePlacedInThisVariable.