I'm builing an app for windows iot core on a raspberry pi 3.
It is a scoreboard fot playing billiard.
The app has to work with a numpad only.
The problem is, I want to navigate between pages when the user presses the enter key, but after a couple of times de enter key doesn't work anymore.
I have made a simple code to let you see what i mean.
mainpage and onother two pages. This is the testcode.
The mainpage
Imports Windows.UI.Core
Public NotInheritable Class MainPage
Inherits Page
Public Sub New()
Me.InitializeComponent()
AddHandler Window.Current.CoreWindow.KeyUp, AddressOf CoreWindow_KeyUp
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub
Private Sub CoreWindow_KeyUp(sender As CoreWindow, e As KeyEventArgs)
Dim Value As String = e.VirtualKey
'Enter
If Value = "13" Then
GotoNext()
End If
End Sub
Private Sub GotoNext()
Frame.Navigate(GetType(BlankPage1))
End Sub
End Class
Page 1
Imports Windows.UI.Core
Public NotInheritable Class BlankPage1
Inherits Page
Public Sub New()
Me.InitializeComponent()
AddHandler Window.Current.CoreWindow.KeyUp, AddressOf CoreWindow_KeyUp
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub
Private Sub CoreWindow_KeyUp(sender As CoreWindow, e As KeyEventArgs)
Dim Value As String = e.VirtualKey
'Enter button
If Value = "13" Then
GotoNext()
End If
End Sub
Private Sub GotoNext()
Frame.Navigate(GetType(Blankpage2))
End Sub
End Class
Page 2
Imports Windows.UI.Core
Public NotInheritable Class BlankPage2
Inherits Page
Public Sub New()
Me.InitializeComponent()
AddHandler Window.Current.CoreWindow.KeyUp, AddressOf CoreWindow_KeyUp
End Sub
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
End Sub
Private Sub CoreWindow_KeyUp(sender As CoreWindow, e As KeyEventArgs)
'we halen de virtuele waarde van de ingedrukte knop op
Dim Value As String = e.VirtualKey
'Enter knop
If Value = "13" Then
GotoNext()
End If
End Sub
Private Sub GotoNext()
Frame.Navigate(GetType(MainPage))
End Sub
End Class
Strangely when you hit enter 9 times it stops and I don't know why.
Related
Main sub gives error:
'Public Shared Sub Main()' has multiple definitions with identical signatures.
Nowhere I can use usage of main sub.
Imports System.ServiceProcess
Public Class MetricArchiver
Inherits System.ServiceProcess.ServiceBase
Public WithEvents Arc As Archiver
Public WithEvents Timer1 As System.Timers.Timer
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Timer1 = New System.Timers.Timer
Timer1.Interval = 30000
Timer1.Enabled = False
End Sub
Public Shared Sub Main() << This gives error.
System.ServiceProcess.ServiceBase.Run(New MetricArchiver)
End Sub
Protected Overrides Sub OnStart(ByVal args() As String)
Arc = New Archiver()
Arc.Run()
Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop()
Arc.Halt()
Timer1.Enabled = False
End Sub
Private Sub Timer1_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles Timer1.Elapsed
End Sub
End Class
I read various posts, and made a practice project, but it does not works.
The form have a button and a text box with a default text 'Updated 0 times'. On button click starts the timer and each time update the text with the number of times the text was updated.
The exception of cross thread calls is not thrown, but when calling the text box, its .Text = "", the text is updated but not the text box on the form. And InvokeRequired is always false.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Here the textBox.Text = "Updated 0 times."
Dim checking_text As String = Me.TextBox1.Text
TimerTest.StartTimer()
End Sub
Delegate Sub UpdateTextInvoke(ByVal new_text As String)
Public Sub UpdateText(ByVal new_text As String)
'Here the textBox.Text = ""
Dim txtB As TextBox = Me.TextBox1
'InvokeRequired always = False.
If txtB.InvokeRequired Then
Dim invk As New UpdateTextInvoke(AddressOf UpdateText)
txtB.Invoke(invk, New Object() {new_text})
Else
'The value of this text box is updated, but the text on the form TextBox1 never changes
txtB.Text = new_text
End If
End Sub
End Class
Public Class TimerTest
Private Shared tmr As New System.Timers.Timer
Private Shared counter As Integer
Public Shared Sub StartTimer()
tmr.Interval = 5000
AddHandler tmr.Elapsed, AddressOf UdpateText
tmr.Enabled = True
End Sub
Public Shared Sub UdpateText(ByVal sender As Object, ByVal e As System.EventArgs)
counter += 1
Form1.UpdateText(String.Format("Updated {0} time(s).", counter))
End Sub
End Class
SOLVED
In the Class TimerTest added this code 'Private Shared myform As Form1 = Form1'
then changed 'Form1.UpdateText' To 'myform.UpdateText'
As indicated in the comments, you are using the default form instance feature of VB.Net. You could pass an instance of the form to the TimerTest class, and replace the reference to Form1 with the instance.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim checking_text As String = Me.TextBox1.Text
TimerTest.StartTimer(Me)
End Sub
Public Sub UpdateText(new_text As String)
If TextBox1.InvokeRequired Then
Dim invk As New Action(Of String)(AddressOf UpdateText)
TextBox1.Invoke(invk, {new_text})
Else
TextBox1.Text = new_text
End If
End Sub
End Class
Public Class TimerTest
Private Shared tmr As New System.Timers.Timer()
Private Shared counter As Integer
Private Shared instance As Form1
Public Shared Sub StartTimer(formInstance As Form1)
instance = formInstance
tmr.Interval = 5000
AddHandler tmr.Elapsed, AddressOf UdpateText
tmr.Enabled = True
End Sub
Public Shared Sub UdpateText(ByVal sender As Object, ByVal e As System.EventArgs)
counter += 1
instance.UpdateText(String.Format("Updated {0} time(s).", counter))
End Sub
End Class
I've created a simple Windows Phone 8.1 app in Visual Studio 2013 and added 2 Basic Pages, along with some code to navigate to the second screen when a button on the first screen is clicked. Going to the second screen works fine, but when the hardware Back button is pressed, the app simply closes. I want the app to go back to the first page when the Back button is pressed.
I have followed the instructions in NavigationHelper.vb and added a Sub New() with some initialization commands, but the Back button still does not work. How can I get pressing Back to work correctly? Thanks in advance.
Here is my code:
BasicPage1.xaml.vb:
Imports App1.Common
Public NotInheritable Class BasicPage1
Inherits Page
Private WithEvents _navigationHelper As New NavigationHelper(Me)
Private ReadOnly _defaultViewModel As New ObservableDictionary()
Public ReadOnly Property NavigationHelper As NavigationHelper
Get
Return _navigationHelper
End Get
End Property
Sub New()
InitializeComponent()
Me._navigationHelper = New Common.NavigationHelper(Me)
AddHandler Me._navigationHelper.LoadState, AddressOf NavigationHelper_LoadState
AddHandler Me._navigationHelper.SaveState, AddressOf NavigationHelper_SaveState
End Sub
Public ReadOnly Property DefaultViewModel As ObservableDictionary
Get
Return _defaultViewModel
End Get
End Property
Private Sub NavigationHelper_LoadState(sender As Object, e As LoadStateEventArgs) Handles _navigationHelper.LoadState
End Sub
Private Sub NavigationHelper_SaveState(sender As Object, e As SaveStateEventArgs) Handles _navigationHelper.SaveState
End Sub
#Region "NavigationHelper registration"
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
_navigationHelper.OnNavigatedTo(e)
End Sub
Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
_navigationHelper.OnNavigatedFrom(e)
End Sub
#End Region
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles Button.Click
Frame.Navigate(GetType(BasicPage2))
End Sub
End Class
BasicPage2.xaml.vb:
Imports App1.Common
Public NotInheritable Class BasicPage2
Inherits Page
Private WithEvents _navigationHelper As New NavigationHelper(Me)
Private ReadOnly _defaultViewModel As New ObservableDictionary()
Public ReadOnly Property NavigationHelper As NavigationHelper
Get
Return _navigationHelper
End Get
End Property
Sub New()
InitializeComponent()
Me._navigationHelper = New Common.NavigationHelper(Me)
AddHandler Me._navigationHelper.LoadState, AddressOf NavigationHelper_LoadState
AddHandler Me._navigationHelper.SaveState, AddressOf NavigationHelper_SaveState
End Sub
Public ReadOnly Property DefaultViewModel As ObservableDictionary
Get
Return _defaultViewModel
End Get
End Property
Private Sub NavigationHelper_LoadState(sender As Object, e As LoadStateEventArgs) Handles _navigationHelper.LoadState
End Sub
Private Sub NavigationHelper_SaveState(sender As Object, e As SaveStateEventArgs) Handles _navigationHelper.SaveState
End Sub
#Region "NavigationHelper registration"
Protected Overrides Sub OnNavigatedTo(e As NavigationEventArgs)
_navigationHelper.OnNavigatedTo(e)
End Sub
Protected Overrides Sub OnNavigatedFrom(e As NavigationEventArgs)
_navigationHelper.OnNavigatedFrom(e)
End Sub
#End Region
End Class
The full solution is here: https://github.com/OrangeFlash81/SimpleWPApp
I fixed the problem myself by adding an event handler using AddHandler and using Frame.Navigate to go back to the first page. I added this code:
Sub New()
AddHandler HardwareButtons.BackPressed, AddressOf BackPressed
End Sub
Sub BackPressed(sender As Object, e As BackPressedEventArgs)
e.Handled = True
Frame.Navigate(GetType(BasicPage1))
End Sub
I'm trying to understand how I can expose an event in this situation:
Class C has an event E
Class B instance an object of class C
Class A instance an object of class B
When the event is raised from class C, is possible to manage it directly from class A?
Public Class C
Public Event E()
Public Sub Function_of_C()
RaiseEvent E()
End Sub
End Class
Public Class B
Public WithEvents m_C as new C
End Class
Public Class A
Public WithEvents m_B as new B
Private Sub Function_of_A() Handles m_B.m_C.E '<-- Doesn't work
'Do something
End Sub
End Class
As explained in the comment to your question. You need to bubble up the event in class B when it receives from C.
Sub Main
Dim test = new A()
End Sub
Public Class C
Public Event E()
Public Sub Function_of_C()
RaiseEvent E()
End Sub
End Class
Public Class B
Public WithEvents m_C as new C
Public Event E()
Public Sub FnB() Handles m_C.E
Console.WriteLine("In B received Event from C")
RaiseEvent E()
End Sub
Public Sub TriggerC()
Console.WriteLine("In B TriggerC, call C.Function_Of_C")
m_C.Function_Of_C()
End Sub
End Class
Public Class A
Public WithEvents m_B as new B
Private Sub Function_of_A() Handles m_B.E
Console.WriteLine("In A received Event from B")
End Sub
public Sub New()
Console.WriteLine("In A constructor, call B.TriggerC")
m_b.TriggerC()
End Sub
End Class
Just as a supplement to Steve's answer, here's an example of the common design pattern to forward events:
Public Class Doorbell
Public Event Ding As EventHandler
Protected Overridable Sub OnDing(e As EventArgs)
RaiseEvent Ding(Me, e)
End Sub
Friend Sub RaiseDing()
Me.OnDing(EventArgs.Empty)
End Sub
End Class
Public Class House
Implements IDisposable
Public Event DoorbellDing As EventHandler
Public Property Doorbell() As Doorbell
Get
Return Me.m_doorbell
End Get
Set(value As Doorbell)
If (Not value Is Me.m_doorbell) Then
Me.UnookDoorbell()
Me.m_doorbell = value
Me.HookDoorbell()
End If
End Set
End Property
Public Sub Dispose() Implements IDisposable.Dispose
Me.UnookDoorbell()
End Sub
Private Sub HandleDoorbellDing(sender As Object, e As EventArgs)
Me.OnDoorbellDing(e)
End Sub
Private Sub OnDoorbellDing(e As EventArgs)
RaiseEvent DoorbellDing(Me, e)
End Sub
Private Sub HookDoorbell()
If (Not Me.m_doorbell Is Nothing) Then
AddHandler Me.m_doorbell.Ding, AddressOf Me.HandleDoorbellDing
End If
End Sub
Private Sub UnookDoorbell()
If (Not Me.m_doorbell Is Nothing) Then
RemoveHandler Me.m_doorbell.Ding, AddressOf Me.HandleDoorbellDing
End If
End Sub
Private m_doorbell As Doorbell
End Class
I have created an application.
The main form is "Form1".
I have declared a class in Form1 like this:
Public Class Form1
Private _MyClass As Class1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_MyClass = New Class1
End Sub
End Class
Class1 is declared like this:
Public Class Class1
Private f As Form2
Public Sub New()
f = New Form2
f.Show()
End Sub
End Class
When I click Button1 on the main form, _MyClass1 is created and Form2 is shown.
Now when I click Button1 for the second time, I expect _MyClass1 to destroyed and a new _MyClass1 to be created.
I expect the first Form2 to disappear because _MyClass1 is destroyed.
I think it only exists in _MyClass1, and since _MyClass1 is destroyed, Form2 should also be unloaded.
Instead, I suddenly have two Form2 windows open.
Where did I go wrong in my thinking?
Coming from VB6, I expect a form to be automatically unloaded if its hosting class is terminated. Isn't this so in VB.NET as well?
You Can use :
Public Class Form1
Private _MyClass As Class1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not _MyClass Is Nothing Then ' To check if Class is initialized, If so then Close the Class or form
_MyClass.close()
End If
_MyClass = New Class1
End Sub
End Class
Public Class Class1
Private f As Form2
Public Sub New()
f = New Form2
f.Text = "form2"
f.Show()
End Sub
Public Sub close()
f.Close()
Me.Finalize() ' Call Destructor
End Sub
End Class
You have a private form inside a private class inside a form. IDisposable is precisely for class (forms are classes) which create other disposable objects (CA will tell you when you have code that creates stuff that is not disposed of properly as with 2 out 3 of your matryoshka dolls):
Public Class Class1
Implements IDisposable
Private f As Form2
Public Sub New()
f = New Form2
f.Text = "form2"
f.Show()
End Sub
Private disposedValue As Boolean ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
If f IsNot Nothing Then
f.Close() ' or f.Dispose
End If
End If
End If
Me.disposedValue = True
End Sub
' VS generated code
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
one tweak to Form1.Designer.vb (CA will tell you about it):
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
_MyClass.Dispose() ' dispose of your toys
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
The button click:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' depending on what the point is, this will work:
If _MyClass Is Nothing Then
_MyClass = New Class1
End If
' if you really need it start anew:
' If _MyClass IsNot Nothing Then
' _MyClass.Dispose
' End If
' _MyClass = New Class1
End Sub
When you again click on button. It will not Destroy Previous object of class.
Every time when you click Button A new Instance or Object is created for class1.