这是个C/S做的项目,在使用中需要提炼的技术如下:
1. 多页签打开的通用方法:
注意: 打开的窗口中做页面跳转往往有些问题~
/// <summary>
/// 显示窗体(如果当前没有窗体实例就生成一个新的,如果已经存在就打开已经存在的窗体)
/// </summary>
/// <param name="sender"></param>
/// <param name="oForm"></param>
private void ShowForm(Form oForm)
{
Form frmToInvoke;
string key;
key = oForm.GetType().ToString();
if (hashTable1[key] == null)
{
frmToInvoke = oForm;
frmToInvoke.Closing += new CancelEventHandler(this.Form_Closing);
//创建页签并显示
Panel p_ye = new Panel();
p_ye.Height = 30;
int locatione_x = 24;
//其他控件变背景
for (int i = 0; i < listhashTabs.Count; i++)
{
Panel p = (Panel)listhashTabs[i]["Panel"];
p.BackgroundImage = Image.FromFile(SysRootPath() + "images\\yeqian_3_24.png");
listhashTabs[i]["State"] = false;
Form form = (Form)listhashTabs[i]["Form"];
form.Size = new Size(2, 2);
}
if (listhashTabs.Count > 0)
{
locatione_x = ((Panel)listhashTabs[listhashTabs.Count - 1]["Panel"]).Location.X + 3 + ((Panel)listhashTabs[listhashTabs.Count - 1]["Panel"]).Width;
}
p_ye.Location = new Point(locatione_x, 5);
p_ye.MinimumSize = new System.Drawing.Size(67, 30);
p_ye.BackgroundImage = Image.FromFile(SysRootPath() + "images\\yeqian_2_24.png");
Label l_ye = new Label();
l_ye.Text = oForm.Text;
l_ye.Location = new Point(6, 6);
l_ye.AutoSize = true;
l_ye.BackColor = Color.Transparent;
l_ye.Font = new Font("微软雅黑", 12);
//绘制圆角
p_ye.Paint += new PaintEventHandler(panelshou_Paint);
PictureBox pibox_ye = new PictureBox();
pibox_ye.Image = Image.FromFile(SysRootPath() + "images\\close_tabs_2.png");
pibox_ye.Size = new System.Drawing.Size(11, 12);
pibox_ye.BackColor = Color.Transparent;
pibox_ye.Tag = key;
//鼠标经过
pibox_ye.MouseEnter += new EventHandler(Tabs_MouseEnter);
pibox_ye.MouseLeave += new EventHandler(Tabs_MouseLeave);
l_ye.MouseEnter += new EventHandler(Tabs_MouseEnter);
l_ye.MouseLeave += new EventHandler(Tabs_MouseLeave);
p_ye.MouseEnter += new EventHandler(Tabs_MouseEnter);
p_ye.MouseLeave += new EventHandler(Tabs_MouseLeave);
//点击关闭
pibox_ye.Click += new EventHandler(pictureBox_Click);
p_ye.Click += new EventHandler(Tabs_Click);
l_ye.Click += new EventHandler(Tabs_Click);
p_ye.Controls.Add(l_ye);
p_ye.Width = l_ye.Width + 36;
p_ye.Controls.Add(pibox_ye);
pibox_ye.Location = new Point(p_ye.Width - pibox_ye.Width - 3, 3);
this.splitContainer2.Panel1.Controls.Add(p_ye);
frmToInvoke.TopLevel = false;
this.splitContainer2.Panel2.Controls.Add(frmToInvoke);
//本页面调整在打开(至前)
//frmToInvoke.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
frmToInvoke.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmToInvoke.BackColor = Color.White;
frmToInvoke.TopMost = true;
frmToInvoke.AutoScroll = true;
frmToInvoke.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom;
frmToInvoke.Size = new Size(this.splitContainer2.Panel2.Width, this.splitContainer2.Panel2.Height);
this.splitContainer2.Panel2.BackColor = Color.White;
hashTabs = new Hashtable();
hashTabs.Add("Label", l_ye);
hashTabs.Add("Panel", p_ye);
hashTabs.Add("PictureBox", pibox_ye);
hashTabs.Add("Form", oForm);
hashTabs.Add("Key", key);
hashTabs.Add("State", true);
listhashTabs.Add(hashTabs);
hashTable1.Add(key, frmToInvoke);
hashTable2.Add(oForm.GetHashCode(), key);
ReadHScrollBar();
}
else
{
ShowFormByKey(key);
frmToInvoke = (System.Windows.Forms.Form)hashTable1[key];
}
frmToInvoke.Opacity = 0.61;
frmToInvoke.Show();
if (_Factoryname != "")
{
Type magicType = frmToInvoke.GetType();
ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
MethodInfo mm = magicType.GetMethod(_Factoryname);
mm.Invoke(frmToInvoke, null);
}
//frmToInvoke.Visible+=
}
2. 项目中使用Access作为数据库,使用注意
1)不可用分号将多个语句放在一起执行,如多语句返回Dataset或拼接多语句做为大的字符串是不允许的
2)2007以下版本, ado.net中不支持replace方法
3)注意多表join的写法
SELECT top 10 K.CourseName as 课程,Q.CourseID as 课程ID,
Z.ChapterName as 章节,Q.CharpteID as 章节ID, T.QueTypeName as 题型,Q.QueTypeID as 题型ID,
D.DiffName as 难度, Q.DifficultyID as 难度ID , Score AS 分值,QueConSimple as 题目,QueAnSimple as 题目答案
FROM ((((QuestionInfo Q
LEFT JOIN CourseInfo K ON K.CourseID=Q.CourseID)
LEFT JOIN ChapterInfo Z ON Z.ChapterID=Q.CharpteID)
LEFT JOIN QueTypeInfo T ON T.QueTypeID=Q.QueTypeID)
LEFT JOIN DifficultyInfo D ON D.DiffID=Q.DifficultyID)
3.Richtext 为C/S中的富文本文件
1)存放的时候以二进制的形式存放。数据库栏位属性设置为Object
byte[] buff = System.Text.Encoding.Default.GetBytes(this.shitiBox.Rtf.Trim());
2) 读取时的方法
MemoryStream stream = new MemoryStream(buff, true);
stream.Write(buff, 0, buff.Length);
string str = System.Text.Encoding.Default.GetString(buff, 0, buff.Length);
Clipboard.SetData(DataFormats.Rtf, str);
this.shitiBox.Paste();
stream.Close();
3)修改字体及样式的相关代码
/// <summary>
/// 修改字体名称
/// </summary>
/// <param name="fontName"></param>
/// <param name="rtb"></param>
private void ChangeFontName(string fontName, RichTextBox rtb)
{
if (fontName == string.Empty)
throw new System.Exception("字体名称参数错误,不能为空");
RichTextBox tempRichTextBox = new RichTextBox(); //用于保存被选中文本的副本
//curRichTextBox是当前文本,即原型
int curRtbStart = rtb.SelectionStart;
int len = rtb.SelectionLength;
int tempRtbStart = 0;
Font font = rtb.SelectionFont;
if (len <= 1 && null != font)
{
rtb.SelectionFont = new Font(fontName, font.Size, font.Style);
return;
}
tempRichTextBox.Rtf = rtb.SelectedRtf;
for (int i = 0; i < len; i++) //逐个设置字体种类
{
tempRichTextBox.Select(tempRtbStart + i, 1);
if (null != tempRichTextBox.SelectionFont)
{
tempRichTextBox.SelectionFont = new Font(fontName, tempRichTextBox.SelectionFont.Size,
tempRichTextBox.SelectionFont.Style, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
}
}
//将副本内容插入到到原型中
tempRichTextBox.Select(tempRtbStart, len);
rtb.SelectedRtf = tempRichTextBox.SelectedRtf;
rtb.Select(curRtbStart, len);
rtb.Focus();
}
/// <summary>
/// 设置字体大小,对应于字号下拉框选项
/// </summary>
/// <param name="fontSize">被选中的字号</param>
private void ChangeFontSize(float fontSize, RichTextBox rtb)
{
if (fontSize <= 0.0)
throw new InvalidProgramException("字号参数错误,不能小于等于0.0");
RichTextBox tempRichTextBox = new RichTextBox();
int curRtbStart = rtb.SelectionStart;
int len = rtb.SelectionLength;
int tempRtbStart = 0;
Font font = rtb.SelectionFont;
if (len <= 1 && null != font)
{
rtb.SelectionFont = new Font(font.Name, fontSize, font.Style);
return;
}
tempRichTextBox.Rtf = rtb.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
if (null != tempRichTextBox.SelectionFont)
{
tempRichTextBox.SelectionFont =
new Font(tempRichTextBox.SelectionFont.Name,
fontSize, tempRichTextBox.SelectionFont.Style);
}
}
tempRichTextBox.Select(tempRtbStart, len);
rtb.SelectedRtf = tempRichTextBox.SelectedRtf;
rtb.Select(curRtbStart, len);
rtb.Focus();
}
///<summary>
///设置字体格式:粗体、斜体、下划线
///</summary>
/// <param name="style">事件触发后传参:字体格式类型</param>
/// <param name="rtb">具体控件</param>
private void ChangeFontStyle(FontStyle style, RichTextBox rtb)
{
if (style != FontStyle.Bold && style != FontStyle.Italic &&
style != FontStyle.Underline)
throw new System.InvalidProgramException("字体格式错误");
RichTextBox tempRichTextBox = new RichTextBox(); //将要存放被选中文本的副本
int curRtbStart = rtb.SelectionStart;
int len = rtb.SelectionLength;
int tempRtbStart = 0;
Font font = rtb.SelectionFont;
if (len <= 1 && font != null) //与上边的那段代码类似,功能相同
{
if (style == FontStyle.Bold && font.Bold ||
style == FontStyle.Italic && font.Italic ||
style == FontStyle.Underline && font.Underline)
{
rtb.SelectionFont = new Font(font, font.Style ^ style);
}
else if (style == FontStyle.Bold && !font.Bold ||
style == FontStyle.Italic && !font.Italic ||
style == FontStyle.Underline && !font.Underline)
{
rtb.SelectionFont = new Font(font, font.Style | style);
}
return;
}
tempRichTextBox.Rtf = rtb.SelectedRtf;
tempRichTextBox.Select(len - 1, 1); //选中副本中的最后一个文字
//克隆被选中的文字Font,这个tempFont主要是用来判断
//最终被选中的文字是否要加粗、去粗、斜体、去斜、下划线、去下划线
Font tempFont = (Font)tempRichTextBox.SelectionFont.Clone();
//清空2和3
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1); //每次选中一个,逐个进行加粗或去粗
if (style == FontStyle.Bold && tempFont.Bold ||
style == FontStyle.Italic && tempFont.Italic ||
style == FontStyle.Underline && tempFont.Underline)
{
tempRichTextBox.SelectionFont =
new Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style ^ style);
}
else if (style == FontStyle.Bold && !tempFont.Bold ||
style == FontStyle.Italic && !tempFont.Italic ||
style == FontStyle.Underline && !tempFont.Underline)
{
tempRichTextBox.SelectionFont =
new Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style | style);
}
}
tempRichTextBox.Select(tempRtbStart, len);
rtb.SelectedRtf = tempRichTextBox.SelectedRtf; //将设置格式后的副本拷贝给原型
rtb.Select(curRtbStart, len);
}
4)