在FileManager类中编写下面的公用函数ErrorHandler,在发生例外时,显示例外错误信息
private void ErrorHandler (string ErrorDescription)
{
messageBox.Show (ErrorDescription);
} "在FileManager类中编写"这是说要到哪里去加代码?
书上的原话~~说的不明不白的。而且没有附完整代码。这个问题我想只有不想回答的没有不知道的吧~~各位大虾,帮忙答一下吧~
private void ErrorHandler (string ErrorDescription)
{
messageBox.Show (ErrorDescription);
} "在FileManager类中编写"这是说要到哪里去加代码?
书上的原话~~说的不明不白的。而且没有附完整代码。这个问题我想只有不想回答的没有不知道的吧~~各位大虾,帮忙答一下吧~
中←在哪里?
布局好窗口后生成代码如下~希望明确指出~~~
刚刚入门,希望不吝赐教~
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 资源管理器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FileManager : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.TextBox txtCurDir;
private System.Windows.Forms.TextBox txtNewDir;
private System.Windows.Forms.ListView lstFiles;
private System.Windows.Forms.TextBox txtNewFile;
private System.Windows.Forms.Button btnCurDir;
private System.Windows.Forms.Button btnNewDir;
private System.Windows.Forms.Button btnNewFile; ///判断程序是否已经初始化
private bool mIsInitialized = false;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public FileManager()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FileManager());
} private void FileManager_Load(object sender, System.EventArgs e)
{
} private void label1_Click(object sender, System.EventArgs e)
{
} private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
} private void textBox3_TextChanged(object sender, System.EventArgs e)
{
}
}
}
private bool mIsInitialized = false;书上的一个实例
说道要加这样一段代码~我不知道要加到哪里
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 资源管理器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FileManager : System.Windows.Forms.Form//类
{
你产生得程序…………
private void ErrorHandler (string ErrorDescription)
{
messageBox.Show (ErrorDescription);
}
private void aaa()//新添加得程序
{
if(error)//如果错误
{
this.ErrorHandler();//调用错误处理,要传参数
}
else
{
…………
}
} }
}
先分析分析~谢谢ztFeng(sunny)
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
namespace 资源管理器
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class FileManager : System.Windows.Forms.Form
{
private System.Windows.Forms.ContextMenu contextMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListView lstFiles;
private System.Windows.Forms.Button btnNewFile;
private System.Windows.Forms.Button btnNewDir;
private System.Windows.Forms.Button btnUp;
private System.Windows.Forms.TextBox txtNewFile;
private System.Windows.Forms.TextBox txtNewDir;
private System.Windows.Forms.TextBox txtCurDir;
private System.Windows.Forms.Label label3;
///判断程序是否已经初始化
private bool mIsInitialized = false;
/// <summary>
/// 发生例外时,显示例外信息
/// </summary>
/// <param name="ErrorDescription"></param>
private void ErrorHandler (string ErrorDescription)
{
MessageBox.Show (ErrorDescription);
}
private string GetInitDir()
{
return Application.StartupPath;
} private void FillFiles()
{
lstFiles.Items.Clear ();
DirectoryInfo cd = new DirectoryInfo(txtCurDir.Text+ "\\");
foreach (DirectoryInfo d in cd.GetDirectories ())
{
lstFiles.Items.Add(new
ListViewItem (new string[]{d.Name,"目录","",
d.LastWriteTime.ToString
("yyyy-MM-dd HH:MM:ss",null)}));
}
foreach (FileInfo f in cd.GetFiles ())
{
lstFiles.Items.Add(new ListViewItem
(new string[]{f.Name,"文件",f.Length.ToString(),
f.LastWriteTime.ToString ("yyyy-MM-dd HH:MM:ss",null)}));
}
}
private void Initialize()
{
try
{
if(mIsInitialized)
return;
txtCurDir.Text=GetInitDir();
lstFiles.View = View.Details;
lstFiles.Columns.Add("文件名",
lstFiles.Width/3,HorizontalAlignment.Left);
lstFiles.Columns.Add("目录/文件",
lstFiles.Width/6,HorizontalAlignment.Center);
lstFiles.Columns.Add("文件大小",
lstFiles.Width/6,HorizontalAlignment.Right);
lstFiles.Columns.Add("最后存取时间",
lstFiles.Width/3,HorizontalAlignment.Left);
FillFiles();
mIsInitialized=true;
}
catch(System.Exception E)
{
ErrorHandler(E.ToString());
}
}
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public FileManager()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//调用Initialize初始化程序
Initialize();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}窗体代码省略 /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FileManager());
} private void FileManager_Load(object sender, System.EventArgs e)
{
} private void label1_Click(object sender, System.EventArgs e)
{
} private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
} private void textBox3_TextChanged(object sender, System.EventArgs e)
{
} private void btnUp_Click(object sender, System.EventArgs e)
{
if(txtCurDir.Text.LastIndexOf ("\\")!=-1)
{
txtCurDir.Text = txtCurDir.Text.Substring
(0,txtCurDir.Text.LastIndexOf ("\\"));
FillFiles();
}
} private void btnNewDir_Click(object sender, System.EventArgs e)
{
if(txtNewDir.Text == "")
{
MessageBox.Show ("目录名称不可空白");
return;
}
if(Directory.Exists(txtCurDir.Text+"\\"+txtNewDir.Text))
{
MessageBox.Show (" 目录 ["+ txtCurDir.Text + "\\" + txtNewDir.Text+"]已存在");
return;
}
Directory.CreateDirectory(txtCurDir.Text+"\\"+txtNewDir.Text);
FillFiles(); } private void btnNewFile_Click(object sender, System.EventArgs e)
{
if(txtNewFile.Text == "")
{
MessageBox.Show ("文件名称不可空白");
return;
}
if (File.Exists (txtCurDir.Text+"\\"+txtNewFile.Text))
{
MessageBox.Show ("文件["+txtCurDir.Text+"\\"+txtNewFile.Text + "]已存在");
return;
}
FileStream fs = File.Create(txtCurDir.Text+"\\"+txtNewFile.Text);
fs.Close ();
FillFiles();
} private void menuItem2_Click(object sender, System.EventArgs e)
{
try
{
if (lstFiles.SelectedItems.Count == 0)
return;
if (lstFiles.SelectedItems[0].SubItems[1].Text=="目录")
{
string strDir=txtCurDir.Text+"\\"+lstFiles.SelectedItems[0].Text;
DialogResult ret = MessageBox.Show ("确定删除目录["+strDir+"]?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (ret==DialogResult.OK)
{
Directory.Delete(strDir,false);
FillFiles();
MessageBox.Show("目录["+strDir+"]已成功的删除.");
}
}
else//删除文件
{
string strFile=txtCurDir.Text+"\\"+lstFiles.SelectedItems[0].Text;
DialogResult ret=MessageBox.Show("确定删除文件["+strFile+"]?","确定删除",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
if (ret==DialogResult.OK)
{
File.Delete (strFile);
FillFiles();
MessageBox.Show("文件["+strFile+"]已成功的删除.");
}
} }
catch(System.Exception E)
{
ErrorHandler(E.ToString());
}
}
}
}
/// Form1 的摘要说明。
/// </summary>
public class FileManager : System.Windows.Forms.Form
-----------
给你加下划线了,你好歹也查找一下这个单词啊。{}符号里面就是书上说的“中”,至于具体哪个位置,看你喜欢了,只要直接在这个类的{}里,(而不是{}里的{}里,呵呵)
{
Application.Run(new FileManager());
}你在这个下面另起一行加上就可以了。变成这样:
static void Main()
{
Application.Run(new FileManager());
}private void ErrorHandler (string ErrorDescription)
{
messageBox.Show (ErrorDescription);
}