读取和更改具有 4 个不同可能值的组合框选定项
Read and change combo-box selected item with 4 different possible values
我有一个多行文本框,用户可以在其中输入内容;其内容应该驱动组合框中当前选定的项目。
我目前有适用于两个项目的现有代码(是和否);但是,我现在需要将其扩展到支持四个项目。这也由计时器每 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
你可以做类似的事情:
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,但核心概念应该映射,我也不确定您的确切实现或要求,本质上我已经完成了以下:
- 创建了一个字典,从 "numeric" 难度类型到组合的显示类型
- 使用字典中的显示值填充组合
- 每当更新计时器触发时,我:
- 从文本框中获取文本
- 尝试在字典中查找匹配难度
- 显示错误或更新组合
现在向组合和文本输入添加 n
个项目是微不足道的,因为您只需向字典中添加一个新条目。
编辑:供将来参考这里是一个更紧凑的版本,也可以工作:
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)
我发现了我的问题。我试图更改错误的组合框大声笑。好吧,至少我了解了字典和 select 案例。好吧,谢谢你的时间。
这是正确的代码大声笑:
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
我有一个多行文本框,用户可以在其中输入内容;其内容应该驱动组合框中当前选定的项目。
我目前有适用于两个项目的现有代码(是和否);但是,我现在需要将其扩展到支持四个项目。这也由计时器每 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
你可以做类似的事情:
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,但核心概念应该映射,我也不确定您的确切实现或要求,本质上我已经完成了以下:
- 创建了一个字典,从 "numeric" 难度类型到组合的显示类型
- 使用字典中的显示值填充组合
- 每当更新计时器触发时,我:
- 从文本框中获取文本
- 尝试在字典中查找匹配难度
- 显示错误或更新组合
现在向组合和文本输入添加 n
个项目是微不足道的,因为您只需向字典中添加一个新条目。
编辑:供将来参考这里是一个更紧凑的版本,也可以工作:
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)
我发现了我的问题。我试图更改错误的组合框大声笑。好吧,至少我了解了字典和 select 案例。好吧,谢谢你的时间。 这是正确的代码大声笑:
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