The reason behind the differences of this.Font = fontdialog1.Font and btnTest.Font
-
This is my first code : private void fontToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowDialog();//display the interface to change font this.Font= fontDialog1.Font; } and the result i get for chaning the font size to 14 is this :
After that, I change the code to this : private void fontToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowDialog();//display the interface to change font btnTest.Font= fontDialog1.Font; } and this is what i get for changing the font size to 14 :
It seems that by using " this.Font " causes the form and the button to resize in proportion with the Font size of the button. However, by using "btnTest.Font" causes only the font size of the button to change without making the form and button to resize in proportion with the font size of the button. Can anyone please tell me why is that so ?
-
This is my first code : private void fontToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowDialog();//display the interface to change font this.Font= fontDialog1.Font; } and the result i get for chaning the font size to 14 is this :
After that, I change the code to this : private void fontToolStripMenuItem_Click(object sender, EventArgs e) { fontDialog1.ShowDialog();//display the interface to change font btnTest.Font= fontDialog1.Font; } and this is what i get for changing the font size to 14 :
It seems that by using " this.Font " causes the form and the button to resize in proportion with the Font size of the button. However, by using "btnTest.Font" causes only the font size of the button to change without making the form and button to resize in proportion with the font size of the button. Can anyone please tell me why is that so ?
The difference is because the Form has its AutoScaleMode property set to Font (which I believe is the VS default for that property). So when you set the Form font size, it triggers an auto scale, but since the Button does not have an AutoScaleMode property, changing the font size does nothing. For what it's worth, if you were using WPF, the button would have resized in both situations. So if you really want that resizing behavior and your requirements allow WPF, I would suggest switching. Otherwise, you may end up having to find some component that does it for you or write all the resizing code yourself.
-
The difference is because the Form has its AutoScaleMode property set to Font (which I believe is the VS default for that property). So when you set the Form font size, it triggers an auto scale, but since the Button does not have an AutoScaleMode property, changing the font size does nothing. For what it's worth, if you were using WPF, the button would have resized in both situations. So if you really want that resizing behavior and your requirements allow WPF, I would suggest switching. Otherwise, you may end up having to find some component that does it for you or write all the resizing code yourself.
Thanks alot! I experimented with the AutoScaleMode property and have a better understanding of the reason.