c#认证考试第4章 总结
4-1:实现NumericUpDown控件与TrackBar控件联动
namespace Text4_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
numericUpDown1.Value = trackBar1.Value;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
trackBar1.Value = (int)numericUpDown1.Value;
}
}
运行结果:
}
4-2:通过控件hScrollBar形成控制条去控制窗体颜色。
namespace Text4_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
this.BackColor = Color.FromArgb(hScrollBar1.Value,hScrollBar2.Value,hScrollBar3.Value);
}
private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
this.BackColor = Color.FromArgb(hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value);
}
private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
{
this.BackColor = Color.FromArgb(hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value);
}
}
}
运行结果:
4-3:与第二题类似 控件用VScrollBar即可 方法与第二题一样
4-4:与前面类似代码不同的只有前景色:this.ForeColor = Color.FromArgb(hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value);
4_5:与第4题类似:this.ForeColor = Color.FromArgb(vScrollBar.Value, hScrollBar2.Value, hScrollBar3.Value);
4-6:通过滑块改变窗体透明度
需要设置 trackBar的事件 ValueChanged为 trackBar1_ValueChanged,不然无法控制到progressBar1进度条的改变
private void trackBar1_ValueChanged(object sender, EventArgs e)
{
this.progressBar1.Value = (int)this.trackBar1.Value;
this.Opacity = 0.5 + (double)trackBar1.Value / 100;
}
运行结果如下:
4-7:利用控件来改变课程
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (this.comboBox1.SelectedIndex)
{
case 0: this.radioButton1.Checked = true; break;
case 1: this.radioButton2.Checked = true; break;
case 2: this.radioButton3.Checked = true; break;
}
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton1.Checked == true)
this.comboBox1.SelectedIndex = 0;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton2.Checked == true)
this.comboBox1.SelectedIndex = 1;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (this.radioButton3.Checked == true)
this.comboBox1.SelectedIndex = 2;
}
}
4-8:
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if (numericUpDown1.Value == 1)
radioButton1.Checked = true;
if (numericUpDown1.Value == 2)
radioButton2.Checked = true;
if (numericUpDown1.Value == 3)
radioButton3.Checked = true;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
this.numericUpDown1.Value=1;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
if (radioButton2.Checked == true)
this.numericUpDown1.Value = 2;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
if (radioButton3.Checked == true)
this.numericUpDown1.Value = 3;
}
运行结果:
4-9:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == false)
listBox1.Items.Remove("English");
if (checkBox1.Checked == true)
listBox1.Items.Add("English");
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == false)
listBox1.Items.Remove("German");
if (checkBox2.Checked == true)
listBox1.Items.Add("German");
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked == false)
listBox1.Items.Remove("French");
if (checkBox1.Checked == true)
listBox1.Items.Add("French");
}
}
运行结果:
4-11:
namespace Text4_11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Time;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Long;
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
}
运行结果:
4-12:
namespace Text4_12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == false)
monthCalendar1.ShowToday = false;
else
monthCalendar1.ShowToday = true;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked == false)
monthCalendar1.ShowTodayCircle = false;
else
monthCalendar1.ShowTodayCircle = true;
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
if (checkBox3.Checked == true)
monthCalendar1.ShowWeekNumbers = true;
else
monthCalendar1.ShowTodayCircle = false;
}
运行结果:
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value == progressBar1.Maximum)
progressBar1.Value = 0;
else if (progressBar1.Value< progressBar1.Maximum)
progressBar1.Value = progressBar1.Value+progressBar1.Step;
if (progressBar2.Value== progressBar2.Maximum)
progressBar2.Value= 0;
else
progressBar2.Value = progressBar2.Value + progressBar2.Step;
}
运行结果:
4-14
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value == progressBar1.Maximum)
progressBar1.Value = 0;
else
progressBar1.Value = progressBar1.Value + progressBar1.Step;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
timer1.Interval = 500;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
timer1.Interval = 100;
}
运行结果:
4-15
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value == progressBar1.Maximum)
progressBar1.Value = 0;
else
progressBar1.Value += progressBar1.Step;
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
button1.Enabled = false;
button2.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
button1.Enabled = true;
button2.Enabled = false;
}
运行结果:
4-16
private void timer1_Tick(object sender, EventArgs e)
{
if (trackBar1.Value == trackBar1.Maximum)
trackBar1.Value = 0;
else
trackBar1.Value++;
if (trackBar3.Value == trackBar3.Maximum)
trackBar3.Value = 0;
else
trackBar3.Value = trackBar3.Value + 5;
}
运行结果:
4-17
private void timer1_Tick(object sender, EventArgs e)
{
if (hScrollBar1.Value == hScrollBar1.Maximum)
hScrollBar1.Value = 0;
else
hScrollBar1.Value++;
if (hScrollBar2.Value == hScrollBar2.Maximum)
hScrollBar2.Value = 0;
else
hScrollBar2.Value = hScrollBar2.Value + 5;
}
运行结果:
4-18:与上题相似 只是一个数横条,一个是竖条,使用vscrollbar ,其他语法相同。
4-19:
设置dateTimePicker数值加1不能直接使用dateTimePicker1.Value++;而是应该设置为dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(1);
private void timer1_Tick(object sender, EventArgs e)
{
dateTimePicker1.Value = dateTimePicker1.Value.AddSeconds(1);
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button1.Enabled = true;
timer1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
timer1.Enabled = false;
}
运行结果:
4-20
private void timer1_Tick(object sender, EventArgs e)
{
monthCalendar1.TodayDate=monthCalendar1.TodayDate.AddDays(1);
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
timer1.Enabled = true;
button3.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Enabled = false;
button3.Enabled = false;
}
运行结果:
以上就是第4单元的题 熟悉的理解控件和他的属性就可以完成这些题目