Navigation Through Records with TrackBar (Slider)

xiaoxiao2021-03-06  83

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 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 establish.

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 disponnected 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 of the big graphhical elements of an application.

The data in windows form is bound by calling data.

Windows Forms 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 Ordynamicall 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 Studient 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, The Same Data Source, The Share The Same CurrencyManager.

For example:

* If you want to know how many records are in a data table, you simply query the TABLE, YOUN

BindingContext Object's

Count Property.

This.BindingContext [DataSet, "StudentTable"]. count - 1;

* If you want to get the current position from the capital

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 limited of the range of the trackbar constro

// 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 by

/// The slider is all the woman to the right.

}

Trackbar (Slider)

There is only one point left l ^ n, namely the Event Handler for Scrolling T

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 direction

TRACKBAR Sends Notification Messages to Indicate The Change.

Trackbar can be aligned horizontally or vertical.

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

THISLABEL1.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",

MessageboxButton.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," first record ",

MessageboxButton.ok, MessageBoxicon.information, 0, 0);

}

}

Download

Source

In conclusionI 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 Chan e showed some interesting tips which could help you maybe in some cases.

Good coding and enjoy

Hüseyin Altindag

Haltindag@btopenworld.com

转载请注明原文地址:https://www.9cbs.com/read-122433.html

New Post(0)