如题,然后在应用程序中使用这个被重写过函数的类,而不是使用原来的类?比如我要重写dgv的ProcessDialogKey(),那么我是不是要继承一个类,然后在这个新类里重写这个函数://先继承
public class MyDataGridView : DataGridView
{
//再重写
protected override bool ProcessDialogKey(Keys keyData)
{
.
.
.
}
.
.
.
}
我之前没有尝试过不继承直接重写,但出错了。我还尝试过把上面这样重写的代码单独放在一个文件中,然而使用的时候也会报一点点小错。
public class MyDataGridView : DataGridView
{
//再重写
protected override bool ProcessDialogKey(Keys keyData)
{
.
.
.
}
.
.
.
}
我之前没有尝试过不继承直接重写,但出错了。我还尝试过把上面这样重写的代码单独放在一个文件中,然而使用的时候也会报一点点小错。
解决方案 »
- 话说C# .NET 给推荐本好书 不要入门的 要提高的
- c# 可以在2个不同的文件定义类属性的特性吗?
- 动态生成类
- 高分急问急问:关于C#程序中DLL引用的问题!!!
- TimerCallBack问题
- 问一个在线程中打开对话框的问题,顶者有分咯,不够再加!!!
- 麻烦大家帮我看看这段生成word报告的代码,不胜感激!不胜感激!不胜感激!
- out string[]可以作为函数的参数吗?
- 在.net中这么把datagird里固定的一个字段变大或变小
- 绝对给分:如何用API在.net环境中显示一个window
- 根据传入的IP地址获取MAC地址,请问如何实现?(C#)
- 在vs2005定位代码时,经常用到向后定位或向前定位,这时会比较难找到关闭的位置(就是一闪一闪表示代码位置),怎样能够快速定位到光标位置
重载就不需要了,OOP概念问题.
{
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
if (!(msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN))
{
return false;
}
if (CheckControl(this.ActiveControl, DataGridView1.Name))//判断当前控件或他的parent是DataGridView1
{
//处理按键
}
else
{
return base.ProcessCmdKey(ref msg,keyData);
}
}
一开始我把这段放在一个Form的类里using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DBSCM.DB;
using DBSCM.IO;
using System.Data.SqlClient;
using System.Collections;namespace DBSCM
{ public partial class frmPO : DevComponents.DotNetBar.Office2007Form
{ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) //激活回车键
{
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
if (!(msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN))
{
return false;
}
if (CheckControl(this.ActiveControl, DataGridView1.Name))//判断当前控件或他的parent是DataGridView1
{
//处理按键
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
} public frmPO()
{
InitializeComponent();
}
.
.
.
}
但是VS报错,于是我就Google了一下C#的override关键字,在http://www.cftea.com/c/2008/04/VETY7GUN1ETHI8XP.asp找到了“重载”与“重写”的说明,然后我把代码改了一下:public partial class MyDGV : DevComponents.DotNetBar.Controls.DataGridViewX
{ protected override bool ProcessCmdKey(ref Message msg, Keys keyData) //激活回车键
{
int WM_KEYDOWN = 256;
int WM_SYSKEYDOWN = 260;
if (!(msg.Msg == WM_KEYDOWN | msg.Msg == WM_SYSKEYDOWN))
{
return false;
}
if (CheckControl(this.ActiveControl, DataGridView1.Name))//判断当前控件或他的parent是DataGridView1
{
//处理按键
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
.
.
.
}
但是这样之后还是有些错误,所以才发帖求助。
没有ProcessCmdKey这个方法
同一作用域。 | 基类与派生类之间。
方法名称相同,但参数个数或类型必须不同。| 方法名称、参数个数和类型都必须相同。
不需要修饰符。 |基类中用 virtual,派生类中用 override。
像我在第10楼所讲的ProcessCmdKey()函数能用重载的方法来自定义处理流程吗?
其实我只是列举,我现在要自定义的函数是DataGridView的ProcessDialogKey()。
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
this.EndEdit(); return true;
}
return base.ProcessDialogKey(keyData);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;namespace dgvdemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.ColumnCount = 4;
dataGridView1.Columns[0].Name = "C1";
dataGridView1.Columns[1].Name = "C2";
dataGridView1.Columns[2].Name = "C3";
dataGridView1.Columns[3].Name = "C4";
} private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int currentcellcolumn = this.dataGridView1.CurrentCell.ColumnIndex;
int currentcellrow = this.dataGridView1.CurrentCell.RowIndex; if (currentcellcolumn == 0)
{
this.dataGridView1[1, currentcellrow].Value ="C2";
this.dataGridView1[2, currentcellrow].Value ="C3";
this.dataGridView1.CurrentCell = this.dataGridView1[3, currentcellrow];
}
} private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
int currentcellcolumn = this.dataGridView1.CurrentCell.ColumnIndex;
if (currentcellcolumn == 1)
{
Form2 frm = new Form2(this);
frm.ShowDialog();
e.Cancel = true;
}
}
}
public class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
this.CommitEdit(DataGridViewDataErrorContexts.Commit);
this.EndEdit(); return true;
}
return base.ProcessDialogKey(keyData);
}
}
}
atlasroben真牛啊!感谢牛人!此帖明天加到200分后结帖。