使用组合框更改表单Backgroundcolor, [英] Change Form Backgroundcolor with Combobox,
问题描述
嘿,
我想为我的程序创建一个选项表单,我希望用户可以通过Form1(普通表单)更改背景颜色
I want to make an Optionsform for my Program, i want that the user can change the Background color by Form1 (Normal Form)
Soo,我将经典颜色添加到了一个组合框(名称:Colors)中,如果用户选择了一种颜色,然后单击Button,他将获得BGColor
代码是问题,我的代码是当下;
Form1.BackColor = Color.Colors.Text
谁能帮我吗?
(对不起,我的英语不好xd)
Soo, i added the classic colors to a Combobox (name: Colors), and if the user choosed one, and clicked the Button he get the BGColor
The code is the Problem, my Code at the Moment is;
Form1.BackColor = Color.Colors.Text
Can anyone help me?
(Sorry for my Bad English xd)
推荐答案
如Reed所言,请使用构造函数创建OptionForm:
As sugested by Reed, create your OptionForm with a Constructor:
Private form1Calling As Form1
Public Sub New(FormCalling As Form1)
' Automatically added for you...
InitializeComponent()
' Saving the source of the call to the option form
form1Calling = FormCalling
End Sub
用类似这样的名称:
call it with something like:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Create the option Form
Dim myOptionForm As New Form2(Me)
myOptionForm.Show()
End Sub
在您的OptionForm中,输入带有颜色名称的ComboBox项.
In your OptionForm, enter the items of the ComboBox with your color names.
然后从ComboBox文本转到Color,执行以下操作:
Then to go from your ComboBox text to the Color do something like this:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim selectedColor As Color
Select Case ComboBox1.SelectedItem.ToString
Case "Red"
selectedColor = Color.Red
Case "Green"
selectedColor = Color.Green
Case "Blue"
selectedColor = Color.Blue
Case "Magenta"
selectedColor = Color.Magenta
End Select
form1Calling.BackColor = selectedColor
End Sub
或者...您可以在窗体中添加一个ColorDialog,让用户选择任何颜色,例如:
Or... you could add a ColorDialog to your form and let the user pick whatever color, for example:
'Double click to let the user pick a color with a ColorPicker
Private Sub Form1_DoubleClick(sender As Object, e As EventArgs) Handles Me.DoubleClick
ColorDialog1.Color = Color.Black
If ColorDialog1.ShowDialog() = DialogResult.OK Then
form1Calling.BackColor = ColorDialog1.Color
End If
End Sub
这篇关于使用组合框更改表单Backgroundcolor,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!