如何在按钮windowsForms中显示图标

时间:2016-04-28 09:22:10

标签: c# image winforms

我想在按钮内添加图标。这是我的代码

private void Printbutton_Click(object sender, EventArgs e)
{
    // Assign an image to the button.
    Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");

   // Align the image and text on the button.
   Printbutton.ImageAlign = ContentAlignment.MiddleRight;
   Printbutton.TextAlign = ContentAlignment.MiddleLeft;

   // Give the button a flat appearance.
   Printbutton.FlatStyle = FlatStyle.Flat;

   if (SetupThePrinting())
         printDocument1.Print();
}

这里的问题是图标最初没有出现,当我点击按钮时会出现。

这里有什么问题?

2 个答案:

答案 0 :(得分:0)

您在printbutton_click事件中添加了图标,而不是在Form initializecomponents

中定义它
    public Form2()
    {
        InitializeComponent();

        // Assign an image to the button.
        Printbutton.Image = Image.FromFile("D:\\Downloads\\print.png");
        // Align the image and text on the button.
        Printbutton.ImageAlign = ContentAlignment.MiddleRight;
        Printbutton.TextAlign = ContentAlignment.MiddleLeft;
        // Give the button a flat appearance.
        Printbutton.FlatStyle = FlatStyle.Flat;
    }

    private void Printbutton_Click(object sender, EventArgs e)
    {
        if (SetupThePrinting())
            printDocument1.Print();
    }

答案 1 :(得分:0)

喜欢这个

    public Form1()
    {
        InitializeComponent();

        // Assign an image to the button.
        button1.Image = Image.FromFile("C:\\Yourfolder");
        // Align the image and text on the button.
        button1.ImageAlign = ContentAlignment.MiddleRight;
        button1.TextAlign = ContentAlignment.MiddleLeft;
        // Give the button a flat appearance.
        button1.FlatStyle = FlatStyle.Flat;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Your print code.
    }
相关问题