Home > Software design >  Button image is not displayed with FlatStyle.System
Button image is not displayed with FlatStyle.System

Time:09-01

I am new to Visual Basic.NET, and programming.
I have a Form with 4 buttons, the same image has been assigned to these buttons.
With Flatstyle.Flat, Flatstyle.Standard and Flatstyle.Popup the image is displayed correctly.
With Flatstyle.System the image is not displayed.
I am trying to find a way around this problem. For this I tried 2 solutions:

  1. I drew an image (img1) but I don't know how to put it in the foreground so that it is above the control button.
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim img1 As Image = My.Resources.image
    Dim g As Graphics = e.Graphics
    g.DrawImage(img1, New Rectangle(260, 180, 24, 24))
End Sub
  1. I have drawn the icon (img2) in the button (System) but I don't know how to display it when I open the program, for the moment it appears when I click on button3 (Popup) and disappears when I hover over the button it is drawn on (Button4, System).
Private Sub Popup_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim img2 As Image = My.Resources.image
    Dim g As Graphics = Button4.CreateGraphics()
    g.DrawImage(img2, 9, 13, 24, 24)
End Sub

Have a look at the picture attached : Form1

Thanks for your help

CodePudding user response:

If you want to draw on a Button, handle the Paint event of that Button, not of the form. NEVER call CreateGarphics.

Also, don't access the same property of My.Resources over and over again. It will extract a new object from resources every time. Access the property once and assign the value to a field, then use that field repeatedly.

  • Related