Navigation Through Records With TrackBar(Slider)

Introduction

This application will show a different way how to navigate through records
in a data source with a TrackBar (Slider) by using either the mouse or the direction keys(Left, Right, Up, Down).

When you move the slider, the trackbar sends notification messages to indicate the change.
If you reach the end or beginning of the TrackBar by moving the slider you will be informed with a MessageBox


Code and How it works
If the program is started, first the connection to the database and then
the data binding for all the controls on the Form will be established.
private void Form1_Load(object sender, System.EventArgs e)
{
   fnGetDatabaseConnection();
   fnGetDataBindings();
}

private void fnGetDatabaseConnection()
{
    //connection to a data source
    OleDbConnection con=new OleDbConnection(conString);
    //Initializes a new instance of the OleDbDataAdapter class with a SelectCommand.
   OleDbDataAdapter dadapter=new OleDbDataAdapter(selectString, con);
    //Initializes a new instance of the Dataset storing data in a disconnected cache
   dataset=new DataSet();
    //refreshes rows in the DataSet
   dadapter.Fill(dataset, "studentTable");
}

A brief introduction to DataBinding and BindingContext used in the method fnGetDataBindings()
DataBinding is the ability to bind some elements of a data source with some graphical elements of an application.
The data in Windows Form is bound by calling DataBindings.
Windows Forms allow you to bind easily to almost any structure that contains data.
Windows Forms Controls(i.e TextBox etc.) support 2 types of data binding:
1. Simple Data Binding
2. Complex Data Binding
Simple Data Binding can be performed either at design time using DataBindings property of a control OR
dynamicall at run time.For example: a simple Windows Form displays data from a single table in a data grid.
Complex Data Binding is useful if you bind a control to a list of values from a data source.
The ComboBox and ListBox controls also support complex data binding.
For example:
// Simple DataBinding
textBox1.DataBindings.Add("Text", dataset, "studentTable.studentID");
The control textBox1 is bound to the studentID column of a studentTable table on the DataSet(dataset) through the BindingContext object.
BindingContext
Every Windows Form has a BindingContext object keeping track of all the CurrencyManager objects on the Windows Form.
CurrencyManager keeeps track of the position in the data source and keeps data-bound controls synchronized with each other.
When you bind a data object to a control(i.e TextBox), a CurrencyManager object is automatically assigned.
If you bind several controls to the same data source, they share the same CurrencyManager.
For example:
* If you want to know how many records are in a data table, you simply query the BindingContext object's Count property.
this.BindingContext[dataset,"studentTable"].Count - 1 ;
*If you want to get the current position from the BindingContext object
this.BindingContext[dataset, "studentTable"].Position + 1;
Here we pass the table "studentTable" to the BindingContext as the bound control.

Now here is the method for Data Binding.
private void fnGetDataBindings()
{
   //display the current row number in the Label control
    this.label1.Text = "Record : " + ((this.BindingContext[dataset, "studentTable"].Position + 1).ToString());
   //Data binding for the all TextBoxes
   this.textBox1.DataBindings.Add("Text", dataset, "studentTable.studentID");
   this.textBox2.DataBindings.Add("Text", dataset, "studentTable.studentName");
   this.textBox3.DataBindings.Add("Text", dataset, "studentTable.studentSubject");
   //set the upper limit of the range of the TrackBar control
   //count how many records in the table
   //get the current position from the BindingContext object
   //and assign to TackBar.Maximum poperties
   this.trackBar1.Maximum = this.BindingContext[dataset,"studentTable"].Count - 1 ;
   //don磘 forget: first row number is 0. that磗 why Count-1
   // The Maximum property sets the value of the track bar when
   // the slider is all the way to the right.
}

TrackBar(Slider)
There is only one point left to mention, namely the event handler for Scrolling the TrackBar(Slider) control
A TrackBar is a window that has a thumb(slider) and optional tick marks. The thumb can be adjusted.
Its position corresponds to the Value property. When you move the slider, using either the mouse
or the direction keys, the TrackBar sends notification messages to indicate the change.
TrackBar can be aligned horizontally or vertically.
Scroll event will occur when you move the slider either with a mouse or keyboard(Left, Right, Up, Down)

private void trackBar1_Scroll(object sender, System.EventArgs e)
{
   //assign the current value(position starting by 0) of TrackBar to the BindingContext
   this.BindingContext[dataset,"studentTable"].Position = this.trackBar1.Value ;
   //display the position in Label
   this.label1.Text = "Record : " + ((this.BindingContext[dataset, "studentTable"].Position + 1).ToString());
   //check if any records in the table
    if ((this.BindingContext[dataset, "studentTable"].Position ) < (this.BindingContext[dataset, "studentTable"].Count-1))
    {
      this.BindingContext[dataset,"studentTable"].Position +=1; //go to next row
    }
   else
    {
      //inform the user about the end of the records by using the method fnShowMessageBoxWithParameters from the class csShowMessageBox
      csShowMessageBox.fnShowMessageBoxWithParameters("You磛e reached the end of the records", "LAST RECORD",
      MessageBoxButtons.OK,  MessageBoxIcon.Information,0,0);
    }
     //check if you are at the first row in the table and inform the user
     if (this.BindingContext[dataset, "studentTable"].Position ==1)
     {
        csShowMessageBox.fnShowMessageBoxWithParameters("You磛e reached the beginning of the records", "FIRST RECORD",
            MessageBoxButtons.OK, MessageBoxIcon.Information,0,0);
     }
}

Download Source

In conclusion
I designed this program to show a different way how to navigate through records
in a data table with a TrackBar(Slider). I think and hope that I磛e showed some interesting tips
which could help you maybe in some cases.

Good coding and enjoy

Hüseyin Altindag
haltindag@btopenworld.com
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值