我有一个用户可以输入的多行文本框;其内容应该驱动组合框中当前选定的项目。
我现有的代码目前适用于两个项目(是和否);但是,我现在需要扩展到支持四个项目。如果用户手动更新文本,则每隔3秒也由计时器更新。组合框无法输入,而是具有预设选择。
该程序还用于编辑属性文件。
我有点不确定如何这样做。
这适用于2项更改:
Dim lines as Textbox1.lines()
Dim ach_tr As String = "announce-player-achievements=" 'stuff to be replaced by string ach_ttr
Dim ach_ttr As String = "" 'empty string to delete string ach_tr
Dim ach As String = lines(My.Settings.AnnouncePlayerAchievements)'line of string ach_tr
ach = ach.Replace(ach_tr, ach_ttr)'removes string ach_tr and leaves the value
If ach = "true" Then
achievements.SelectedItem = "Yes"
Else
achievements.SelectedItem = "No"
End If
我已尝试过这4件物品:
Dim lines as Textbox1.lines()
Dim diff_tr As String = "difficulty="
Dim diff_ttr As String = ""
Dim diff As String = lines(My.Settings.Difficulty)
diff = diff.Replace(diff_tr, diff_ttr)
If diff = "0" Then
achievements.SelectedItem = "Peaceful"
End If
If diff = "1" Then
achievements.SelectedItem = "Easy"
End If
If diff = "2" Then
achievements.SelectedItem = "Normal"
End If
If diff = "3" Then
achievements.SelectedItem = "Hard"
End If
而这一个:
Dim lines as Textbox1.lines()
Dim diff_tr As String = "difficulty="
Dim diff_ttr As String = ""
Dim diff As String = lines(My.Settings.Difficulty)
diff = diff.Replace(diff_tr, diff_ttr)
If diff = "0" Then
achievements.SelectedItem = "Peaceful"
Else
If diff = "1" Then
achievements.SelectedItem = "Easy"
Else
If diff = "2" Then
achievements.SelectedItem = "Normal"
Else
If diff = "3" Then
achievements.SelectedItem = "Hard"
End If
End If
End If
End If
答案 0 :(得分:0)
你可以做类似的事情:
var difficultyMap = new Dictionary<string,string>
{
{"0", "Peaceful"},
{"1", "Easy"},
{"2", "Normal"},
{"3", "Hard"}
};
difficultyCombo.Items = difficultyMap.Values;
updateTimer.Tick += (s,e) => {
var text = inputTextBox.Text;
string found;
if (!difficultyMap.TryGetValue(text))
{
MessageBox.Show("unknown difficulty");
return;
}
difficultyCombo.SelectedItem = found;
};
这是在C#,因为我不太了解VB,但核心概念应该映射,我也不确定你的确切实现或要求,实质上我已经完成了以下工作:
现在向组合和文本输入中添加n
个项目是微不足道的,因为您只需在字典中添加一个新条目。
答案 1 :(得分:0)
编辑:为了将来的参考,还有一个更紧凑的版本也可以使用:
Public LineNum As Globals.LineNumEnum
Dim serveriptr As String = ("server-ip=")
Dim sipc As String = PropView.OutputRTF.Lines(LineNum.SerIP)
sipc = sipc.Replace(serveriptr, Nothing)
我发现了我的问题。我试图改变错误的组合框lol。至少我了解了词典和精选案例。谢谢你的时间。 这是正确的代码lol:
Dim diff_tr As String = "difficulty="
Dim diff_ttr As String = ""
Dim diff As String = lines(My.Settings.Difficulty)
diff = diff.Replace(diff_tr, diff_ttr)
If diff = "0" Then
difficulty.SelectedItem = "Easy"
ElseIf diff = "1" Then
difficulty.SelectedItem = "Easy"
ElseIf diff = "2" Then
difficulty.SelectedItem = "Normal"
ElseIf diff = "3" Then
difficulty.SelectedItem = "Hard"
End If