Question Removing a Label from the PictureBox

Shefali

New member
Joined
Feb 14, 2015
Messages
3
Programming Experience
Beginner
Date: 14-Feb-2015

Dear Members,

I am developing a project in VB 2008 where a product image is to be displayed in a picture box when a user selects the product name from DataGridView. All the images are stored on a hard-drive. If image-path is not stored in the Database [Access-2007] for any product, a label [lblNoImage2Display] is created and displayed in the same picture-box saying ?Image not available?.

On selection of certain product for which image-path is not stored in the DataBase, the label is displayed in the picture-box perfectly. But when I move to next product for which image-path is available, the picture-box displays the relevant image along with the label. I have tried setting the VISIBLE property to false of the lblNoImage but it continues to display. The picture-box [pbxProduct] is a design-time control and created on Win-form. The Label is created at Runtime.

I am not able to figure out what?s wrong with the code. I am re-producing the code below:

Dim lblNoImage2Display As New Label
With lblNoImage2Display
.Text = "Nothing to display"
.Location = New Drawing.Point(130, 20)
.AutoSize = True
.Font = New Font("Cambria", 14, FontStyle.Regular, GraphicsUnit.Point)
End With
pbxCard.Controls.Add(lblNoImage2Display)

Dim strFileName As String = dgvList.CurrentRow.Cells.Item("pImagePath").Value.ToString

pbxCard.Image = Nothing
If strFileName.Length > 0 Then
lblNoImage2Display.Visible = False
lblNoImage2Display.SendToBack()
With pbxCard
.Refresh()
.SizeMode = PictureBoxSizeMode.StretchImage
.Image = System.Drawing.Image.FromFile(strFileName)
End With
Else
lblNoImage2Display.Visible = True
End If

Any suggestions/ modifications are awaited to achieve desired result.

Thanks and Regards,
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,587
Location
Norway
Programming Experience
10+
Add this label in designer and just set Visible (or Show/Hide).
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,587
Location
Norway
Programming Experience
10+
The problem still continued.... The label is still displaying over the image of the product.
What code do you have now?
 

Shefali

New member
Joined
Feb 14, 2015
Messages
3
Programming Experience
Beginner
Dear John,

I created the label (lblNoImage2Display) on the WinForm and commented its declaration line from the Code window... i.e. Dim lblNoImage2Display as New Label.

Rest is unchanged.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,587
Location
Norway
Programming Experience
10+
Dear John,

I created the label (lblNoImage2Display) on the WinForm and commented its declaration line from the Code window... i.e. Dim lblNoImage2Display as New Label.

Rest is unchanged.
Then you don't need any of this code, you can do this in designer:
' Dim lblNoImage2Display As New Label
With lblNoImage2Display
.Text = "Nothing to display"
.Location = New Drawing.Point(130, 20)
.AutoSize = True
.Font = New Font("Cambria", 14, FontStyle.Regular, GraphicsUnit.Point)
End With
pbxCard.Controls.Add(lblNoImage2Display)
I also don't see the point with this line:
lblNoImage2Display.SendToBack()
You're then left with this label code in effect:
If strFileName.Length > 0 Then
lblNoImage2Display.Visible = False
Else
lblNoImage2Display.Visible = True
End If
If that is no showing/hiding label as you want then you need to debug to see if condition matches what you think it should.
 
Top Bottom