高手进入!!!!!请再次关注:delphi7中的TListBox.Create问题!现有的所有分! (17分)

  • 主题发起人 主题发起人 zhang_yz
  • 开始时间 开始时间
Z

zhang_yz

Unregistered / Unconfirmed
GUEST, unregistred user!
求助了!http://www.delphibbs.com/delphibbs/dispq.asp?lid=2520853
在一个窗体中调用
SetDBComboxIterm(TListBox(ComboBox1),'SP');
(SetDBComboxIterm在一个公用函数中,没有窗体)
procedure SetDBComboxIterm(var dbcbxField:TListBox;strName:String);
var
i:Integer;
adoqryTemp:TADOQuery;
begin
adoqryTemp := TADOQuery.Create(nil);
with adoqryTemp do
begin
Close;
Connection := dmRhcdb.ADOConnectionRhcdb;
SQL.Clear;
SQL.Add('select CItem from T_Zdwh where Cname = '''+strName+'''');
Open;
dbcbxField.Clear;
for i:=1 to RecordCount do
begin
if (i = 1) then
First
else
Next;
dbcbxField.Items.Add(FieldByName('CItem').AsString);////此处出现问题!
end;
Close;
end;
adoqryTemp.Free;
end;
请问高手怎样修改!(我印象当中在delphi5中编译好象没有问题)
我看过一些相关资料
要队TlistBox.Parent付值,但我怎么都调不通!!!
或者在开发增加
dbcbxField := TListBox.Create() //但参数该如何写!
请调试!

还有一个问题:
我用到一个DbGrid,动态显示单位信息(就选用一个字符型字段)。
但在显示过程中,我增加的单位并不是按增加的先后顺序显示,而是随着增加的单位,在DbGrid表中显示的次序乱了,有什么办法可以按录入单位的先后顺序进行依次排序。
前提尽量不修改原数据库结构什么的,
求众位了!

 
dbcbxField 之前有没有 CREATE 呢?代码好象没有问题。
 
to:mstar
谢谢!
dbcbxField 之前没有 CREATE !
但编译时确实有问题!
 
TempListBox := TListBox.Create(Self);
TempListBox.Parent:=Self;
 
ComboBox1 和 TLISTBOX 是不同的
 
????????????????????????????????????????????
解决不了吗???
 
dbcbxField是不是连着adoqryTemp
这样的话有可以出错
 
高手是不会回答这种标题的问题的吧? ^_^
 
刘麻子,你是高手,我改标题了,请回答!谢谢!
 
TO:刘麻子
不回答了吗?!
 
????????
还没人回答!
 
interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, DBTables;

type
TForm1 = class(TForm)
ComboBox1: TComboBox;
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure SetDBComboxIterm(var dbcbxField: TListBox; strName:String);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.SetDBComboxIterm(var dbcbxField: TListBox; strName:String);
var
i:Integer;
qry: TQuery;
begin
with TQuery.Create(Nil) do
begin
DataBaseName := 'DBDEMOS';
Close;
SQl.Text := 'SELECT Name, Continent FROM Country where Continent =:Continent';
ParamByName('Continent').AsString := strName;
try
Open;
while not Eof do
begin
//调用 addItem方法,

dbcbxField.AddItem(FieldByName('Name').AsString, nil);
//dbcbxField.Items.Add(FieldByName('Name').AsString);
Next;
end;
finally
Close;
Free;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SetDBComboxIterm(ListBox1, 'South America');
SetDBComboxIterm(TListBox(ComboBox1), 'South America');
end;

end.
 
To:wangler,

你写的程序我也是可以调通的,没有问题!
不过!可能我没说清楚!在有窗口调函数的,是一个现在的ComboBox1的控件,给强行转换成TlistBox的。
你看这样可以调通吗?

unit Unit2;
interface
uses
Forms,StdCtrls;
Function SelectItemFromList(LbxOut:TListBox): String;
implementation
Function SelectItemFromList(LbxOut:TListBox): String;
var
tempvalue : String ;
begin
//判断输出控件和输出字符串列表
LbxOut.ParentWindow := Application.Handle;
tempvalue := 'aaaa';
LbxOut.Visible := False;//
if LbxOut = nil then
Exit;
if (LbxOut <> nil) then
LbxOut.Clear;
LbxOut.Items.Add(tempvalue);
end ;
end.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
//var
//aListBox: TListBox;
begin
//aListBox := TListBox.Create(nil);
SelectItemFromList(TListBox(ComboBox1));
ShowMessage(TListBox(ComboBox1).Items.Strings[0]);
//aListBox.free;
end;

end.

具体:http://www.delphibbs.com/delphibbs/dispq.asp?lid=2520853
 
unit Unit2;

interface

uses
StdCtrls, DBTables;

procedure SetDBComboxIterm(dbcbxField: TListBox; strName:String);

implementation

procedure SetDBComboxIterm(dbcbxField: TListBox; strName:String);
begin
if dbcbxField = nil then exit;
with TQuery.Create(Nil) do
begin
DataBaseName := 'DBDEMOS';
Close;
SQl.Text := 'SELECT Name, Continent FROM Country where Continent =:Continent';
ParamByName('Continent').AsString := strName;
try
Open;
while not Eof do
begin
//调用 addItem方法, 此处是关键
//注意ListBox.Items.Add 与 Combobox.Items.Add区别,具体请参看vcl源代码
dbcbxField.AddItem(FieldByName('Name').AsString, nil);
dbcbxField.Items.Add(FieldByName('Name').AsString);
Next;
end;
finally
Close;
Free;
end;
end;
end;
end.

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ComboBox1: TComboBox;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
ListBox: TListBox;
Combobox: TCombobox;
begin
SetDBComboxIterm(ListBox1, 'South America');
SetDBComboxIterm(TListBox(ComboBox1), 'South America');
end;

end.
 
多人接受答案了。
 
后退
顶部