111,120
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestControlLibrary
{
[DefaultBindingProperty("Value")]
public partial class TestTextBox : TextBox
{
private string value = "";
[DefaultValue("")]
[Bindable(true)]
public string Value
{
get { return this.value; }
set { this.value = value; this.Text = this.value.ToString(); }
}
[Bindable(false)]
[Browsable(false)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}
public TestTextBox()
{
InitializeComponent();
}
}
}
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;
using TestControlLibrary;
namespace Test
{
public partial class Form1 : Form
{
private CurrencyManager cm;
private DataView dv;
private DataSet ds = new DataSet();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable("m_test");
DataRow dr;
dt.Columns.Add("number", typeof(string));
dt.Columns.Add("name", typeof(string));
for (int i = 0; i < 20; i++)
{
dr = dt.NewRow();
dr[0] = "0" + i.ToString("00");
dr[1] = "1" + i.ToString("00");
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
ds.AcceptChanges();
dv = new DataView(dt);
textBox3.DataBindings.Add("Value", dv, "number"); //这个textBox3就是上面的控件
dataGridView1.DataSource = dv;
cm = (CurrencyManager)this.BindingContext[dv];
}
private void button1_Click(object sender, EventArgs e)
{
cm.Position--;
}
private void button2_Click(object sender, EventArgs e)
{
cm.Position++;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (ds.HasChanges())
if (MessageBox.Show("Data changed,Exit?", "Changed", MessageBoxButtons.OKCancel,
MessageBoxIcon.Question) == DialogResult.Cancel)
e.Cancel = true;
}
}
}