I have an app that allows for text to be dragged-dropped into a text box. I also have a check box that allows the app to always be on top of all windows. My issues is when I drag text into the text box and I hit the enter key, it does not run the function unless I have the window in actual focus (by clicking on it).
My question is, how can I make sure when I drag text into the text box, it will make the window be the focus so when I hit the enter key, it will run my function?
Here is what I am trying with no luck:
Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles Me.DragEnter
Me.Focus()
End Sub
Private Sub Form1_DragOver(sender As Object, e As DragEventArgs) Handles Me.DragOver
Me.Focus()
End Sub
I am posting the code as it works fine for me, try it on new project:
Public Class Form1
Private Sub TextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
' Drop text and move cursor to end of drag-dropped text
TextBox1.Text = e.Data.GetData(DataFormats.Text)
TextBox1.SelectionStart = TextBox1.Text.Length + 1
TextBox1.Focus()
Me.Activate()
End Sub
End Class
and designer:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.AllowDrop = True
Me.TextBox1.Location = New System.Drawing.Point(30, 54)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(216, 125)
Me.TextBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TopMost = True
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class
Try to use Me.Activate() instead in DragEnter it should be enough
Related
I created a project one year ago and now I want to add background color to all the focused textboxes.
I know I can create the events for all textboxes but it will take a lot of time, and I know that I can create a custom control (textbox) but I don't prefer.
so can I add those events for all textboxes in my project?
Let's go through all the possibilities here.
Firstly, the usual thing to do with events is to create a distinct handler for each event of each control, e.g.
Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
'...
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
'...
End Sub
Private Sub TextBox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
'...
End Sub
Private Sub TextBox2_Leave(sender As Object, e As EventArgs) Handles TextBox2.Leave
'...
End Sub
If you're doing the same thing for the same event of each control though, you can condense that into a single event handler per event:
Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter, TextBox2.Enter
Dim tb = DirectCast(sender, TextBox)
'...
End Sub
Private Sub TextBoxws_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave, TextBox2.Leave
Dim tb = DirectCast(sender, TextBox)
'...
End Sub
The sender parameter refers to the object that raised the event, so you can access the appropriate TextBox with a cast. The objects whose event you handle don't have to be the same type and you can even handle multiple events with the one method, as long as the signatures are compatible:
Private Sub Controls_FocusChanged(sender As Object, e As EventArgs) Handles TextBox1.Enter,
TextBox2.Enter,
ComboBox1.Enter,
TextBox1.Leave,
TextBox2.Leave,
ComboBox1.Leave
Dim cntrl = DirectCast(sender, Control)
'The event is raised before the change happens so the control
'will have focus on Leave and will not have focus on Enter.
cntrl.BackColor = If(cntrl.Focused, SystemColors.Window, Color.Yellow)
End Sub
Note that the designer can help you do this. You can select multiple controls, open the Properties window, click the Events button and then double-click the desired event to generate a single event handler with the selected event for all selected controls in the Handles clause. You can then select that existing event handler in the drop-down list for another event for one or more controls to add then to the Handles clause too. You can edit the method name in the code window as appropriate. You can also write the method and the Handles clause yourself if you want to.
Secondly, to write less code in each form, you can put your event handler(s) in a module somewhere and then use the AddHandler statement to attach it to the events when a form loads:
Module CommonEventHandlers
Public Sub Controls_FocusChanged(sender As Object, e As EventArgs)
Dim cntrl = DirectCast(sender, Control)
'The control will have focus on Leave and will not have focus on Enter.
cntrl.BackColor = If(cntrl.Focused, SystemColors.Window, Color.Yellow)
End Sub
End Module
and:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each tb In Controls.OfType(Of TextBox)()
AddHandler tb.Enter, AddressOf CommonEventHandlers.Controls_FocusChanged
AddHandler tb.Leave, AddressOf CommonEventHandlers.Controls_FocusChanged
Next
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
For Each tb In Controls.OfType(Of TextBox)()
RemoveHandler tb.Enter, AddressOf CommonEventHandlers.Controls_FocusChanged
RemoveHandler tb.Leave, AddressOf CommonEventHandlers.Controls_FocusChanged
Next
End Sub
That will handle the events for all TextBoxes that were added directly to the form. If you want different controls and/or some are in child containers then you would need to adjust that accordingly. Note that the event handlers need to be removed when you're done.
Finally, the "proper" solution is to use a custom control. You create a custom control simply by adding a class to your project and then adding an Inherits line to that class. You then override the appropriate method for the event you would otherwise handle, e.g. OnEnter method for Enter event. The code you put in the method is basically the same as you would put in the event handler, except it refers to the current object rather than the sender:
Public Class TextBoxEx
Inherits TextBox
Private defaultBackColor As Color
''' <inheritdoc />
Protected Overrides Sub OnEnter(e As EventArgs)
defaultBackColor = BackColor
BackColor = Color.Yellow
MyBase.OnEnter(e)
End Sub
''' <inheritdoc />
Protected Overrides Sub OnLeave(e As EventArgs)
BackColor = defaultBackColor
MyBase.OnLeave(e)
End Sub
End Class
You can then just edit the designer code files of your existing forms to use that custom control instead of the standard TextBox, e.g. this:
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(0, 0)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 20)
Me.TextBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As TextBox
becomes this:
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Me.TextBox1 = New TextBoxEx()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(0, 0)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 20)
Me.TextBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As TextBoxEx
That's just two lines of code changed per control, which you can do with the Find & Replace functionality. Everything will look and work exactly as it did before, but your TextBoxes will automatically exhibit the new behaviour. Once you've built your project, the custom control will be added to the Toolbox, so you can add it to forms in the designer like any other control.
Note that, in order to access the designer code files, you need to select your project or an item within it in the Solution Explorer and click the Show All Files button. You can then expand the node for your form and open the designer code file.
I am interested in vb and am confused how to save the contents of a Picturebox as a png file when button1 is clicked. When I try to do it now, it produces the error I show below. I can't figure out how to fix this because I'm new to using visual basic. The debugger seemed to catch something but I can't make sense of it. I would be extremely pleased if someone would help me to fix this. Here is the code:
'*** Acknowlegements ***
'Ideas for this code came from the MicroSoft "Scribble" sample code,
'Christian Graus's excellent arcticle on a C++ code called "Doodle"
'and the MicroSoft website.
'By John Buettner
'26 July 2003
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form
'Namespace myPaint
Inherits System.Windows.Forms.Form ' Of course ;)
Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
'*** below I declare some values for an Alpha and other user selected variables
'these will be used as I expand this program for a higher level use.
Dim myAlpha As Integer = 100 ' declare a Alpha variable
Dim myUserColor As New Color() 'this is a color the user selects
Friend WithEvents Button1 As Button
Friend WithEvents SaveFileDialog1 As SaveFileDialog
Dim myPenWidth As Single = 5 'set pen width variable
'**************************************************************
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form))
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.GhostWhite
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(0, 33)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(696, 423)
Me.PictureBox1.TabIndex = 2
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Image = Global.Ubernote.My.Resources.Resources.fl
Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.Button1.Location = New System.Drawing.Point(12, 4)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 3
Me.Button1.Text = "Save"
Me.Button1.UseVisualStyleBackColor = True
'
'SaveFileDialog1
'
'
'Form
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(696, 456)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form"
Me.Text = "Draw"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
Catch
MsgBox("No way, Hose!")
End Try
End If
PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
' Here is where we do the actual painting
Try ' error trapping
myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
'change the value of myUserColor
myAlpha = 100 ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path! :)
Catch
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
SaveFileDialog1.ShowDialog()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Try
' Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, SaveFileDialog1.FileName)
' PictureBox1.Image.Save(FileToSaveAs, System.Drawing.Imaging.ImageFormat.Jpeg)
'PictureBox1.Image.Save(FileToSaveAs)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim path As String
Dim pic As Image
pic = PictureBox1.Image
SaveFileDialog1.ShowDialog()
pic.Save(SaveFileDialog1.FileName)
End Sub
try this
I throw in huge troubles with recreating a multproject solution into a single project solution where my program become unusable.
I have all code files saved and problem is with showing designer in IDE and errors connecting with that.
Situation:
All forms are subclassed with class called cls_transform which make form transparent while moving.
Public Class cls_transform
Inherits System.Windows.Forms.Form
Private _OpacityMove As Double = 0.5
Private _OpacityOriginal As Double = 1
Private Const WM_NCLBUTTONDOWN As Long = &HA1
Private Const WM_NCLBUTTONUP As Long = &HA0
Private Const WM_MOVING As Long = &H216
Private Const WM_SIZE As Long = &H5
Private Sub cls_transform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)
Static LButtonDown As Boolean
If CLng(m.Msg) = WM_NCLBUTTONDOWN Then
LButtonDown = True
ElseIf CLng(m.Msg) = WM_NCLBUTTONUP Then
LButtonDown = False
End If
If LButtonDown Then
If CLng(m.Msg) = WM_MOVING Then
If Me.Opacity <> _OpacityMove Then
_OpacityOriginal = Me.Opacity
Me.Opacity = _OpacityMove
End If
End If
ElseIf Not LButtonDown Then
If Me.Opacity <> _OpacityOriginal Then Me.Opacity = _OpacityOriginal
End If
MyBase.DefWndProc(m)
End Sub
Public Property OpacityMove() As Double
Get
Return _OpacityMove
End Get
Set(ByVal Value As Double)
_OpacityMove = Value
End Set
End Property
Private Sub InitializeComponent()
Me.SuspendLayout()
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Name = "cls_transform"
Me.ResumeLayout(False)
End Sub
End Class
This is how code of one empty form "frm_myForm" look like:
Public Class frm_myForm
Inherits cls_transform
Private Sub frm_myForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
And this is Designer code of that form:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frm_myForm
Inherits cls_transform
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'frm_myForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Name = "frm_myForm"
Me.Text = "frm_myForm"
Me.ResumeLayout(False)
End Sub
End Class
When I try to "View Designer" from IDE instead of to see a form I get white screen with error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: frm_myForm --- The base class 'noviprog.cls_transform' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
That happen with all forms which inherits from cls_transform.
When cls_transform was in separate library (separate project of same solution) which was referenced to actual project that works.
Is it possible to get this working when code files and that class would be in same project and how to get this working?
I am trying to implement a function on my parent form, that when the event fires, I want to perform actions on all of the child forms that are open. Because any given child form may or may not be open at a given time, I can't handle it directly from the event on the parent form: i.e., cant do the following as Child1 may not be initiated at the time:
--Parent Form--
Public Sub ParentEvent()
DoParentAction()
DoChild1Action()
DoChild2Action()
End Sub
Is there a way on each child page to listen for ParentEvent() to be fired? essentially, what I want to do is handle the ParentEvent() being fired, on the child page the same as if a button was clicked on the child page, something like this:
--Child1--
Public Sub ChildEvent() Handles ParentForm.DoParentAction()
DoChild1Action()
End Sub
This is easy to do, you just have to step around VB's WithEvents and Handles syntax to get at it.
Public Class ParentForm
Event OnDoSomething()
Private Sub DoSomething()
RaiseEvent OnDoSomething()
End Sub
End Class
and then
Public Class ChildForm
Public Sub New()
InitializeComponent()
AddHandler ParentForm.OnDoSomething, AddressOf DoSomething
End Sub
Private Sub DoSomething()
' do something
End Sub
Private Sub ChildForm_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
RemoveHandler ParentForm.OnDoSomething, AddressOf DoSomething
End Sub
End Class
It's important to always make sure the event handler is removed before disposing the child form (else you end up with a memory leak).
The above assumes you are using the VB default instance of ParentForm - if you're not, obviously you have to reference things accordingly. A better approach might be to make the parent an argument in the constructor like:
Public Sub New(ByVal parent as ParentForm)
InitializeComponent()
AddHandler parent.OnDoSomething, AddressOf DoSomething
End Sub
also, of course, modifying the RemoveHandler section as well (you'd need to keep a reference to the parent). Another option is to hook/unhook in the ParentChanged event if this is an MDI application.
The only other caveat is that you can't create any of the child forms in the constructor of the parent form since you end up with self-reference during construction.
Sure.
Add a public event to the parent form:
Public Event EventFired(ByVal timestamp As DateTime)
In each child form, add a handler:
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 1: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
When you create the child form, add a handler:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l_child1 = New ChildForm1()
AddHandler Me.EventFired, AddressOf l_child1.ParentEventFired
l_child1.Show(Me)
End Sub
You can use this approach whether you are using an MDI or simply free-floating windows.
FULL CODE
ParentForm Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ParentForm
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(166, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open Child 1"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 41)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(166, 23)
Me.Button2.TabIndex = 0
Me.Button2.Text = "Open Child 2"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(12, 231)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(166, 23)
Me.Button3.TabIndex = 0
Me.Button3.Text = "Fire Event"
Me.Button3.UseVisualStyleBackColor = True
'
'ParentForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "ParentForm"
Me.Text = "ParentForm"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
End Class
ParentForm Code Behind
Public Class ParentForm
Public Event EventFired(ByVal timestamp As DateTime)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l_child1 = New ChildForm1()
AddHandler Me.EventFired, AddressOf l_child1.ParentEventFired
l_child1.Show(Me)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim l_child2 = New ChildForm2()
AddHandler Me.EventFired, AddressOf l_child2.ParentEventFired
l_child2.Show(Me)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
RaiseEvent EventFired(DateTime.Now)
End Sub
End Class
ChildForm1 Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
'
'ChildForm1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label1)
Me.Name = "ChildForm1"
Me.Text = "ChildForm1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
ChildForm1 Code Behind
Public Class ChildForm1
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 1: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
End Class
ChildForm2 Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm2
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'ChildForm2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label1)
Me.Name = "ChildForm2"
Me.Text = "ChildForm2"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
ChildForm2 Code Behind
Public Class ChildForm2
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 2: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
End Class
The form is an About Us form so has nothing on it only a text box and a OK button.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Here is how I'm opening the form:
Private Sub AboutAppStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutAppStripMenuItem.Click
Dim formAbout As New FormAbout()
formAbout.Show()
End Sub
Why won't the button close the form? I'm puzzled, I tried another button just in case with the same result.
UPDATE: I set a break point on Me.Close() and it isn't reaching it when I click the button, I created a new button and the same thing happened.
Thanks
I am betting the event handler for the button1_click event has been inadvertently removed.
Try double-clicking on the button in design time and see if it pulls you back to that same exact piece of code - or a new event handler definition.
If it's a new event handler definition - copy your code there and delete the first one.
There are other ways to manually add the event handler in the designer's code-behind - but maybe that's for a later progression.
From within VS click the "Show all files" button in solutions explorer. Grab us the code in .Designer.vb and paste it in here and we'll nail it down for you definitively.
Here's mine:
Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
_
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
_
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(131, 91)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(133, 50)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
From MSDN:
Showing the control is equivalent to setting the Visible property to true. After the Show method is called, the Visible property returns a value of true until the Hide method is called.
when formabout is open
click on pause(break all) button in visual studio
click on step into in debug in visual studio
click on the close button in formabout
you will see which code is executed, if any
* edit *
another question
is formabout.enabled property is true?
I tested the following
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim f As New Form2
f.Show()
End Sub
End Class
Public Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Me.Close()
End Sub
End Class
and did not have problems. As was suggested, re-create your button and code.
Add you Button Dynamically may solve the problem.
Place the following code in the load event of about Form.
Public Sub FormAbout_Load(ByVal sender as object,ByVal e as System.EventArgs)Handles Me.Load
Dim btn as new Button()
AddHandler btn.Click ,AddressOf _ClickToClose
End Sub
Private Sub _ClickToClose(ByVal sender as object,ByVal e as System.EventArgs)
Me.Close()
End Sub
Simple.
Select Project Properties from Solution Explorer.
Select Security tab to either uncheck "Enable ClickOnce..." or select "This is a full trust application".
Save the properties settings.
Solved.