2012-01-30 4 views
1

다음 코드를 사용하여 폼의 가로 가운데에 단추 컨트롤을 정렬하려면 다음과 같이하십시오.양식 컨트롤을 가로로 가운데 맞춤하기

let myForm:Form = new Form() 
myForm.Text <- "myForm" 
myForm.Height <- 500 
myForm.Width <- 500 
let button:Button = new Button() 
button.Text <- "Click" 
myForm.Controls.Add(button) 
button.Location <- Point(200, 20) 
// Using this code to align this control to center 

컨트롤을 수평 가운데 정렬하는 좋은 방법을 제안 해주세요. Form이 최대화되면 위의 내용은 작동하지 않습니다. 다른 좋은 습관이 있다면 코드를 개선하라고 제안하십시오.

감사합니다.

답변

2

번역의 @의 ispiro의 대답 -이 만 생성자에 happending 경우 양식이

Form1.Resize.Add (fun _ -> button1.Location <- new Point(this.ClientSize.Width/2 - button1.Width/2, button1.Location.Y)) 

크기를 조정할 때마다 가운데로 할 경우, 너는 할 수있어

button.Location <- Point(myForm.Width/2 - button.Width/2, button.Location.Y) 
3

이렇게하면 C#으로 처리 할 수 ​​있습니다. 어쩌면 당신을 도울 수 있습니다.

Resize += new EventHandler(Form1_Resize); 

그리고 : F 번호에

void Form1_Resize(object sender, EventArgs e) 
{ 
    button1.Location = new Point(this.ClientSize.Width/2 - button1.Width/2, button1.Location.Y); 
} 
관련 문제