Pass string to Dialog in VB.net - vb.net

I have a form and from this I call
dialogPrintDiet.ShowDialog()
which launchs my dialog. I need to pass a string value and need the easiest way to do this in VB.NET .

Try properties, for example setting some text boxes in your dialog:
Property FirstName() As String
Get
Return txtFirstName.Text
End Get
Set(ByVal Value As String)
txtFirstName.Text = Value
End Set
End Property
Property LastName() As String
Get
Return txtLastName.Text
End Get
Set(ByVal Value As String)
txtLastName.Text = Value
End Set
End Property

You can either add a property to the form or you can add a parameter to your form's constructor.
An example of the first method would look like (where Message is the name of the property)
frm.Message = "Some text"
An example of the second method would look like
Dim frm As New SampleForm ( "Some text" )
Your form code would be something like
Public Class SampleForm
Private someMessage As String
Public Sub New(ByVal msg As String)
InitializeComponent()
If Not (String.IsNullOrEmpty(msg)) Then
someMessage = msg
End If
End Sub
Property Message() As String
Get
Return someMessage
End Get
Set(ByVal Value As String)
someMessage = Value
End Set
End Property
End Class

Related

How can I assign a property value using 'With {...}' syntax?

In the new Sub New , I want to Insert value of property
Ex:
1-Class
Public Class A
Property Name As String
Sub New()
MsgBox(Name) 'Empty
End Sub
End Class
2- Form
Dim a As New A With {.Name = "ABCDE"} 'MsgBox Empty
Dim a As New A With {.Name = "ABCDE"} 'MsgBox Empty
The message box will be empty, because in the above statement the order of execution is:
First new gets called and all the statements inside the subroutine new gets executed.
Then the initialization step happens for the variables in with statement.
Now the alternate solution, if you want to print the name is during initialization:
You can print during property set, as shown below. (you can use a bool variable for not printing further when name is set a value.)
Public Class A
'PROPERTY GET AND SET
Private _name As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
MsgBox(_name) 'PRINT HERE
End Set
End Property
'NEW SUBROUTINE
Sub New()
End Sub
End Class
calling:
Dim a1 As New A With
{.Name = "ABCDE"}

UserControl + ArrayList + vb.net

When i get the text of a listbox i write
ListBox1.Items(2).ToString()
What is the code that allows me to do this?
UserControl.positioniwant(2).title
UserControl.positioniwant(1).msg
It's called an "Indexed Property":
Public Class Foo
Public Property Position(ByVal index As Integer) As String
Get
Return _position(index)
End Get
Set(ByVal Value As String)
_position(index) = Value
End Set
End Property
Private _position(9) As String
End Class

Deserializing nested json in vb.net

I'm new to json and am working on deserializing some nested json into an object. The outer object works fine, but I'm not getting any values for the inner object. I've tried several solutions including using list object, collections, the datacontractserializer, but nothing seems to work. I think I'm probably missing something obvious. Here is what I have now:
The json string looks like this:
{"type":"lookup","message":"Success","version":0.1,"user":{"loginName":"username","vendor":null}}
My code is as follows:
<Serializable()> Public Class LookupReturn
Private _Type As String = ""
Private _Message As String = ""
Private _Version As String = ""
Private _user As New jsonUser
Public Property Type() As String
Get
Return _Type
End Get
Set(ByVal value As String)
_Type = value
End Set
End Property
Public Property Message() As String
Get
Return _Message
End Get
Set(ByVal value As String)
_Message = value
End Set
End Property
Public Property Version() As String
Get
Return _Version
End Get
Set(ByVal value As String)
_Version = value
End Set
End Property
Public Property Userobj() As jsonUser
Get
Return _user
End Get
Set(ByVal value As jsonUser)
_user = value
End Set
End Property
End Class
<Serializable()> Public Class jsonUser
Private _loginName As String = ""
Private _vendor As String = ""
Public Property loginName() As String
Get
Return _loginName
End Get
Set(ByVal value As String)
_loginName = value
End Set
End Property
Public Property vendor() As String
Get
Return _vendor
End Get
Set(ByVal value As String)
_vendor = value
End Set
End Property
End Class
Dim _Json As New JavaScriptSerializer()
Dim _Message as string = "{"type":"lookup","message":"Success","version":0.1,"user"{"loginName":"username","vendor":null}}"
Dim returnData As LookupReturn = _Json.Deserialize(Of LookupReturn)(_Message)
I'm geting data in the type, message, version values for the LookupReturn object, and it's returning an object for the user item, but the value for loginName is an empty string.
Any help would be appreciated!
Thanks!
After working with this some more, I realized that the problem was that my nested object was not named as it should be in the Get/Set section of my code. Because I was calling it "Userobj" instead of "user" as it was called in the jSon, the deserializer wasn't able to work properly. I changed the name to "user" and all worked well!

Storing a value for each ComboBox item

In lack of a Value property I planned to use Classes for storing Text and Value properties for my ComboBox items. So far I've succeeded.
Here is my class:
Public Class clCombobox
Public cname As String
Public cvalue As Integer
Public Property Display() As String
Get
Return Me.cname
End Get
Set(ByVal value As String)
Me.cname = value
End Set
End Property
Public Property Value() As String
Get
Return Me.cvalue
End Get
Set(ByVal value As String)
Me.cvalue = value
End Set
End Property
Public Sub New(ByVal name As String, ByVal value As String)
cname = name
cvalue = value
End Sub
Public Overrides Function ToString() As String
Return cname
End Function
End Class
Data is being added to the ComboBox like this:
cmbComboxBox.Items.Add(New clCombobox("Text", 1))
It seems like this works so far. But how do I get data back. Like if I want the value of the selected CheckBox item?
I tried using:
CType(cmbCombobox.SelectedItem, clCombobox).Value()
Did not work.
As per the documentation, use the SelectedItem property to retrieve the object that you stored in it.
Code to retrieve value you want:
Dim selectedItem as clCombobox = CType(cmbComboBox.SelectedItem, clCombobox)
Dim value As Integer = selectedItem.cvalue

how to add value to combobox item

How can I add data value of each item to combobox in Visual Basic 2010?
Like html drop-down box.
Or is there anyway to add values to each item ?
I am adding item from MySQL database like this:
Command = New MySqlCommand("SELECT * FROM `maillist` WHERE l_id = '" & id & "'", connection)
Command.CommandTimeout = 30
Reader = Command.ExecuteReader()
If Reader.HasRows = True Then
While Reader.Read()
ComboBox1.Items.Add(Reader("name"))
End While
End If
I need to add Reader("ID") as value of each item...
Although this question is 5 years old I have come across a nice solution.
Use the 'DictionaryEntry' object to pair keys and values.
Set the 'DisplayMember' and 'ValueMember' properties to:
Me.myComboBox.DisplayMember = "Key"
Me.myComboBox.ValueMember = "Value"
To add items to the ComboBox:
Me.myComboBox.Items.Add(New DictionaryEntry("Text to be displayed", 1))
To retreive items like this:
MsgBox(Me.myComboBox.SelectedItem.Key & " " & Me.myComboBox.SelectedItem.Value)
I am assuming that you are wanting to add items to a ComboBox on an Windows form. Although Klaus is on the right track I believe that the ListItem class is a member of the System.Web.UI.WebControls namespace. So you shouldn't be using it in a Windows forms solution. You can, however, create your own class that you can use in its place.
Create a simple class called MyListItem (or whatever name you choose) like this:
Public Class MyListItem
Private mText As String
Private mValue As String
Public Sub New(ByVal pText As String, ByVal pValue As String)
mText = pText
mValue = pValue
End Sub
Public ReadOnly Property Text() As String
Get
Return mText
End Get
End Property
Public ReadOnly Property Value() As String
Get
Return mValue
End Get
End Property
Public Overrides Function ToString() As String
Return mText
End Function
End Class
Now when you want to add the items to your ComboBox you can do it like this:
myComboBox.Items.Add(New MyListItem("Text to be displayed", "value of the item"))
Now when you want to retrieve the value of the selected item from your ComboBox you can do it like this:
Dim oItem As MyListItem = CType(myComboBox.SelectedItem, MyListItem)
MessageBox.Show("The Value of the Item selected is: " & oItem.Value)
One of the keys here is overriding the ToString method in the class. This is where the ComboBox gets the text that is displayed.
Matt made an excellent point, in his comment below, about using Generics to make this even more flexible. So I wondered what that would look like.
Here's the new and improved GenericListItem class:
Public Class GenericListItem(Of T)
Private mText As String
Private mValue As T
Public Sub New(ByVal pText As String, ByVal pValue As T)
mText = pText
mValue = pValue
End Sub
Public ReadOnly Property Text() As String
Get
Return mText
End Get
End Property
Public ReadOnly Property Value() As T
Get
Return mValue
End Get
End Property
Public Overrides Function ToString() As String
Return mText
End Function
End Class
And here is how you would now add Generic items to your ComboBox. In this case an Integer:
Me.myComboBox.Items.Add(New GenericListItem(Of Integer)("Text to be displayed", 1))
And now the retrieval of the item:
Dim oItem As GenericListItem(Of Integer) = CType(Me.myComboBox.SelectedItem, GenericListItem(Of Integer))
MessageBox.Show("The value of the Item selected is: " & oItem.Value.ToString())
Keep in mind that the type Integer can be any type of object or value type. If you want it to be an object from one of your own custom classes that's fine. Basically anything goes with this approach.
If you want to use SelectedValue then your combobox must be databound.
To set up the combobox:
ComboBox1.DataSource = GetMailItems()
ComboBox1.DisplayMember = "Name"
ComboBox1.ValueMember = "ID"
To get the data:
Function GetMailItems() As List(Of MailItem)
Dim mailItems = New List(Of MailItem)
Command = New MySqlCommand("SELECT * FROM `maillist` WHERE l_id = '" & id & "'", connection)
Command.CommandTimeout = 30
Reader = Command.ExecuteReader()
If Reader.HasRows = True Then
While Reader.Read()
mailItems.Add(New MailItem(Reader("ID"), Reader("name")))
End While
End If
Return mailItems
End Function
Public Class MailItem
Public Sub New(ByVal id As Integer, ByVal name As String)
mID = id
mName = name
End Sub
Private mID As Integer
Public Property ID() As Integer
Get
Return mID
End Get
Set(ByVal value As Integer)
mID = value
End Set
End Property
Private mName As String
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
End Class
Instead of adding Reader("Name") you add a new ListItem. ListItem has a Text and a Value property that you can set.
Yeah, for most cases, you don't need to create a class with getters and setters. Just create a new Dictionary and bind it to the data source. Here's an example in VB using a for loop to set the DisplayMember and ValueMember of a combo box from a list:
Dim comboSource As New Dictionary(Of String, String)()
cboMenu.Items.Clear()
For I = 0 To SomeList.GetUpperBound(0)
comboSource.Add(SomeList(I).Prop1, SomeList(I).Prop2)
Next I
cboMenu.DataSource = New BindingSource(comboSource, Nothing)
cboMenu.DisplayMember = "Value"
cboMenu.ValueMember = "Key"
Then you can set up a data grid view's rows according to the value or whatever you need by calling a method on click:
Private Sub cboMenu_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboMenu.SelectionChangeCommitted
SetListGrid(cboManufMenu.SelectedValue)
End Sub
Now you can use insert method instead add
' Visual Basic
CheckedListBox1.Items.Insert(0, "Copenhagen")