一、窗体与界面设计
1、设置窗体的默认按钮
窗体的Load事件中设置 this.AccessButton = this.按钮名
2、关闭窗体前弹出确认窗口
Code
private void Main_FormClosing(object sender,
FormClosingEventArgs e)
{
if (MessageBox.Show("将要关闭程序,是否继续?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
3、使窗体居中显示
可设置窗体的StartPosition属性值为CenterScreen,也可设置窗体的坐标。
4、给DataGridView增加表头
DataGridView.Columns[0].HeaderText = "名称";
5、给窗体增加状态栏
实现效果:
通过StatusStrip显示窗体状态栏
同时将状态栏分成三部分
居左边显示相关文字信息
中间空白显示
居右边显示时间信息
(1).创建窗体及添加StatusStrip
默认StatusStrip名称为statusStrip1
(2).在statusStrip1的Items属性中
添加三个StatusLabel
默认名称为toolStripStatusLabel1,2,3
按1,2,3的顺序排列
(3).修改toolStripStatusLabel1的Text属性
为相关文字如"欢迎使用本系统"
(4).修改toolStripStatusLabel2的Text属性 为空
Sprint属性为True
BorderSides属性为Left,Right
(5).修改toolStripStatusLabel3的Text属性 为空
在Form的Load事件中 修改其显示为当前时间
this.toolStripStatusLabel3.Text = "登录时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
(6).如果要使状态栏时间信息随操作系统当前时间不停的改变
则可以通过增加Timer控件来实现
增加Timer控件 timer1
编写其Tick事件为
private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripStatusLabel3.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
在Form的Load事件中 对timer1进行相关设置
private void MainForm_Load(object sender, EventArgs e)
{
this.toolStripStatusLabel3.Text = "系统当前时间:" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
this.timer1.Interval=1000;
this.timer1.Start();
}
6、DataGridView列数据显示顺序与数据源数据顺序不同问题
设置datagridview.Columns[0].DisplayIndex = newIndex;
7、使用单例模式创建窗体
private static Form1 frmNew = null;
将窗体的构造函数定为私有,避免在外部直接创建窗体
然后定义一个公有静态方法,供外部调用该方法获取此窗体的唯一实例
public static Form1 GetInstance()
{
if(frmNew == null || frmNew.IsDisposed)
{
frmNew = new Form1();
}
return frmNew;
}
好处:全局(单线程中)就一个Form1的实例
坏处:该实例一旦创建,将一直存在,除非Disposed
注意:如果是多线程,则需要对GetInstance方法进行改进,使用lock进行判断
8、Show与ShowDialog方法的区别
Show窗体显示为无模式窗体,Show方法后面的代码会立即执行,且无模式窗体与主窗体可以同时进行操作
ShowDialog显示为模式窗体,ShowDialog方法后面的代码在未关闭模式窗体前不会执行,单击对话框的关闭窗体按钮或设置DialogResult属性的值时,不调用窗体的Close方法,实际上是把窗体的Visible属性赋值为false,隐藏窗体。
9、集合已修改;可能无法执行枚举操作
把与遍历DataGridView与DataSet的循环foreach改成for,foreach是取只读的,在取的时候数据不能改变(包括修改,删除,添加等)
10、在DataGridView插入checkbox列
DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
column.Name = "cbSelect";
column.HeaderText = "选择";
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
column.FlatStyle = FlatStyle.Standard;
column.CellTemplate = new DataGridViewCheckBoxCell();
column.CellTemplate.Style.BackColor = Color.Beige;
11、使用System.IO命名空间中的DriveInfo类获取计算机的磁盘信息
DriveInfo[] myAllDrives = DriveInfo.GetDrives(); 取得目前系统中所有逻辑磁盘驱动器
判断磁盘是否已就绪,可用IsReady属性
12、解决使用Form.ShowDialog打开新窗体,会刷新两次的问题
Form.ShowDialog();
if(Form.DialogResult == DialogResult.OK)
{
//操作
}
原来是if(Form.ShowDialog() == DialogResult.OK)
{
}
这样就会刷新两次
13、多次引用静态数据,必须先清除原有数据,否则会造成数据重复。
14、子窗体引用父窗体
在创建子窗体的位置加上
Form2.Owner = this;
在使用父窗体时
Form1 frm1 = (Form1)this.Owner;
15、设置DataGridView里的控件默认值
在窗体的Load事件中进行设置 即可
dgvData2[1, 0].Value = ((DataGridViewComboBoxCell)dgvData2[1, 0]).Items[0].ToString();
二、Web
1、“SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间”
如果有model层,可在相应的类中设置其DateTime类型为DateTime? 可空类型,即可往数据库datetime字段插入空值。
2、在客户端对表格动态增加表格行及客户端控件
<table id="myTable" width="98%" align="center" border="1" cellpadding="2" cellspacing="1">
<tr>
<td>
<div align="right">候选个数</div>
</td>
<td>
<select id="dropChoicesCount" onchange="selectedchange()" name="sel" runat="server" class="commdrop">
<option value="4">4</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
</tr>
<tbody id="selecttbody">
<tr id="trTemplete">
<td align="right">供选答案1</td>
<td><input type="text"/></td>
</tr>
</tbody>
</table>
<script language="javascript" type="text/javascript">
var tbody;
var template; //模板行
window.onload = function() {
tbody = document.getElementById("selecttbody");
template = document.getElementById("trTemplete");
selectedchange();
}
function ClearRows() {
var newBody = document.createElement("tbody");
var tbl = document.getElementById("myTable");
tbl.replaceChild(newBody, tbody);
tbody = newBody;
}
function NewRow() {
//利用cloneNode(true)来复制
//就算有100个td,每个td都有不同的样式,都一次搞定!!!
var newRow = template.cloneNode(true);
tbody.appendChild(newRow);
return newRow;
}
function selectedchange() {
var n = document.getElementById("dropChoicesCount").value;
n = parseInt(n);
ClearRows();
for (var i = 0; i < n; i++) {
var nr = NewRow();
nr.cells[0].innerHTML = "供选答案" + (i + 1);
}
}
</script>
三、Javascript
1、JS输出大写字母 String.fromCharCode(65)-->A
2、客户端对FCKeditor的值进行空值验证
在<FCKEDITOR>标记后面加上一个CustomValidator控件
<FCKeditorV2:FCKeditor ID="fckInfo" runat="server" Width="98%" Height="200px" ToolbarSet="Basic">
</FCKeditorV2:FCKeditor>
<asp:CustomValidator ID="valxfck_info" runat="server" ClientValidationFunction="CheckFCKText" ErrorMessage="题目内容不能为空" ValidateEmptyText="True" Display="Dynamic">
</asp:CustomValidator>
<script type="text/javascript">
/* 检测FCKeditor的值是否为空
-------------------------------*/
var oEditer;
function CheckFCKText(source,arguments)
{
var value = oEditer.GetXHTML(true);
if(value == "")
{
arguments.IsValid = false;
}
else
{
arguments.IsValid = true;
}
}
function FCKeditor_OnComplete(editorInstance)
{
oEditer = editorInstance;
}
</script>
3、获取表格中tbody集合
var otbodys = document.getElementById("otable").tBodies;