2016-04-28 47 views
0

我想在按鈕內添加圖標。這裏是我的代碼如何在按鈕內顯示圖標windowsForms

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(); 
} 

這裏的問題是,該圖標沒有出現在第一,當我點擊該按鈕出現。

這裏有什麼問題?

+10

你加入printbutton_click事件中的圖標,而不是將其定義表格initializecomponents –

+0

@SebastianSchulz我如何準確地做? –

回答

0

你加入printbutton_click事件中的圖標,而不是將其定義表格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(); 
    } 
+0

@Sebatian Schulz非常感謝:) –

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. 
    } 
相關問題