全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1022
推到 Plurk!
推到 Facebook!

使用滑鼠滾輪轉動後將資料帶至Edit1.text問題

答題得分者是:mephise
kevinsoung
一般會員


發表:26
回覆:34
積分:12
註冊:2011-11-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-02-14 16:36:33 IP:60.248.xxx.xxx 訂閱
各位前輩 我有一個Tfrom1在窗內有一個TDBGrid顯示出資料  我用下方式上滑鼠滾輪可以滾動TDBGrid上的資料  我要如何將滑鼠滾輪經過的地方將TDBGrid的第一個欄位的資料放入Edit1.text內 請各位前輩幫幫忙  萬般感激不盡 
[code delphi]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, SMDBGrid;
type
TDBGrid=class(DBGrids.TDBGrid)
public
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
end;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
Edit1: TEdit;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TDBGrid.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;MousePos: TPoint): Boolean;
begin
if WheelDelta < 0 then
datasource.DataSet.Next;
if wheelDelta > 0 then
DataSource.DataSet.Prior;
end;
end.
[/code]
編輯記錄
kevinsoung 重新編輯於 2012-02-14 01:37:22, 註解 無‧
t27
中階會員


發表:34
回覆:95
積分:90
註冊:2002-06-19

發送簡訊給我
#2 引用回覆 回覆 發表時間:2012-02-14 22:19:55 IP:218.160.xxx.xxx 訂閱
 procedure TFrmBS00A3.DBGridMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
inherited;
if MasterSrc.DataSet <> nil then
begin
if (Sender is TDBGrid) then
begin
with TDBGrid(Sender).DataSource.DataSet do
begin
if not EOF then Next;
end;
Handled := True;
//GetDataFromDBF(TDBGrid(Sender).DataSource,self);
//DispData;
edit1.text := TDBGrid(Sender).......
end;
end;
end;

procedure TFrmBS00A3.DBGridMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
inherited;
if MasterSrc.DataSet <> nil then
begin
if (Sender is TDBGrid) then
begin
with TDBGrid(Sender).DataSource.DataSet do
begin
if not BOF then Prior;
end;
Handled := True;
//GetDataFromDBF(TDBGrid(Sender).DataSource,self);
//DispData;
edit1.text := ........
end;
end;
end;
編輯記錄
t27 重新編輯於 2012-02-14 07:21:03, 註解 無‧
kevinsoung
一般會員


發表:26
回覆:34
積分:12
註冊:2011-11-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2012-02-15 08:49:51 IP:60.248.xxx.xxx 訂閱
感謝前輩的回覆
我將您提供的放入後如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, SMDBGrid, StdCtrls;
type
TDBGrid=class(DBGrids.TDBGrid)
public
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
end;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TDBGrid.DoMouseWheel(Shift: TShiftState; WheelDelta: Integer;MousePos: TPoint): Boolean;
begin
if WheelDelta < 0 then
Begin
datasource.DataSet.Next;
End;
if wheelDelta > 0 then
DataSource.DataSet.Prior;
end;
procedure TForm1.DBGridMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
inherited;
if MasterSrc.DataSet <> nil then
begin
if (Sender is TDBGrid) then
begin
with TDBGrid(Sender).DataSource.DataSet do
begin
if not EOF then Next;
end;
Handled := True;
//GetDataFromDBF(TDBGrid(Sender).DataSource,self);
//DispData;
edit1.text := TDBGrid(Sender).......
end;
end;
end;
procedure TForm1.DBGridMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
begin
inherited;
if MasterSrc.DataSet <> nil then
begin
if (Sender is TDBGrid) then
begin
with TDBGrid(Sender).DataSource.DataSet do
begin
if not BOF then Prior;
end;
Handled := True;
//GetDataFromDBF(TDBGrid(Sender).DataSource,self);
//DispData;
edit1.text := ........
end;
end;
end;
end.

但是不行無法執行
Build 出現以下訊息
[Warning] Unit1.pas(45): Return value of function 'TDBGrid.DoMouseWheel' might be undefined
[Error] Unit1.pas(47): Undeclared identifier: 'DBGridMouseWheelDown'
[Error] Unit1.pas(47): ';' expected but '(' found
[Error] Unit1.pas(48): ';' expected but ')' found
[Error] Unit1.pas(50): This form of method call only allowed in methods of derived types
[Error] Unit1.pas(52): Undeclared identifier: 'MasterSrc'
[Error] Unit1.pas(54): Undeclared identifier: 'Sender'
[Error] Unit1.pas(54): Operator not applicable to this operand type
[Error] Unit1.pas(64): Undeclared identifier: 'edit1'
[Error] Unit1.pas(64): '(' expected but ':=' found
[Error] Unit1.pas(64): Statement expected, but expression of type 'TDBGrid' found
[Error] Unit1.pas(67): '.' expected but ';' found
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'

我用的是DelPhi 7 可否協助排除問題
感激不盡萬般感謝

kevinsoung
一般會員


發表:26
回覆:34
積分:12
註冊:2011-11-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2012-02-17 08:38:06 IP:60.248.xxx.xxx 訂閱
有哪位前輩可以協助我
幫忙處理這個問題
感激不盡

mephise
高階會員


發表:4
回覆:149
積分:205
註冊:2004-02-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2012-02-17 18:02:04 IP:210.69.xxx.xxx 訂閱
光是程式碼Copy進來可不行, 你還要宣告啊
錯誤訊息說的很清楚, 你使用了未宣告的物件
所以請注意 type 部分的宣告:

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Grids, DBGrids, SMDBGrid, StdCtrls;
type
TDBGrid=class(DBGrids.TDBGrid)
public
function DoMouseWheel(Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint): Boolean; override;
end;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Table1: TTable;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
這邊要宣告
procedure TForm1.DBGridMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint; var Handled: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
------
Mephise Chen
前興德工程師
系統時間:2017-10-24 19:30:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!