2015-10-16 69 views
0

嘗試更改表單的高度。在窗體我試圖去改變它給我的錯誤:嘗試更改表單的高度時出錯

"Access violation at address 005B9963 in module 'M1Project.exe. Read of address 0000005C."

在其他形式我可以改變高度精細,它只是這個形式。有誰知道爲什麼會出現這個錯誤? 下面的代碼:

procedure TExamQuestions.RandomQuestionButtonClick(Sender: TObject); 
    var 
    gif:TgifImage; 
    filelocation,temp:string; 
    i,x,cycle,questionid,length:integer; 
    questionpool: array [0..81] of integer; 
begin 
    Randomize; 
    cycle:=random(80)+1; 
    questionid:= cycle mod x; 
    currentquestion:=questionpool[questionID]; 
    temp:=inttostr(currentquestion); 
    if temp='0' then 
    temp:=inttostr(questionpool[x]); 
    gif:=TgifImage.Create; 
    loginmenu.ADOQuery1.SQL.Clear; 
    loginmenu.ADOQuery1.SQL.Add('SELECT question from examquestions where questionID='+temp+''); 
    loginmenu.ADOQuery1.Active:=true;    
     filelocation:=loginmenu.datasource1.DataSet.FieldByName('Question').AsString; 
    filelocation:=('O:\Subjects\Computing\Year 13 EXEs\DButcher\A2 bant\Project = (\Program\Resources\List\'+filelocation); 
    gif.loadfromfile(filelocation); 
    length:=gif.Height; 
    ExamQuestionPicutre.Height:=length; 
    background.ClientHeight:=length; 
    ExamQuestionPicutre.Picture.assign(gif); 
    examquestions.Height:=length; 
end; 

回答

2

的原因是,examquestionsnil。我們看不到它初始化的位置,或者看起來沒有初始化。

人會想象examquestions聲明是這樣的一個全局變量:

var 
    ExamQuestions: TExamQuestions; 

如果形式自動創建的,則.dpr文件將包含該行:

Application.CreateForm(TExamQuestions, ExamQuestions); 

但它看起來就像你刪除了該行並手動創建了表單。這很好,確實是一個很好的舉措。但是當你這樣做時,你還應該刪除IDE爲你添加的無用的全局變量。這隻會導致這樣的混亂。

在任何情況下,您都不應該使用全局實例變量來運行Self。更換

examquestions.Height:=length; 

Height:=length; 

最後,你似乎沒有考慮在所有的字母大小寫。誠然,Pascal不區分大小寫,但只是決定使用小寫字母表示您鍵入的所有代碼,這會使代碼難以閱讀。不要這樣做。使用Pascal案例。不要將Pascal案件與所有小寫字母混合使用。實際上根本不要混合。始終如一。

+0

您還應該考慮局部變量「length」的另一個名稱,因爲「Length」也是內部函數的名稱。 –

相關問題