代码如下,很简单地的功能:就是继承DataGridView控件,让其始终显示水平与垂直滚动条,但是否可用则由相应的数据集来控制.
现在的问题是:如果在Form1界面设计时删除继承过的CustomDataGridView控件时,则会报"处理此命令时出错.未将对象引用设置到对象的实例",
测试如果仅注释:HorizontalScrollBar.Show();垂直滚动条可以正常显示,水平滚动条不显示.在Form1界面设计时删除也不会报错了.为什么水平滚动条的代码可以正常调用?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add(16);
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; } private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add(1);
} private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
} } public class CustomDataGridView : DataGridView
{ /// <summary>
/// Constructor,添加dataGridView1控件时,直接继承自CustomDataGridView类
/// </summary>
public CustomDataGridView() : base()
{
// 垂直滚动条
VerticalScrollBar.Visible = true;
VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged);
// 水平滚动条
HorizontalScrollBar.Visible = true;
HorizontalScrollBar.VisibleChanged += new EventHandler(HorizontalScrollBar_VisibleChanged);
}
/// <summary>
/// 垂直滚动条
/// </summary>
void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
{
if (!VerticalScrollBar.Visible)
{
int width = VerticalScrollBar.Width;
VerticalScrollBar.Location = new Point(ClientRectangle.Width - width - 1, 1);
VerticalScrollBar.Size = new Size(width, ClientRectangle.Height - this.HorizontalScrollBar.Height -1);
VerticalScrollBar.Show();
}
} /// <summary>
/// 水平滚动条
/// </summary>
void HorizontalScrollBar_VisibleChanged(object sender, EventArgs e)
{
if (!HorizontalScrollBar.Visible)
{
int height = HorizontalScrollBar.Height;
HorizontalScrollBar.Location = new Point(1, ClientRectangle.Height - height - 1);
HorizontalScrollBar.Size = new Size(ClientRectangle.Width - this.VerticalScrollBar.Width -1, height);
HorizontalScrollBar.Show();
}
}
}
}
现在的问题是:如果在Form1界面设计时删除继承过的CustomDataGridView控件时,则会报"处理此命令时出错.未将对象引用设置到对象的实例",
测试如果仅注释:HorizontalScrollBar.Show();垂直滚动条可以正常显示,水平滚动条不显示.在Form1界面设计时删除也不会报错了.为什么水平滚动条的代码可以正常调用?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add(16);
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells; } private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add(1);
} private void button2_Click(object sender, EventArgs e)
{
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentCell.RowIndex);
} } public class CustomDataGridView : DataGridView
{ /// <summary>
/// Constructor,添加dataGridView1控件时,直接继承自CustomDataGridView类
/// </summary>
public CustomDataGridView() : base()
{
// 垂直滚动条
VerticalScrollBar.Visible = true;
VerticalScrollBar.VisibleChanged += new EventHandler(VerticalScrollBar_VisibleChanged);
// 水平滚动条
HorizontalScrollBar.Visible = true;
HorizontalScrollBar.VisibleChanged += new EventHandler(HorizontalScrollBar_VisibleChanged);
}
/// <summary>
/// 垂直滚动条
/// </summary>
void VerticalScrollBar_VisibleChanged(object sender, EventArgs e)
{
if (!VerticalScrollBar.Visible)
{
int width = VerticalScrollBar.Width;
VerticalScrollBar.Location = new Point(ClientRectangle.Width - width - 1, 1);
VerticalScrollBar.Size = new Size(width, ClientRectangle.Height - this.HorizontalScrollBar.Height -1);
VerticalScrollBar.Show();
}
} /// <summary>
/// 水平滚动条
/// </summary>
void HorizontalScrollBar_VisibleChanged(object sender, EventArgs e)
{
if (!HorizontalScrollBar.Visible)
{
int height = HorizontalScrollBar.Height;
HorizontalScrollBar.Location = new Point(1, ClientRectangle.Height - height - 1);
HorizontalScrollBar.Size = new Size(ClientRectangle.Width - this.VerticalScrollBar.Width -1, height);
HorizontalScrollBar.Show();
}
}
}
}
如果一定要在删除这个控件时不会报错,可以将CustomDataGridView 改为
partial class CustomDataGridView : DataGridView
{
/// <summary>
/// Constructor,添加dataGridView1控件时,直接继承自CustomDataGridView类
/// </summary>
public CustomDataGridView()
: base()
{
int width = VerticalScrollBar.Width;
VerticalScrollBar.Location = new Point(ClientRectangle.Width - width - 1, 1);
VerticalScrollBar.Size = new Size(width, ClientRectangle.Height - this.HorizontalScrollBar.Height - 1);
VerticalScrollBar.Show(); int height = HorizontalScrollBar.Height;
HorizontalScrollBar.Location = new Point(1, ClientRectangle.Height - height - 1);
HorizontalScrollBar.Size = new Size(ClientRectangle.Width - this.VerticalScrollBar.Width - 1, height);
HorizontalScrollBar.Show();
}
}