Silverlight 5 Events - vb.net

I am written in Silverlight 5 with VB.Net. I have a number of child windows. I want to catch the event when the user closes this window without clicking on any button. Please give me an example if possible. I prefer VB but I can translate C#.
Bob

You can subscribe to the Closing or Closed Events of you ChildWindows, in the Closing event you can check the DialogResult to see if it is True, if so let the Window Close otherwise Cancel the closing of the ChildWindow by setting e.Cancel to True. The example that I have here shows both events and will allow you to stop the ChildWindows Close.
Partial Public Class MainPage
Inherits UserControl
Dim child As ChildWindow1
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
child = New ChildWindow1
AddHandler child.Closed, AddressOf ChildClosed
AddHandler child.Closing, AddressOf ChildClosing
child.Show()
End Sub
Private Sub ChildClosed(sender As Object, e As EventArgs)
Dim result As Boolean? = CType(sender, ChildWindow).DialogResult
If IsNothing(result) Then
'Do something if DialogResult is Nothing You can not cancel close with this event
ElseIf Not result Then
'Do what you want when the Cancel Button is clicked
ElseIf result Then
'Do what you want when the Ok Button is clicked
End If
End Sub
Private Sub ChildClosing(sender As Object, e As ComponentModel.CancelEventArgs)
Dim result As Boolean? = CType(sender, ChildWindow).DialogResult
If IsNothing(result) Then
e.Cancel = True 'This will cancel the ChildWindows Close and leave it open
ElseIf Not result Then
'Do what you want when the Cancel Button is clicked
ElseIf result Then
'Do what you want when the Ok Button is clicked
End If
End Sub
End Class

Related

Sub To Summarize Click Events

I have a windows form app where a user clicks a menu item to open a form. Once the menu item is clicked, it checks whether form is open and notifies user if form is open else it opens the form. The code for this is as below
Private Sub UsersBarButtonItem_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles UsersBarButtonItem.ItemClick
If Application.OpenForms().OfType(Of Users).Any Then
MessageBox.Show("Form Is Open", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
For Each openform In Application.OpenForms()
If TypeOf (openform) Is Users Then CType(openform, Users).BringToFront()
Next
Else
Dim Form As New Users
Form.MdiParent = Me
Form.Show()
End If
End Sub
I would like a subroutine to help me avoid repetiting this code for each menu item click events.
Ok, with a bit of help from This thread, you could use reflection/linq to create a new instance of the passed form from its' .Name string If you find a better way, I would be interested to learn myself.
Private Sub ButtonFormB_Click(sender As Object, e As EventArgs) Handles ButtonFormB.Click
CheckForm(FormB)
End Sub
Private Sub CheckForm(Frm As Form)
For Each OpnForm As Form In Application.OpenForms
If OpnForm Is Frm Then
OpnForm.BringToFront()
Exit Sub
End If
Next
Dim formType =
Reflection.Assembly.GetExecutingAssembly().GetTypes().Where(Function(a) a.BaseType = GetType(Form) AndAlso a.Name = Frm.Name).FirstOrDefault()
If formType IsNot Nothing Then
Dim NewForm As Form = CType(Activator.CreateInstance(formType), Form)
NewForm.Show()
End If
End Sub

SelectionChanged event occurs more times than it should

I have grid and those grid is populate on Form's load event. At the end line of that event i am hooking method handler for my SelectionChanged event of this grid. I want to get current selected row's zero cell's 1 value. Unfortunately when i run program my SelectionChanged event method handler is called infinite times... And i have no idea why is that.
So its basically like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
some code which populating data to grid...
'here hooking up method after data is there already to not fire it up during grid population
AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
End Sub
and this is event handler method itself:
Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs)
RemoveHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
ItemPanelImgs.Items.Clear()
'Dim images As New List(Of Article_Image)
Dim selectedNummer As String = String.Empty
selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()
'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
'ItemPanelImgs.DataSource = images
End If
AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
End Sub
P.S I am using to be concrete supergrid control from DotnetBar devcomponenets but it shouldn't be diffrent from ordinary controls behaviour.
What could be wrong here?
For those whom would like to debug here is sample app
EDIT:
I also tried this way but its still going to infinitive loop...
Public IgnoreSelectionChanged As Boolean = False
Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs) Handles gridArtikels.SelectionChanged
If IgnoreSelectionChanged Then Exit Sub
IgnoreSelectionChanged = True
If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
ItemPanelImgs.Items.Clear()
'Dim images As New List(Of Article_Image)
Dim selectedNummer As String = String.Empty
selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()
'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
'ItemPanelImgs.DataSource = images
End If
IgnoreSelectionChanged = False
End Sub

How can I use the .Show() method in such a way the user can't click other forms already opened in background?

Now i'm using a'jobdone' flag and the following behaviour (that it looks quite horrible to me...):
Dim NewLoginForm As New LoginClass
LoginClass.jobdone = False
NewLoginForm.Show()
While (LoginClass.jobdone = False)
Application.DoEvents()
End While
NewLoginForm.Close()
If I try to use ShowDialog() the behaviour is better but it's a problem to manage all the opening and closing windows (if the forms are many) and I notice that all the background already opened forms close themselves either if one ShowDialog() form is closed...
Thanks
Ran a quick test and this worked perfectly:
Public Sub ForceOpen(ByRef frm As Form)
frm.Show()
For Each otherForm As Form In Application.OpenForms
If otherForm Is frm Then
AddHandler frm.FormClosing, AddressOf BlockFormClosing
Else
otherForm.Enabled = False
End If
Next
End Sub
Public Sub BlockFormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs)
e.Cancel = True
End Sub
Public Sub EnableOpenForms()
For Each frm As Form In Application.OpenForms
frm.Enabled = True
RemoveHandler frm.FormClosing, AddressOf BlockFormClosing
Next
End Sub
The calling form will open the stay-open form with ForceOpen(FormStayOpen). When the stay-open form's conditions for allowing the user to close it have been satisfied, have it call EnableOpenForms().

SerialPort and Control Updating in MDI form

As my title implies i have the following problem, i am receiving data from serial port and i update a richtextbox in a MDI Form with the control.invoke method
(Code in SerialPort.DataReceived Event)
If myTerminal.Visible Then
myTerminal.MyRichTextBox1.Invoke(New MethodInvoker(Sub()
myTerminal.MyRichTextBox1.AppendText(dataLine & vbCrLf)
End Sub))
End If
But as a mdi form it has the ability to close and reopen. So when the serialport is sending data to richtextbox and the user click the close button and the form gets disposed. Then the error "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."... Any Idea????
My regards,
Ribben
That code is not in the SerialPort.DataReceived event it is in the event handler. (Yes, I'm nitpicking, but it points to a solution.) The best thing to do is have the form that owns myTerminal add the handler when it is created and remove the handler when it closes.
Thank you for your answer but unfortunately that's not the solution. First of all my SerialPort Class must inform 2 Forms (Form with richtextbox, Form with Listview) and another class which is responsible for drawing (Unmanaged Directx 9.0c about 4 Forms), so to implement right the serialport class i have made my own events. Again to the problme, it caused because the Serialport.DataReceived everytime it occurs creates a thread in the threadpool and when i dispose the form simply it's too slow to catch up with all the threads and so there is at least one thread which invokes the control which is already disposed!
As a temp solution i came up with (The Below code is in the TerminalForm Class which inherits Form):
Private VisibleBoolean As Boolean = False
Private Index As Integer = 0
Private Sub DataToAppend(ByVal _text As String)
If VisibleBoolean Then
Me.MyRichTextBox1.Invoke(New MethodInvoker(Sub()
Me.MyRichTextBox1.AppendText(_text & vbCrLf)
End Sub))
ElseIf Index = 1 Then
Index = 0
myDispose()
RemoveHandler myserialport.DataToSend2, AddressOf DataToAppend
End If
End Sub
Private Sub Me_Activated(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Activated
VisibleBoolean = True
AddHandler myserialport.DataToSend2, AddressOf DataToAppend
End Sub
Private Sub myDispose()
If Index = 0 And Not Me.IsDisposed Then
Me.Invoke(New MethodInvoker(Sub()
MyBase.Dispose(True)
End Sub))
End If
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
End Sub
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
Index = 1
VisibleBoolean = False
End Sub
I know i don't like either but at least it's working!
Anyother improvement or suggestion is more

vb.net how do I make an application that only has a notifyicon and no windows form?

I want to make an application that only has a notifyicon and doesn't have any visible window form when it starts up. I see some example sort of like what I want to do for c#, but I don't see how to do this in vb.net project.
A form is not strictly necessary. You can instantiate a NotifyIcon and use that without creating a form:
Public Class AppContext
Inherits ApplicationContext
Private notifyIcon As NotifyIcon
Private appActive As Boolean
Public Sub New()
AddHandler Application.ApplicationExit, AddressOf OnApplicationExit
notifyIcon = New NotifyIcon()
notifyIcon.Icon = My.Resources.ActiveIcon
notifyIcon.Text = "The app is active."
AddHandler notifyIcon.MouseClick, AddressOf OnIconMouseClick
appActive = True
notifyIcon.Visible = True
End Sub
Private Sub OnApplicationExit(ByVal sender As Object, ByVal e As EventArgs)
If notifyIcon IsNot Nothing Then
notifyIcon.Dispose()
End If
End Sub
Private Sub OnIconMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
If e.Button = MouseButtons.Left Then
appActive = Not appActive
notifyIcon.Icon = If(appActive, My.Resources.ActiveIcon, My.Resources.InactiveIcon)
notifyIcon.Text = If(appActive, "The app is active.", "The app is not active.")
Else
If MsgBox("Do you want to Exit?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
notifyIcon.Visible = False
ExitThread()
End If
End If
End Sub
End Class
And then start your app from a Sub Main:
Public Module EntryPoint
Public Sub Main()
Dim ctx As New AppContext()
Application.Run(ctx)
End Sub
End Module
Just put the form transparent and resize it to 1x1..
And add a notifyicon..
And on the Form Load Event do this:
NotifyIcon.Visible = True
Then make what ever you want..
You can create a context menu strip (A Menu when you right click on it )
PS: If you do it, You need to go on the NotifyIcon properties and set the Context Menu Strip to that you created..
Hope it helped you..