how to convert combobox item into value and calculate it
I want to convert selected item in combobox into a number and calculate it.
I got message like this when i try to start my vb.net program.
Conversion from string "< 20 " to type 'Integer' is not valid
Any help is greatly appreciated.
this is my code:
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim LHR As Integer
Dim TipeRetak As Integer
Dim LbRetak As Integer
Dim LuKer As Integer
Dim Alur As Integer
Dim Tambal As Integer
Dim Kasar As Integer
Dim amblas As Integer
Select Case ComboLHR.SelectedIndex
Case "< 20 "
LHR = 0
Case "20 - 50"
LHR = 1
Case "50 - 200"
LHR = 2
Case "200 - 500"
LHR = 3
Case "500 - 2000"
LHR = 4
Case "2000 - 5000"
LHR = 5
Case "5000 - 20000"
LHR = 6
Case "20000 - 50000"
LHR = 7
Case "> 50000"
LHR = 8
End Select
Select Case ComboTipeRetak.SelectedIndex
Case "Buaya"
TipeRetak = 5
Case "Acak"
TipeRetak = 4
Case "Melintang"
TipeRetak = 3
Case "Memanjang"
TipeRetak = 1
Case "Tidak Ada"
TipeRetak = 1
End Select
Select Case ComboLebarRetak.SelectedIndex
Case "> 2 mm"
LbRetak = 3
Case "1 - 2 mm"
LbRetak = 2
Case "< 1 mm"
LbRetak = 1
Case "Tidak Ada"
LbRetak = 0
End Select
Select Case ComboLuasKerusakan.SelectedIndex
Case "> 30%"
LuKer = 3
Case "10 - 30%"
LuKer = 2
Case "< 10%"
LuKer = 1
Case "0"
LuKer = 0
End Select
Select Case ComboKedalamanAlur.SelectedIndex
Case "> 20 mm"
Alur = 7
Case "11 - 20 mm"
Alur = 5
Case "6 - 10 mm"
Alur = 3
Case "0 - 5 mm"
Alur = 1
Case "Tidak Ada"
Alur = 0
End Select
Select Case ComboTambal.SelectedIndex
Case ">30 %"
Tambal = 3
Case "20 - 30 %"
Tambal = 2
Case "10 - 20%"
Tambal = 1
Case "< 10%"
Tambal = 0
End Select
Select Case ComboKekasaran.SelectedIndex
Case "Desintegration"
Kasar = 4
Case "Pelepasan Butir"
Kasar = 3
Case "Rough(Hungry)"
Kasar = 2
Case "Fatty"
Kasar = 1
Case "Close Texture"
Kasar = 0
End Select
Select Case ComboAmblas.SelectedIndex
Case "> 5/100 m"
amblas = 4
Case "2 - 5/100 m"
amblas = 2
Case "0 - 2/100 m"
amblas = 1
Case "Tidak Ada"
amblas = 0
End Select
Dim comand As New MySqlCommand("INSERT INTO `tb_bnkt`(`nomor`, `Nama`, `kondisi prioritas`) VALUES (@nomor,@NamaRuas,@kondisi)", Connector)
comand.Parameters.Add("@nomor", MySqlDbType.VarChar).Value = TextNomor.Text
comand.Parameters.Add("@NamaRuas", MySqlDbType.VarChar).Value = ComboNamaRuas.Text
comand.Parameters.Add("@kondisi", MySqlDbType.VarChar).Value = 17 - (Val(LHR + TipeRetak + LbRetak + LuKer + Alur + Tambal + Kasar + amblas))
If comand.ExecuteNonQuery() = 1 Then
MessageBox.Show("Data disimpan")
Loading()
TextNomor.Clear()
ComboNamaRuas.Text = String.Empty
ComboLHR.Text = String.Empty
ComboTipeRetak.Text = String.Empty
ComboLebarRetak.Text = String.Empty
ComboLuasKerusakan.Text = String.Empty
ComboKedalamanAlur.Text = String.Empty
ComboTambal.Text = String.Empty
ComboKekasaran.Text = String.Empty
ComboAmblas.Text = String.Empty
Else
MessageBox.Show("Error")
End If
End Sub
vb.net
add a comment |
I want to convert selected item in combobox into a number and calculate it.
I got message like this when i try to start my vb.net program.
Conversion from string "< 20 " to type 'Integer' is not valid
Any help is greatly appreciated.
this is my code:
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim LHR As Integer
Dim TipeRetak As Integer
Dim LbRetak As Integer
Dim LuKer As Integer
Dim Alur As Integer
Dim Tambal As Integer
Dim Kasar As Integer
Dim amblas As Integer
Select Case ComboLHR.SelectedIndex
Case "< 20 "
LHR = 0
Case "20 - 50"
LHR = 1
Case "50 - 200"
LHR = 2
Case "200 - 500"
LHR = 3
Case "500 - 2000"
LHR = 4
Case "2000 - 5000"
LHR = 5
Case "5000 - 20000"
LHR = 6
Case "20000 - 50000"
LHR = 7
Case "> 50000"
LHR = 8
End Select
Select Case ComboTipeRetak.SelectedIndex
Case "Buaya"
TipeRetak = 5
Case "Acak"
TipeRetak = 4
Case "Melintang"
TipeRetak = 3
Case "Memanjang"
TipeRetak = 1
Case "Tidak Ada"
TipeRetak = 1
End Select
Select Case ComboLebarRetak.SelectedIndex
Case "> 2 mm"
LbRetak = 3
Case "1 - 2 mm"
LbRetak = 2
Case "< 1 mm"
LbRetak = 1
Case "Tidak Ada"
LbRetak = 0
End Select
Select Case ComboLuasKerusakan.SelectedIndex
Case "> 30%"
LuKer = 3
Case "10 - 30%"
LuKer = 2
Case "< 10%"
LuKer = 1
Case "0"
LuKer = 0
End Select
Select Case ComboKedalamanAlur.SelectedIndex
Case "> 20 mm"
Alur = 7
Case "11 - 20 mm"
Alur = 5
Case "6 - 10 mm"
Alur = 3
Case "0 - 5 mm"
Alur = 1
Case "Tidak Ada"
Alur = 0
End Select
Select Case ComboTambal.SelectedIndex
Case ">30 %"
Tambal = 3
Case "20 - 30 %"
Tambal = 2
Case "10 - 20%"
Tambal = 1
Case "< 10%"
Tambal = 0
End Select
Select Case ComboKekasaran.SelectedIndex
Case "Desintegration"
Kasar = 4
Case "Pelepasan Butir"
Kasar = 3
Case "Rough(Hungry)"
Kasar = 2
Case "Fatty"
Kasar = 1
Case "Close Texture"
Kasar = 0
End Select
Select Case ComboAmblas.SelectedIndex
Case "> 5/100 m"
amblas = 4
Case "2 - 5/100 m"
amblas = 2
Case "0 - 2/100 m"
amblas = 1
Case "Tidak Ada"
amblas = 0
End Select
Dim comand As New MySqlCommand("INSERT INTO `tb_bnkt`(`nomor`, `Nama`, `kondisi prioritas`) VALUES (@nomor,@NamaRuas,@kondisi)", Connector)
comand.Parameters.Add("@nomor", MySqlDbType.VarChar).Value = TextNomor.Text
comand.Parameters.Add("@NamaRuas", MySqlDbType.VarChar).Value = ComboNamaRuas.Text
comand.Parameters.Add("@kondisi", MySqlDbType.VarChar).Value = 17 - (Val(LHR + TipeRetak + LbRetak + LuKer + Alur + Tambal + Kasar + amblas))
If comand.ExecuteNonQuery() = 1 Then
MessageBox.Show("Data disimpan")
Loading()
TextNomor.Clear()
ComboNamaRuas.Text = String.Empty
ComboLHR.Text = String.Empty
ComboTipeRetak.Text = String.Empty
ComboLebarRetak.Text = String.Empty
ComboLuasKerusakan.Text = String.Empty
ComboKedalamanAlur.Text = String.Empty
ComboTambal.Text = String.Empty
ComboKekasaran.Text = String.Empty
ComboAmblas.Text = String.Empty
Else
MessageBox.Show("Error")
End If
End Sub
vb.net
TheSelectedIndex
is the index of the item selected, which is obviously not what you're checking there. If you want to check the text then use theText
and if you use the SelectedIndex` then check the index. That said, in the first case at least, you don't need aSelect Case
because you can simply assign theSelectedIndex
directly toLHR
.
– jmcilhinney
Nov 28 '18 at 5:29
Also make use of the text property of the combo box instead of it's selected index
– preciousbetine
Nov 28 '18 at 5:35
thank you @jmcilhinney , i changed SelectedIndex into Selecteditem and it works.
– Student
Nov 28 '18 at 6:17
add a comment |
I want to convert selected item in combobox into a number and calculate it.
I got message like this when i try to start my vb.net program.
Conversion from string "< 20 " to type 'Integer' is not valid
Any help is greatly appreciated.
this is my code:
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim LHR As Integer
Dim TipeRetak As Integer
Dim LbRetak As Integer
Dim LuKer As Integer
Dim Alur As Integer
Dim Tambal As Integer
Dim Kasar As Integer
Dim amblas As Integer
Select Case ComboLHR.SelectedIndex
Case "< 20 "
LHR = 0
Case "20 - 50"
LHR = 1
Case "50 - 200"
LHR = 2
Case "200 - 500"
LHR = 3
Case "500 - 2000"
LHR = 4
Case "2000 - 5000"
LHR = 5
Case "5000 - 20000"
LHR = 6
Case "20000 - 50000"
LHR = 7
Case "> 50000"
LHR = 8
End Select
Select Case ComboTipeRetak.SelectedIndex
Case "Buaya"
TipeRetak = 5
Case "Acak"
TipeRetak = 4
Case "Melintang"
TipeRetak = 3
Case "Memanjang"
TipeRetak = 1
Case "Tidak Ada"
TipeRetak = 1
End Select
Select Case ComboLebarRetak.SelectedIndex
Case "> 2 mm"
LbRetak = 3
Case "1 - 2 mm"
LbRetak = 2
Case "< 1 mm"
LbRetak = 1
Case "Tidak Ada"
LbRetak = 0
End Select
Select Case ComboLuasKerusakan.SelectedIndex
Case "> 30%"
LuKer = 3
Case "10 - 30%"
LuKer = 2
Case "< 10%"
LuKer = 1
Case "0"
LuKer = 0
End Select
Select Case ComboKedalamanAlur.SelectedIndex
Case "> 20 mm"
Alur = 7
Case "11 - 20 mm"
Alur = 5
Case "6 - 10 mm"
Alur = 3
Case "0 - 5 mm"
Alur = 1
Case "Tidak Ada"
Alur = 0
End Select
Select Case ComboTambal.SelectedIndex
Case ">30 %"
Tambal = 3
Case "20 - 30 %"
Tambal = 2
Case "10 - 20%"
Tambal = 1
Case "< 10%"
Tambal = 0
End Select
Select Case ComboKekasaran.SelectedIndex
Case "Desintegration"
Kasar = 4
Case "Pelepasan Butir"
Kasar = 3
Case "Rough(Hungry)"
Kasar = 2
Case "Fatty"
Kasar = 1
Case "Close Texture"
Kasar = 0
End Select
Select Case ComboAmblas.SelectedIndex
Case "> 5/100 m"
amblas = 4
Case "2 - 5/100 m"
amblas = 2
Case "0 - 2/100 m"
amblas = 1
Case "Tidak Ada"
amblas = 0
End Select
Dim comand As New MySqlCommand("INSERT INTO `tb_bnkt`(`nomor`, `Nama`, `kondisi prioritas`) VALUES (@nomor,@NamaRuas,@kondisi)", Connector)
comand.Parameters.Add("@nomor", MySqlDbType.VarChar).Value = TextNomor.Text
comand.Parameters.Add("@NamaRuas", MySqlDbType.VarChar).Value = ComboNamaRuas.Text
comand.Parameters.Add("@kondisi", MySqlDbType.VarChar).Value = 17 - (Val(LHR + TipeRetak + LbRetak + LuKer + Alur + Tambal + Kasar + amblas))
If comand.ExecuteNonQuery() = 1 Then
MessageBox.Show("Data disimpan")
Loading()
TextNomor.Clear()
ComboNamaRuas.Text = String.Empty
ComboLHR.Text = String.Empty
ComboTipeRetak.Text = String.Empty
ComboLebarRetak.Text = String.Empty
ComboLuasKerusakan.Text = String.Empty
ComboKedalamanAlur.Text = String.Empty
ComboTambal.Text = String.Empty
ComboKekasaran.Text = String.Empty
ComboAmblas.Text = String.Empty
Else
MessageBox.Show("Error")
End If
End Sub
vb.net
I want to convert selected item in combobox into a number and calculate it.
I got message like this when i try to start my vb.net program.
Conversion from string "< 20 " to type 'Integer' is not valid
Any help is greatly appreciated.
this is my code:
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim LHR As Integer
Dim TipeRetak As Integer
Dim LbRetak As Integer
Dim LuKer As Integer
Dim Alur As Integer
Dim Tambal As Integer
Dim Kasar As Integer
Dim amblas As Integer
Select Case ComboLHR.SelectedIndex
Case "< 20 "
LHR = 0
Case "20 - 50"
LHR = 1
Case "50 - 200"
LHR = 2
Case "200 - 500"
LHR = 3
Case "500 - 2000"
LHR = 4
Case "2000 - 5000"
LHR = 5
Case "5000 - 20000"
LHR = 6
Case "20000 - 50000"
LHR = 7
Case "> 50000"
LHR = 8
End Select
Select Case ComboTipeRetak.SelectedIndex
Case "Buaya"
TipeRetak = 5
Case "Acak"
TipeRetak = 4
Case "Melintang"
TipeRetak = 3
Case "Memanjang"
TipeRetak = 1
Case "Tidak Ada"
TipeRetak = 1
End Select
Select Case ComboLebarRetak.SelectedIndex
Case "> 2 mm"
LbRetak = 3
Case "1 - 2 mm"
LbRetak = 2
Case "< 1 mm"
LbRetak = 1
Case "Tidak Ada"
LbRetak = 0
End Select
Select Case ComboLuasKerusakan.SelectedIndex
Case "> 30%"
LuKer = 3
Case "10 - 30%"
LuKer = 2
Case "< 10%"
LuKer = 1
Case "0"
LuKer = 0
End Select
Select Case ComboKedalamanAlur.SelectedIndex
Case "> 20 mm"
Alur = 7
Case "11 - 20 mm"
Alur = 5
Case "6 - 10 mm"
Alur = 3
Case "0 - 5 mm"
Alur = 1
Case "Tidak Ada"
Alur = 0
End Select
Select Case ComboTambal.SelectedIndex
Case ">30 %"
Tambal = 3
Case "20 - 30 %"
Tambal = 2
Case "10 - 20%"
Tambal = 1
Case "< 10%"
Tambal = 0
End Select
Select Case ComboKekasaran.SelectedIndex
Case "Desintegration"
Kasar = 4
Case "Pelepasan Butir"
Kasar = 3
Case "Rough(Hungry)"
Kasar = 2
Case "Fatty"
Kasar = 1
Case "Close Texture"
Kasar = 0
End Select
Select Case ComboAmblas.SelectedIndex
Case "> 5/100 m"
amblas = 4
Case "2 - 5/100 m"
amblas = 2
Case "0 - 2/100 m"
amblas = 1
Case "Tidak Ada"
amblas = 0
End Select
Dim comand As New MySqlCommand("INSERT INTO `tb_bnkt`(`nomor`, `Nama`, `kondisi prioritas`) VALUES (@nomor,@NamaRuas,@kondisi)", Connector)
comand.Parameters.Add("@nomor", MySqlDbType.VarChar).Value = TextNomor.Text
comand.Parameters.Add("@NamaRuas", MySqlDbType.VarChar).Value = ComboNamaRuas.Text
comand.Parameters.Add("@kondisi", MySqlDbType.VarChar).Value = 17 - (Val(LHR + TipeRetak + LbRetak + LuKer + Alur + Tambal + Kasar + amblas))
If comand.ExecuteNonQuery() = 1 Then
MessageBox.Show("Data disimpan")
Loading()
TextNomor.Clear()
ComboNamaRuas.Text = String.Empty
ComboLHR.Text = String.Empty
ComboTipeRetak.Text = String.Empty
ComboLebarRetak.Text = String.Empty
ComboLuasKerusakan.Text = String.Empty
ComboKedalamanAlur.Text = String.Empty
ComboTambal.Text = String.Empty
ComboKekasaran.Text = String.Empty
ComboAmblas.Text = String.Empty
Else
MessageBox.Show("Error")
End If
End Sub
vb.net
vb.net
edited Nov 28 '18 at 8:30
equisde
3,30622545
3,30622545
asked Nov 28 '18 at 5:22
StudentStudent
92
92
TheSelectedIndex
is the index of the item selected, which is obviously not what you're checking there. If you want to check the text then use theText
and if you use the SelectedIndex` then check the index. That said, in the first case at least, you don't need aSelect Case
because you can simply assign theSelectedIndex
directly toLHR
.
– jmcilhinney
Nov 28 '18 at 5:29
Also make use of the text property of the combo box instead of it's selected index
– preciousbetine
Nov 28 '18 at 5:35
thank you @jmcilhinney , i changed SelectedIndex into Selecteditem and it works.
– Student
Nov 28 '18 at 6:17
add a comment |
TheSelectedIndex
is the index of the item selected, which is obviously not what you're checking there. If you want to check the text then use theText
and if you use the SelectedIndex` then check the index. That said, in the first case at least, you don't need aSelect Case
because you can simply assign theSelectedIndex
directly toLHR
.
– jmcilhinney
Nov 28 '18 at 5:29
Also make use of the text property of the combo box instead of it's selected index
– preciousbetine
Nov 28 '18 at 5:35
thank you @jmcilhinney , i changed SelectedIndex into Selecteditem and it works.
– Student
Nov 28 '18 at 6:17
The
SelectedIndex
is the index of the item selected, which is obviously not what you're checking there. If you want to check the text then use the Text
and if you use the SelectedIndex` then check the index. That said, in the first case at least, you don't need a Select Case
because you can simply assign the SelectedIndex
directly to LHR
.– jmcilhinney
Nov 28 '18 at 5:29
The
SelectedIndex
is the index of the item selected, which is obviously not what you're checking there. If you want to check the text then use the Text
and if you use the SelectedIndex` then check the index. That said, in the first case at least, you don't need a Select Case
because you can simply assign the SelectedIndex
directly to LHR
.– jmcilhinney
Nov 28 '18 at 5:29
Also make use of the text property of the combo box instead of it's selected index
– preciousbetine
Nov 28 '18 at 5:35
Also make use of the text property of the combo box instead of it's selected index
– preciousbetine
Nov 28 '18 at 5:35
thank you @jmcilhinney , i changed SelectedIndex into Selecteditem and it works.
– Student
Nov 28 '18 at 6:17
thank you @jmcilhinney , i changed SelectedIndex into Selecteditem and it works.
– Student
Nov 28 '18 at 6:17
add a comment |
1 Answer
1
active
oldest
votes
With simple class and data-binding you can simplify your code a little bid and get rid of current problem as well.
Public Class MyItem
Public ReadOnly Name As String
Public ReadOnly Value As Integer
Public Sub New(name As String, value As Integer)
Me.Name = name
Me.Value = value
End Sub
End Class
' Then in constructor create collection of values and bind it ot the combobox
Dim LHRValues As New List(Of MyItem) From
{
New MyItem("< 20 ", 0),
New MyItem("20 - 50", 1),
New MyItem("50 - 200", 2),
New MyItem("200 - 500", 3),
New MyItem("500 - 2000", 4),
New MyItem("2000 - 5000", 5),
New MyItem("5000 - 20000", 6),
New MyItem("20000 - 50000", 7),
New MyItem("> 50000", 8)
}
ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues
' Then in the code where you need selected value
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
' Other selected values
End Sub
You only need to cast ComboLHR.SelectedValue
to the type you expect (Integer
), because SelectedValue
is of type object
. I hope you have Option Strict
set On
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512641%2fhow-to-convert-combobox-item-into-value-and-calculate-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
With simple class and data-binding you can simplify your code a little bid and get rid of current problem as well.
Public Class MyItem
Public ReadOnly Name As String
Public ReadOnly Value As Integer
Public Sub New(name As String, value As Integer)
Me.Name = name
Me.Value = value
End Sub
End Class
' Then in constructor create collection of values and bind it ot the combobox
Dim LHRValues As New List(Of MyItem) From
{
New MyItem("< 20 ", 0),
New MyItem("20 - 50", 1),
New MyItem("50 - 200", 2),
New MyItem("200 - 500", 3),
New MyItem("500 - 2000", 4),
New MyItem("2000 - 5000", 5),
New MyItem("5000 - 20000", 6),
New MyItem("20000 - 50000", 7),
New MyItem("> 50000", 8)
}
ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues
' Then in the code where you need selected value
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
' Other selected values
End Sub
You only need to cast ComboLHR.SelectedValue
to the type you expect (Integer
), because SelectedValue
is of type object
. I hope you have Option Strict
set On
.
add a comment |
With simple class and data-binding you can simplify your code a little bid and get rid of current problem as well.
Public Class MyItem
Public ReadOnly Name As String
Public ReadOnly Value As Integer
Public Sub New(name As String, value As Integer)
Me.Name = name
Me.Value = value
End Sub
End Class
' Then in constructor create collection of values and bind it ot the combobox
Dim LHRValues As New List(Of MyItem) From
{
New MyItem("< 20 ", 0),
New MyItem("20 - 50", 1),
New MyItem("50 - 200", 2),
New MyItem("200 - 500", 3),
New MyItem("500 - 2000", 4),
New MyItem("2000 - 5000", 5),
New MyItem("5000 - 20000", 6),
New MyItem("20000 - 50000", 7),
New MyItem("> 50000", 8)
}
ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues
' Then in the code where you need selected value
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
' Other selected values
End Sub
You only need to cast ComboLHR.SelectedValue
to the type you expect (Integer
), because SelectedValue
is of type object
. I hope you have Option Strict
set On
.
add a comment |
With simple class and data-binding you can simplify your code a little bid and get rid of current problem as well.
Public Class MyItem
Public ReadOnly Name As String
Public ReadOnly Value As Integer
Public Sub New(name As String, value As Integer)
Me.Name = name
Me.Value = value
End Sub
End Class
' Then in constructor create collection of values and bind it ot the combobox
Dim LHRValues As New List(Of MyItem) From
{
New MyItem("< 20 ", 0),
New MyItem("20 - 50", 1),
New MyItem("50 - 200", 2),
New MyItem("200 - 500", 3),
New MyItem("500 - 2000", 4),
New MyItem("2000 - 5000", 5),
New MyItem("5000 - 20000", 6),
New MyItem("20000 - 50000", 7),
New MyItem("> 50000", 8)
}
ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues
' Then in the code where you need selected value
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
' Other selected values
End Sub
You only need to cast ComboLHR.SelectedValue
to the type you expect (Integer
), because SelectedValue
is of type object
. I hope you have Option Strict
set On
.
With simple class and data-binding you can simplify your code a little bid and get rid of current problem as well.
Public Class MyItem
Public ReadOnly Name As String
Public ReadOnly Value As Integer
Public Sub New(name As String, value As Integer)
Me.Name = name
Me.Value = value
End Sub
End Class
' Then in constructor create collection of values and bind it ot the combobox
Dim LHRValues As New List(Of MyItem) From
{
New MyItem("< 20 ", 0),
New MyItem("20 - 50", 1),
New MyItem("50 - 200", 2),
New MyItem("200 - 500", 3),
New MyItem("500 - 2000", 4),
New MyItem("2000 - 5000", 5),
New MyItem("5000 - 20000", 6),
New MyItem("20000 - 50000", 7),
New MyItem("> 50000", 8)
}
ComboLHR.DisplayMember = "Name" ' Property Name will be used as a text
ComboLHR.ValueMember = "Value" ' Property Value will be used as a value
ComboLHR.DataSource = LHRValues
' Then in the code where you need selected value
Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
Dim selectedLHR As Integer = DirectCast(ComboLHR.SelectedValue, Integer)
' Other selected values
End Sub
You only need to cast ComboLHR.SelectedValue
to the type you expect (Integer
), because SelectedValue
is of type object
. I hope you have Option Strict
set On
.
answered Nov 28 '18 at 8:47
FabioFabio
20.1k22048
20.1k22048
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53512641%2fhow-to-convert-combobox-item-into-value-and-calculate-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The
SelectedIndex
is the index of the item selected, which is obviously not what you're checking there. If you want to check the text then use theText
and if you use the SelectedIndex` then check the index. That said, in the first case at least, you don't need aSelect Case
because you can simply assign theSelectedIndex
directly toLHR
.– jmcilhinney
Nov 28 '18 at 5:29
Also make use of the text property of the combo box instead of it's selected index
– preciousbetine
Nov 28 '18 at 5:35
thank you @jmcilhinney , i changed SelectedIndex into Selecteditem and it works.
– Student
Nov 28 '18 at 6:17