Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Specify an external font for the control.
#2
C# code use chatgpt:
 
Code:
Copy      Help
void Main()
{
    var form = new Form();
    var label = new Label();
    var button = new Button();
 
    form.Text = "Test Form";
    form.Controls.Add(label);
    form.Controls.Add(button);
 
    label.Text = "Hello World";
    label.Width = 200; // Set the width of the label to 200
 
    button.Text = "Confirm";
    button.Width = 100; // Set the width of the button to 100
    button.Location = new Point(100, 100); // Set the position of the button to x=100, y=100
    button.Click += (sender, e) => {
        MessageBox.Show("The Confirm button has been clicked!");
    };
 
    ApplyFontToControls(form);
 
    form.ShowDialog();
}
 
void ApplyFontToControls(Control parentControl)
{
    PrivateFontCollection pfc = new PrivateFontCollection();
    string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string fontPath = Path.Combine(desktopPath, "SourceSans3-Regular.ttf");
 
    try
    {
        pfc.AddFontFile(fontPath);
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error loading font file: " + ex.Message);
    }
 
    Font font = new Font(pfc.Families[0], 14);
 
    foreach (Control control in parentControl.Controls)
    {
        control.Font = font;
    }
}


Messages In This Thread
RE: Specify an external font for the control. - by Davider - 10-11-2023, 03:00 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)