Dynamic Button where click closes form

nzcam

Member
Joined
Aug 29, 2005
Messages
6
Programming Experience
Beginner
Dynamic Button where click closes form (RESOLVED)

Hi, I am trying to write a a windows app where the close button is created via a public class. The click event is created in here as well.
I can get the click event to process but I can not get the click event to close the form.

Please help!!

Code for class looks like

Public Class SetFormv

Public Sub Set_Form(ByVal in_frm As System.Windows.Forms.Form)

Dim btnClose As New Button

btnClose.Text = "Close"

btnClose.DialogResult = DialogResult.OK

AddHandler btnClose.Click, AddressOf BtnClose_Click

in_frm.Controls.Add(btnClose)

End Sub

Public Sub BtnClose_Click(ByVal sender As Object, ByVal e As EventArgs)

' This procedure handles events raised by the object Obj.
EndSub

EndClass













End Sub



End
Class

 
Last edited:
I will try to simplify ... within onLoad event add this code (it makes me more sense but it can be placed in any other event ... another button's click for example)

VB.NET:
Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] Form1_Load([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Object, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.EventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#0000ff]MyBase[/color][/size][size=2].Load
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] btnClose [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] Button
 
[/size][size=2][color=#0000ff]With[/color][/size][size=2] btnClose
 
.Text = "Close"
 
.Top = 100
 
.Left = 16
 
.Width = 256
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]With
 
[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls.Add(btnClose)
 
[/size][size=2][color=#0000ff]AddHandler[/color][/size][size=2] btnClose.Click, [/size][size=2][color=#0000ff]AddressOf[/color][/size][size=2] Button1_Click
 
btnClose.PerformClick()
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub

this is the button1 and its click event looks like

VB.NET:
Private[/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2][color=#000000] Button1_Click([/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] sender [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.Object, [/color][/size][size=2][color=#0000ff]ByVal[/color][/size][size=2][color=#000000] e [/color][/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#000000] System.EventArgs) [/color][/size][size=2][color=#0000ff]Handles[/color][/size][size=2][color=#000000] Button1.Click[/color]
 
MessageBox.Show("It should work this time !!!")
 
[/size][size=2][color=#0000ff]End[/color][/size][size=2][color=#0000ff]Sub



Regards ;)
 

Attachments

  • CreateButtonAndAddhandler.zip
    21.7 KB · Views: 35
Last edited:
Hi, Thanks for that.


But!!

what I am trying to do is have a seperate class (outside the form) that creates a close button on a form and has a click event that closes the form.
So I can have this close button is same position in eveny form with same click event.

I can get the click event to work (to show message) but I cant get the click event to close the form.

Here is the full code that I have in the class and this class is called from load event in form!!


NB: I have taken out the Handles btnClose.Click code that you supplied as when this was in the click event (messagebox) processed twice.


Public Class SetForm

Dim WithEvents btnClose As New Button

Public Sub Set_Form(ByVal in_frm As System.Windows.Forms.Form)

in_frm.Height = 768

in_frm.Width = 1024

in_frm.Location =
New Point(0, 0)

global.GVA_THSSCR = in_frm.ToString()



With btnClose

.Text = "Close"

.DialogResult = DialogResult.OK

.Size =
New Size(616, 104)

.Location =
New Point(208, 600)

.Font =
New Font(btnClose.Font.FontFamily.Name, 26)

.BackColor = System.Drawing.Color.LightCyan

End With

AddHandler btnClose.Click, AddressOf BtnClose_Click

in_frm.Controls.Add(btnClose)

'in_frm.Controls.Add(btnClose)

End Sub

Public Sub BtnClose_Click(ByVal sender As Object, ByVal e As EventArgs) MessageBox.Show("This is a checkbox") 'This is where I want to close the form!!

End Sub

End
Class

 
I think you should be using the advantages given by inheritance in this situation.
By that I mean you should create a form that has the button in the position you want that includes the click event handler for that button.
Then have the other forms inherit from that form using the Inherits statement.
Feel free to post more queries if they arise.
 
Well, well look who is back in town ...
radost.gif

Nice to see you again Paszt ... btw, you can make a class that casting sender and if sender is clicked current form is getting to close. Now you only need to make a new instance of your class and that's it i beleive (i will try to prepare something from the kitchen ... something fresh:D)

Regards ;)
 
actually i agree with Paszt make a "baseform" that has any logo's and buttons and stuff that you want on the other forms then in the solution explorer right-click the project and click Add -> Add Inherited Form -> give it a filename -> click open -> pick a form that's listed (this list of forms comes from the project) -> click ok

now you'll have a new form and you'll notice that the existing button can't be moved or anything because it was inherited

also keep in mind on the BaseForm for this you should give it a default form size that's big enough to fit everything that you'll be adding to the other form's that's inheriting this one
 
Hi, Sorry, it seems that I didn't understand your problem correctly
cupanje.gif


Well, I agree that inheritance would be better solution but anyway it can be resolved in another way also ... as it follows:
PHP:
Public Class myButton
	Private Sub performClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
		If TypeOf sender Is Button Then
			Dim myBut As Button = DirectCast(sender, Button)
			myBut.Parent.Visible = False 'just to improvise
		 'because the Parent property returns an object of type Control you cannot say simple mybut.parent.close
			'Control has not Close() method
		 'if you want to close the form just get type of myBut form 'myBut.Parent.GetType()
			'if it is a form you can CAST it!  and close it!
		End If
		MessageBox.Show("This is it?")
	End Sub

	Public Sub AddBtn(ByRef tb As Form, ByRef btn As Button, ByVal y As Integer)
		Dim i As Integer
		With btn
			.Size = New Size(320, 23)
			.Location = New Point(16, y)
			.Text = "This is button from class"
			.ForeColor = Color.Orange
		End With
		tb.Controls.Add(btn)
		AddHandler btn.Click, AddressOf performClick
	End Sub
End Class


btw, take a look at this article (get the attached proj btw) http://www.vbdotnetforums.com/showthread.php?t=3108

Maybe it will give you an idea how to adapt it to your needs ... all you need to do is to add Me.close statement into Sub ButtonClickEvent (maybe it is not happiest solution but hey we are improvising :D)

Regards ;)


edit: i have forgotten to explain usage of the code above ... say you have an existing button on current form ... just join this code to its click event

PHP:
Dim newButton As New myButton
Dim mybut As New Button
newButton.AddBtn(Me, mybut, 40)
'and don't forget ... you can control each property through arguments of the sub/s ... i.e. X position
 
Thanks to all

Thanks every one for you thoughts, I have now completed my simple task of closing the form from a seperate class.

I used the button.parent way and it worked a treat!!

Thanks

nzcam
 
Sorry but I have to say that that is a hack and you should do what Paszt and JB suggested. OOP works a certain way for a certain reason. When you start doing things the way you have chosen you get back to the bad old days where code is hard to understand, hard to maintain and hard to extend beyond the specific case it was originally designed for. Obviously you can do things however you see fit, but if I was your employer I would be unimpressed with that solution.
 
kulrom said:
Do not say the same thing twice, you're not a politician! JK :D
I also pointed out: "Well, I agree that inheritance would be better solution but anyway and so on ...."
I hope you noted that ... or maybe you didn't?


Regards ;)
I noted it, and I myself have been known to provide solutions to people that I don't especially like myself because they are determined to do things a certain way, which is their prerogative. I just wanted to stress the fact that I agree very strongly with the comments made by you and others that inheritance is the "correct" way to solve this problem. People sometimes think that they should just take the easy option, particularly in a simple situation. That is the best time to learn the more advanced techniques, though. Learn them now and you are already prepared for when things get more complex. If you wait until later then it will likely be more difficult. I recommend learning things when the opportunity arises, rather than waiting until necessity dictates it.
 
jmcilhinney said:
I myself have been known to provide solutions to people that I don't especially like myself because they are determined to do things a certain way
I meant I didn't like the solution, not the people. :)
 
inheritance of form

OK, Thanks to all of you for showing me the way!!

I'll change what I am doing to inheritance !!

If something is done then it should be done the correct way

Thanks!!
 
Wise indeed, and you'll be glad you did. You will be able to apply what you learn from this experience to many other situations in the future, so you will save yourself time and effort in the long run.

Welcome aboard, by the way. I just realised that the first post in this thread was your first in the forums.
 
Newbe

Yes indeed. This was my 1st post and it is great to see that you guys are also showing us the correct way of doing thinks rather than the way we ask for help!!

Have a happy day!!
 
Back
Top