这周的实验课,老师给我们安排的任务是基于第一个任务的基础上来完成的。上实验课之前的理论课上,老师提示了我们在上一周遇到的问题解决办法。在这次实验课上我也快速的修改了上次实验课上的不足和没有完成的部分。
首先是完成滚动条部分,要求三个滚动条能控制textbox的背景颜色,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实验二
{
public partial class gundong : Form
{
public gundong()
{
InitializeComponent();
}
private void hScrollBar3_Scroll(object sender, ScrollEventArgs e)
{
textBox1.BackColor = Color.FromArgb(hScrollBar1.Value,hScrollBar2.Value,hScrollBar3.Value);
}
private void hScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
textBox1.BackColor = Color.FromArgb(hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value);
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
textBox1.BackColor = Color.FromArgb(hScrollBar1.Value, hScrollBar2.Value, hScrollBar3.Value);
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
}
}
很明显,我们分别为滚动条添加了hscrollbar这个事件,在事件中我们使用的是color.fromarg()这个函数来实现的功能。
函数的参数分别代表的是每个滚动条的当前值(0-255之间)
第二部分要求的是在contentmenuscript中实现随着item的切换而切换picturebox中的图片,在左下角的状态栏中显示当前的状态。这里的难点就是在开始load 这个pivturebox的时候,要求我们默认选择春天这一项,这时,在状态栏中,如何显示的问题。其实,按最简单的想法来解决问题,很简单,代码中给出了答案:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 实验二
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void 春ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"chun.jpg");
春ToolStripMenuItem.Checked = true;
夏ToolStripMenuItem.Checked = false;
秋ToolStripMenuItem.Checked = false;
冬ToolStripMenuItem.Checked = false;
toolStripStatusLabel1.Text = "春";
}
private void 夏ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"xia.jpg");
春ToolStripMenuItem.Checked = false;
夏ToolStripMenuItem.Checked = true;
秋ToolStripMenuItem.Checked = false;
冬ToolStripMenuItem.Checked = false;
toolStripStatusLabel1.Text = "夏";
}
private void 秋ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"qiu.jpg");
春ToolStripMenuItem.Checked = false;
夏ToolStripMenuItem.Checked = false;
秋ToolStripMenuItem.Checked = true;
冬ToolStripMenuItem.Checked = false;
toolStripStatusLabel1.Text = "秋";
}
private void 冬ToolStripMenuItem_Click(object sender, EventArgs e)
{
pictureBox1.Image = Image.FromFile(@"dong.jpg");
春ToolStripMenuItem.Checked = false;
夏ToolStripMenuItem.Checked = false;
秋ToolStripMenuItem.Checked = false;
冬ToolStripMenuItem.Checked = true;
toolStripStatusLabel1.Text = "冬";
}
private void 滚动条的使用ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (toolStripComboBox1.SelectedIndex == 0)
{
gundong gd2 = new gundong();
gd2.Show();
}
else
{
zice zc = new zice();
zc.Show();
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void toolStripComboBox1_Click(object sender, EventArgs e)
{
}
private void 自测题ToolStripMenuItem_Click(object sender, EventArgs e)
{
zice zc = new zice();
zc.Show();
}
private void 退出ToolStripMenuItem_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//statusStrip1.Text = "春";
toolStripStatusLabel1.Text = "春";
}
}
}
反思:winform学习的门槛是很低的,但要想做的精致了却需要一番功夫在里面。