Form Background Image Disappears

Y

yEaH rIgHt

'-- Remove Image
Me.BackgroundImage = Nothing


jcrouse said:
The following code works great:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim bm As New Bitmap(OpenFileDialog1.FileName)
Me.BackgroundImage = bm
End If
End Sub

Now, how do i remove the background Image or set it to null or nothing?

Thanks,
John
 
G

Guest

Another thing. I use the following code on form2 (frmLC):

Private Sub chkP1JoyUp_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkP1JoyUp.CheckedChanged
If chkP1JoyUp.Checked = True Then
frm1.lblP1JoyUp.Visible = True
frm1.lblP1JoyUp.Top = (frm1.Height / 2) - (frm1.lblP1JoyUp.Height / 2)
frm1.lblP1JoyUp.Left = (frm1.Width / 2) - (frm1.lblP1JoyUp.Width / 2)
Else
frm1.lblP1JoyUp.Visible = False
frm1.lblP1JoyUp.Top = 10
frm1.lblP1JoyUp.Left = 10
End If
End Sub

If I check a checkbox it makes a label on form1 visible and moves it to the center of the form. It now seems to be broken. The label never becomes visible when I step through the code in my debugger. This happened after I change my code to your method for switching between forms (which works great). Any ideas what I'm missing here?

Thanks,
John
 
C

Cor Ligthert

Hi John,

As I said in this thread, properties can set to nothing.
I would make this a little bit else because bitmaps seems to be expensive
for memory and therefore should be disposed when not used anymore.

I hope this is what you where looking for?

Cor
\\\
Private bm As Bitmap
Private Sub Button2_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openfiledialog1 As New OpenFileDialog
If openfiledialog1.ShowDialog() = DialogResult.OK Then
bm = New Bitmap(openfiledialog1.FileName)
Me.BackgroundImage = bm
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Me.BackgroundImage = Nothing
bm.Dispose()
End Sub
///
 
Y

yEaH rIgHt

Do you have a reference to form1 in form2? You need a reference to that form
if you want to minipulate any of the labels.



jcrouse said:
Another thing. I use the following code on form2 (frmLC):

Private Sub chkP1JoyUp_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles chkP1JoyUp.CheckedChanged
If chkP1JoyUp.Checked = True Then
frm1.lblP1JoyUp.Visible = True
frm1.lblP1JoyUp.Top = (frm1.Height / 2) - (frm1.lblP1JoyUp.Height / 2)
frm1.lblP1JoyUp.Left = (frm1.Width / 2) - (frm1.lblP1JoyUp.Width / 2)
Else
frm1.lblP1JoyUp.Visible = False
frm1.lblP1JoyUp.Top = 10
frm1.lblP1JoyUp.Left = 10
End If
End Sub

If I check a checkbox it makes a label on form1 visible and moves it to
the center of the form. It now seems to be broken. The label never becomes
visible when I step through the code in my debugger. This happened after I
change my code to your method for switching between forms (which works
great). Any ideas what I'm missing here?
 
N

news.microsoft.com

I believe so. I have the following code at the top of form1:

Imports System
Imports System.IO
Imports System.Xml
Public Class Form1
Inherits System.Windows.Forms.Form
Dim mouseX As Integer
Dim mouseY As Integer
Dim _mouseDown As Boolean = False
Dim myMouseDown As Boolean
Dim WithEvents frmlc As frmLabelControls
Dim bmGlobal As Bitmap

The next to last line should do it.

????,
John
 
C

Cor Ligthert

Hi John,

Again, why not using that shared class I showed you.
With settings things in that from form2 you can do everything in the visible
event from form1.

Cor
 
J

jcrouse

Cor...I think I found it in your earlier post. I do not understand it.
Again, i am a n00b. It appears to examine the checkboxes as an array? My
checkboxes are individual controls. I may put them in an array for
addressing purposes in a loop at a later time when I understand .Net 2003
syntax a little bit better. Right now I just want to get it working. Here is
the code from my test application:

Form 1:

Public Class Form1
Inherits System.Windows.Forms.Form
Dim WithEvents frm2 as Form2

Windows Form Designer Generated Code...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If frm2 Is Nothing OrElse frm2.IsDisposed Then

frm2 = New Form2

End If

frm2.Show()

Me.Hide()

End Sub

Private Sub frm2_visiblechanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles frm2.VisibleChanged

Me.Show()

End Sub



Private Sub frm2_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles frm2.Closing

frm2 = Nothing

End Sub

End Class

Public Class Checked

Private Shared mCheck(25) As Boolean

Public Shared Sub SetCheck(ByVal index As Integer, ByVal setting As
Boolean)

mCheck(index) = setting

End Sub

Public Shared Function GetCheck(ByVal index As Integer) As Boolean

Return mCheck(index)

End Function

End Class





Form 2:



Public Class Form2

Inherits System.Windows.Forms.Form

Dim frm1 As Form1

Windows Form Designer Generated Code...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim frm1 As New Form1

Me.Hide()

End Sub



Public Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles CheckBox1.CheckedChanged

If CheckBox1.Checked = True Then

CheckBox1.Checked = Checked.GetCheck(1)

End If

End Sub

End Class
 
Y

yEaH rIgHt

Umm, you don't need this in Form1. Unless, of course, you created your
lables this way. Did you?

'-- You don't need this
Dim WithEvents frmlc As frmLabelControls


You need this in form2

Dim frm1 as Form1

Public WriteOnly Property SetForm() As Form1
Set(ByVal Value As Form1)
frm1 = Value
End Set
End Property

Then in form1, Set the form. Like this.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If fm Is Nothing OrElse fm.IsDisposed Then
fm = New Form2()
fm.SetForm = Me
End If
fm.Show()
Me.Hide
End Sub
 
Y

yEaH rIgHt

Remove the Dim frm1 As New Form1 in the Button click event in form2. You
created a new Form1 with that code. Why?
 
J

jcrouse

The only place that string appears in Form2 is here:

Public Class frmLabelControls
Inherits System.Windows.Forms.Form
Dim frm1 as New Form1

I am suspecting the code you are looking at has since been changed. Please
see the other post for the code from my test app.

Thank you for your help,
John
 
J

jcrouse

It seems that I need form2 declared somehow in form1. If I comment out this
line:

Dim WithEvents frm2 as Form2

then the following code is no good.\

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If frm2 Is Nothing OrElse frm2.IsDisposed Then

frm2 = New Form2

frm2.SetForm = Me

End If

frm2.Show()

Me.Hide()

End Sub



It underlines all of the frm2's in the Button1 Click event since they are
not declared.



Thanks, what now, I think we're close,

John
 
C

Cor Ligthert

Hi John,

I thought bellow is your whole application.
(With 2 textboxes, without the XML, without BackImage)

It is just a sample, I have done nothing to let it look nice.

I hope this shows you an easy way?

Cor
-------------------------------------------------------
It needs a project with 2 forms
on form 1
2 labels and a button
on form 2
2 checkboxes and a button

And than paste in this code


\\\Form 1
Private WithEvents frm2 As New Form2
Private lblArea() As Label = New Label() {Label1, Label2}
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
lblArea = New Label() {Label1, Label2}
frm2.Show()
frm2.TopLevel = True
End Sub
Private Sub frm2_VisibleChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles frm2.VisibleChanged
For i As Integer = 0 To lblArea.Length - 1
lblArea(i).Visible = Checked.GetCheck(i)
Next
Me.Show()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e _
As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
frm2.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm2.Show()
Me.Hide()
End Sub
End Class
Public Class Checked
Private Shared mCheck(25) As Boolean
Public Shared Sub SetCheck(ByVal index As _
Integer, ByVal setting As Boolean)
mCheck(index) = setting
End Sub
Public Shared Function GetCheck(ByVal _
index As Integer) As Boolean
Return mCheck(index)
End Function
'End Class
///
\\\form2
\\\Form2
Private chkArea() As CheckBox
Private Sub Form2_Load(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles MyBase.Load
chkArea = New CheckBox() {CheckBox1, CheckBox2}
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To chkArea.Length - 1
Checked.SetCheck(i, chkArea(i).Checked)
Next
Me.Hide()
End Sub
///
 
J

jcrouse

With the above mention line in, everything works the way it should. I really
want to say thanks for your (and Cor's) help on this issue. See my posts in
a few days when I start the XML reader portion. That ought to be fun. I'll
be looking for you Cor.

Thanks,
John
 
J

jcrouse

WOW, thanks Cor. That's a lot of effort. I appreciate it. I'll look at it
this evening. No the code I've been posting is just a sample application. My
real code is probably 30 pages long. Mostly because i haven't implimented
and arrays or loops yet. Just lots of IF then ELSE's until I get the hang of
it. Believe it or not the sad thing here is that I actually taught a few
semesters of Basic and about 6 semesters of Intro to VB5 and VB6 at the
local college. Things sure have changed. I don't know how you guys keep up
with it all.

Later,
John
 
Y

yEaH rIgHt

Cor,
This will cause an error if the user closes Form2 and the clicks this again.
Go ahead and try it. I think this project is now going backwards instead of
forwards. Good Luck

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
frm2.Show()
Me.Hide()
End Sub
 
C

Cor Ligthert

Hi,
This will cause an error if the user closes Form2 and the clicks this again.
Go ahead and try it. I think this project is now going backwards instead of
forwards. Good Luck

May I show John, how to use a shared class, which prevents him from writting
at least 25 complext methods with events?

Your sample has one and it is even difficult to explain

What you told will not give an error, however I left a piece what should be
removed
The last 2 rows should be deleted.

And to prevent that the user closes form 2 you can hide the control box or
prevent just closing of that form using the control box by.

Private Sub Form2_Closing(ByVal sender As _
Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles
MyBase.Closing
e.Cancel = True
End Sub

Cor
 
Y

yEaH rIgHt

It won't create an error? You're nuts. You didn't tell him to cancel the
close or hide the control box, did you?
Your sample has one and it is even difficult to explain

People with low IQs (Cor), everything is hard to explain. Good riddance
genuis.
 
S

scorpion53061

Why are you hostile to Cor? He was trying to help. You should have just
said "thank you" and been on your way.
 
Y

yEaH rIgHt

Thank you for what? Please explain.



scorpion53061 said:
Why are you hostile to Cor? He was trying to help. You should have just
said "thank you" and been on your way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top