using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 图片管理器
{
public partial class FrmLoadPic : Form
{
public FrmLoadPic()
{
InitializeComponent();
}
public FrmLoadPic(ListBox lst,StatusStrip ssp)
{
InitializeComponent();
lstFolder = lst;
staMsg = ssp;
openFileDialog1.Filter = "图像文件(*.BMP;*.JPG;*.GIF;" + "*.jpeg;*.ico)|*.BMP;*.JPG;*.GIF;*.jpeg;*.ico"; }
private ListBox lstFolder;
private StatusStrip staMsg;
private void FrmLoadPic_Load(object sender, EventArgs e)
{ } private void btnSelPic_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
foreach(string s in openFileDialog1.FileNames)
{
if (!chklsPics.Items.Contains(s) && PicInfo.IsImage(s))
{
PicInfo pic = new PicInfo(s);
chklsPics.Items.Add(pic, true);
}
}
}
}
}
}这是第一个类,btnSelPic_Click是一个浏览图片文件的按钮。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace 图片管理器
{
class PicInfo
{
private string _fullName;
private string _nameNoExtension;
public PicInfo(string path)
{
_fullName = path;
_nameNoExtension = Path.GetFileNameWithoutExtension(path);
}
public string nameNoExtension
{
get
{ return _nameNoExtension; }
set
{
if (value != "" && value.IndexOf('.') == -1)
{
_nameNoExtension = value;
}
}
}
public string GetExtension()
{
return Path.GetExtension(_fullName);
}
public static bool IsImage(string path)
{
string ext=Path.GetExtension(path).ToUpper();
if (ext == ".BMP" || ext == ".JPG" || ext == ".GIF" || ext == ".JPEG" || ext == ".ICO")
{
return true;
}
else
{
return false;
}
}
public override string ToString()
{
return _fullName;
}
}
}
这是第2个类,现在我有3个问题不明白,麻烦各位指点。
1.为什么PicInfo pic = new PicInfo(s);就把s包装成了PicInfo类的对象。
2.为什么PicInfo类中要重写ToString()方法。
3.我设断点调式chklsPics.Items.Add(pic, true);这句话的时候为什么会执行ToString()方法,而且还要执行两次。
我好菜,如果问错了大家不要取笑我。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 图片管理器
{
public partial class FrmLoadPic : Form
{
public FrmLoadPic()
{
InitializeComponent();
}
public FrmLoadPic(ListBox lst,StatusStrip ssp)
{
InitializeComponent();
lstFolder = lst;
staMsg = ssp;
openFileDialog1.Filter = "图像文件(*.BMP;*.JPG;*.GIF;" + "*.jpeg;*.ico)|*.BMP;*.JPG;*.GIF;*.jpeg;*.ico"; }
private ListBox lstFolder;
private StatusStrip staMsg;
private void FrmLoadPic_Load(object sender, EventArgs e)
{ } private void btnSelPic_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
foreach(string s in openFileDialog1.FileNames)
{
if (!chklsPics.Items.Contains(s) && PicInfo.IsImage(s))
{
PicInfo pic = new PicInfo(s);
chklsPics.Items.Add(pic, true);
}
}
}
}
}
}这是第一个类,btnSelPic_Click是一个浏览图片文件的按钮。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace 图片管理器
{
class PicInfo
{
private string _fullName;
private string _nameNoExtension;
public PicInfo(string path)
{
_fullName = path;
_nameNoExtension = Path.GetFileNameWithoutExtension(path);
}
public string nameNoExtension
{
get
{ return _nameNoExtension; }
set
{
if (value != "" && value.IndexOf('.') == -1)
{
_nameNoExtension = value;
}
}
}
public string GetExtension()
{
return Path.GetExtension(_fullName);
}
public static bool IsImage(string path)
{
string ext=Path.GetExtension(path).ToUpper();
if (ext == ".BMP" || ext == ".JPG" || ext == ".GIF" || ext == ".JPEG" || ext == ".ICO")
{
return true;
}
else
{
return false;
}
}
public override string ToString()
{
return _fullName;
}
}
}
这是第2个类,现在我有3个问题不明白,麻烦各位指点。
1.为什么PicInfo pic = new PicInfo(s);就把s包装成了PicInfo类的对象。
2.为什么PicInfo类中要重写ToString()方法。
3.我设断点调式chklsPics.Items.Add(pic, true);这句话的时候为什么会执行ToString()方法,而且还要执行两次。
我好菜,如果问错了大家不要取笑我。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货