使用 vb。我尝试使用组合框的文本作为字体大小的值。这意味着,作为用户,我可以通过单击下拉菜单来选择我想要的字体大小。
下拉式菜单:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged ComboBox1.Items.Add("6") ComboBox1.Items.Add("10") ComboBox1.Items.Add("12") End Sub
现在所选的选项应该会影响字体大小。但因此,我需要从字符串转换为大小。有人能帮忙吗?
另外,我还可以想象有一种更有效的方法可以让用户有机会更改字体大小。欢迎提供任何提示和建议!
答案1
类似这样的事情应该对你有用:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Your combo box should be built and populated when the form loads, not when the combo box is changed
ComboBox1.Items.Add("6")
ComboBox1.Items.Add("10")
ComboBox1.Items.Add("12")
ComboBox1.SelectedIndex = 0 'This Auto-Selects the first entry to prevent you having to handle "null"'s
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim tryParseResult As Boolean 'holds the True/False for whether conversion to a number worked
Dim parsedNumber As Integer 'Holds the number after conversion
tryParseResult = Int32.TryParse(ComboBox1.SelectedItem, parsedNumber) 'try converting to number - "parsedNumber" will contain the number on success - "tryParseResult" will contain a true/false for success
'If tryPArseResult = False - conversion failed - tell the user and stop.
If tryParseResult = False Then
MessageBox.Show("Conversion of " & ComboBox1.SelectedItem & " To Number Failed") 'Tell the user it failed
Exit Sub 'Stop processing
End If
'Set TextBox1's Font to the same font with a new size
TextBox1.Font = New Font(TextBox1.Font.Name, parsedNumber)
End Sub
End Class
如果你想用多个控件来做到这一点 - 你需要查看在表单中循环遍历控件,OfType
如果你只想处理某些控件