这次实验三主要是练习DriveListBox,DirListBox,FileListBox三个控件的。
1.添加新控件的方法,在工具箱任意一个控件处点击鼠标右键,然后在要添加进去的控件前面打勾
2.要是mfc的FileListBox要控制可以显示的文件的格式,而且是多种格式,用*.jpg*;*.gif*;*.tiff*这样
3.驱动器列表框,目录列表框和文件列表框是层层相扣的一个关系
private void DriveListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//当在驱动器列表框中选择一个新的驱动器的时候触发该事件
//将目录列表框的当前路径设置成选中的驱动器的
dirListBox1.Path = driveListBox1.Drive;
}
private void DirListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//当在目录列表框中选择一个新的目录的时候触发该事件
//将文件列表框的当前路径设置成选中的目录的
fileListBox1.Path = dirListBox1.Path;
}
dirListBox和fileListBox都是Path,DriveListBox是Drive;
4.图片要想显示出来路径要对,有些时候上面的Path不太对,少一个反斜杠,就要判断一下有没有,没有的话就加上,但是要注意反斜杠是特殊字符,要加一个转义字符。
private void TextBox1_TextChanged(object sender, EventArgs e)
{
String file;//当前文件的路径
String ch;
file = fileListBox1.Path;
ch = file.Substring(file.Length - 1, 1);//获取当前路径的最后一个字符
if (ch == "\\")
{
textBox1.Text = fileListBox1.Path + fileListBox1.FileName;
}
else
textBox1.Text = fileListBox1.Path + "\\" + fileListBox1.FileName;
}
5.上一张下一张功能
private void Button1_Click(object sender, EventArgs e)
{
if (fileListBox1.SelectedIndex == -1)//未选中状态
fileListBox1.SelectedIndex = fileListBox1.Items.Count - 1;
else if (fileListBox1.SelectedIndex == 0)//选中第一张图片
fileListBox1.SelectedIndex = fileListBox1.Items.Count - 1;
else
fileListBox1.SelectedIndex -= 1;
}
private void Button2_Click(object sender, EventArgs e)
{
if (fileListBox1.SelectedIndex == -1)//未选中状态
fileListBox1.SelectedIndex = 0;
else if (fileListBox1.SelectedIndex == fileListBox1.Items.Count - 1)//选中最后一张图片
fileListBox1.SelectedIndex = 0;
else
fileListBox1.SelectedIndex += 1;
}
6.自己多做了放大缩小功能,但没实现滚轮查看不同部分,再努力吧,先交上作业
private void Button3_Click(object sender, EventArgs e)
{
pictureBox1.Width = pictureBox1.Width * 2;
pictureBox1.Height = pictureBox1.Height * 2;
}
private void Button4_Click(object sender, EventArgs e)
{
pictureBox1.Width = pictureBox1.Width / 2;
pictureBox1.Height = pictureBox1.Height / 2;
}
这是完整代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace windows_lib3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DriveListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//当在驱动器列表框中选择一个新的驱动器的时候触发该事件
//将目录列表框的当前路径设置成选中的驱动器的
dirListBox1.Path = driveListBox1.Drive;
}
private void DirListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//当在目录列表框中选择一个新的目录的时候触发该事件
//将文件列表框的当前路径设置成选中的目录的
fileListBox1.Path = dirListBox1.Path;
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
String file;//当前文件的路径
String ch;
file = fileListBox1.Path;
ch = file.Substring(file.Length - 1, 1);//获取当前路径的最后一个字符
if (ch == "\\")
{
textBox1.Text = fileListBox1.Path + fileListBox1.FileName;
}
else
textBox1.Text = fileListBox1.Path + "\\" + fileListBox1.FileName;
}
private void Button1_Click(object sender, EventArgs e)
{
if (fileListBox1.SelectedIndex == -1)//未选中状态
fileListBox1.SelectedIndex = fileListBox1.Items.Count - 1;
else if (fileListBox1.SelectedIndex == 0)//选中第一张图片
fileListBox1.SelectedIndex = fileListBox1.Items.Count - 1;
else
fileListBox1.SelectedIndex -= 1;
}
private void Button2_Click(object sender, EventArgs e)
{
if (fileListBox1.SelectedIndex == -1)//未选中状态
fileListBox1.SelectedIndex = 0;
else if (fileListBox1.SelectedIndex == fileListBox1.Items.Count - 1)//选中最后一张图片
fileListBox1.SelectedIndex = 0;
else
fileListBox1.SelectedIndex += 1;
}
private void FileListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
String file;//当前文件的路径
String ch;
file = fileListBox1.Path;
ch = file.Substring(file.Length - 1, 1);//获取当前路径的最后一个字符
if (ch == "\\")
{
textBox1.Text = fileListBox1.Path + fileListBox1.FileName;
}
else
textBox1.Text = fileListBox1.Path +"\\"+ fileListBox1.FileName;
pictureBox1.Image = Image.FromFile(textBox1.Text);
}
private void Button3_Click(object sender, EventArgs e)
{
pictureBox1.Width = pictureBox1.Width * 2;
pictureBox1.Height = pictureBox1.Height * 2;
}
private void Button4_Click(object sender, EventArgs e)
{
pictureBox1.Width = pictureBox1.Width / 2;
pictureBox1.Height = pictureBox1.Height / 2;
}
}
}
这是效果图