各位論壇大神,本人設計了一個掃描錄入系統,想請教一下,如何限制edit1只能輸入首字母為B,b以及數字,edit2只能輸入A,a,輸入錯誤則報錯。附上代碼以及界面,現功能為edit1掃描完成接受到enter信號則跳轉到edit2,edit2掃描完成接收到掃描槍的發出的enter則按鈕按下完成錄入,并跳轉edit1,新人一枚,希望能得到大家的幫助謝謝大家

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Menus, SkinCaption, WinSkinData;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Edit2: TEdit;
Button2: TButton;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
MainMenu1: TMainMenu;
N1: TMenuItem;
SkinData1: TSkinData;
SkinCaption1:
procedure Button2Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
with adoquery1 do
begin
close;
sql.text:='insert into saomiao(hougai,ziju) values('''+edit1.Text+''','''+edit2.Text+''')' ;
execsql;
end;
end;
procedure TForm1.N1Click(Sender: TObject);
begin
form2.Show;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then //13 是回車
begin
Edit2.SetFocus;
end;
end;
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
begin
button2.Click;
Edit1.SetFocus;
end;
end;
end.
uj5u.com熱心網友回復:
輸入錯誤能報錯提示uj5u.com熱心網友回復:
if not in [] then或者寫到change里面,判斷下字符長度,如果lengh()長度為1,則if……else……條碼機器可以設定,掃描完成后是否加回車uj5u.com熱心網友回復:
Edit1首字符是B或b,后跟數字:A12312412還是首字符是B或b或數字。
表述不清楚?
uj5u.com熱心網友回復:
同意2樓的OnChange事件中處理做法,補充:
procedure TForm1.Edit1Change(Sender: TObject);
begin
if LowerCase(copy(edit1.Text,1,1)='a' then
//合法;
else
//非法;
end;
uj5u.com熱心網友回復:
4樓有問題是報錯非常頻繁,這是因為掃碼是一次掃入多個字符,如果第一個字符錯了,后面每個字符輸入都會報錯,所以2樓的方法應該更合理,在回車后判斷輸入是否合法,合法跳到edit2,不合法反饋錯誤資訊,重新操作,或者在exit事件里判斷也行uj5u.com熱心網友回復:
首字符是B或b或數字
uj5u.com熱心網友回復:
Edit1 首字符是A或a或數字Edit2 首字符是B或b或數字
1、只能按要求輸入;
2、要求輸入后,如中途洗掉首字符,將清空字符。
這樣不需要,提示錯誤,只要錄入,就符合規范。
全部代碼如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure Edit1Change(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var E:TEdit;
S:string;
begin
with Sender as TEdit do
begin
E:=Sender as TEdit;
S:=E.Text;
if Length(S)=0 then
if not (key in ['0'..'9','A','a']) then
key := #0;
end;
end;
procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
var E:TEdit;
S:string;
begin
with Sender as TEdit do
begin
E:=Sender as TEdit;
S:=E.Text;
if Length(S)=0 then
if not (key in ['0'..'9','B','b']) then
key := #0;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
var E:TEdit;
S:string;
Firstkey:Char;
begin
with Sender as TEdit do
begin
E:=Sender as TEdit;
S:=E.Text;
if Length(S)>0 then
begin
Firstkey:=S[1];
if not (Firstkey in ['0'..'9','A','a']) then
E.Text:='';
end;
end;
end;
procedure TForm1.Edit2Change(Sender: TObject);
var E:TEdit;
S:string;
Firstkey:Char;
begin
with Sender as TEdit do
begin
E:=Sender as TEdit;
S:=E.Text;
if Length(S)>0 then
begin
Firstkey:=S[1];
if not (Firstkey in ['0'..'9','B','b']) then
E.Text:='';
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
end.
uj5u.com熱心網友回復:
效果圖:
uj5u.com熱心網友回復:
搞定了。我估計的意思是edit1前面只能是B,b后面只能是數字 比如b1234334是這樣嗎?代碼如下:if Length(edt1.Text)>0 then begin
if (Copy(edt1.SelText,0,1)='b') or (Copy(edt1.SelText,0,1)='B') then begin
if not(Key in ['B','b',#8]) then begin
Key:=#0;
ShowMessage('輸入不合法');
end;
end
else begin
if not(Key in ['0'..'9',#8]) then begin
Key:=#0;
ShowMessage('輸入不合法');
end;
end
end
else begin
if not(Key in ['B','b',#8]) then begin
Key:=#0;
ShowMessage('輸入不合法');
end;
end;
uj5u.com熱心網友回復:
在KeyPress事件里寫代碼,edit2自己套用!uj5u.com熱心網友回復:
第一次輸入很好控制。麻煩在修改那里!我的思路是選擇需改的內容有B或b那么只能輸入修改成B!如果修改選擇的內容沒用B或b那么就只能輸入數字!比如 edit1輸入了內容 b12345然后 b12被選擇了,那么這個時候只能輸入B或b 。如果234被選擇了那么只能輸入數字!uj5u.com熱心網友回復:
建議采用7樓的做法,選擇KeyPress和change結合的限制方式,效果會好很多
uj5u.com熱心網友回復:
好吧,七樓已經說的很好了。轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80260.html
標籤:語言基礎/算法/系統設計
上一篇:Delphi7中query添加了所有欄位,添加sql陳述句時只搜索顯示某些欄位,為嘛其它不需要的欄位老報錯,提示找不到?
下一篇:關于List回圈性能問題
