kaos_king
New member
- Joined
- Jun 15, 2016
- Messages
- 4
- Programming Experience
- Beginner
Hi Guys,
I'm new to C# and I am re-doing a simple application that I did in VB in order to learn a little. This makes it easier as I already have the logic so I am essentially just converting each line. I'm sorry to register to ask this as I can normally just google and gather things I need but I am a little stumped on this (what I assume is an easy problem).
The application only has the MainForm and is hidden upon startup, it is then activated by pressing both the control keys. I did this in VB with the use of GetAsyncKeyState and a timer:
This has worked perfectly for over a year - although I'm not sure if that's the best way of achieving what I want but "it worked for me"
Now it comes to C# and I'm taking the same approach, this is the code I am testing with just trying to get the form to show/hide and it won't work from the timer. However a button on the form has no issue hiding it so I was thinking maybe its a threading problem, but if that's the case, why would it work in VB?
The commented out MessageBox's work fine along with the capturedKeys bool however I cannot get the form to show/hide from this timer. I'm afraid to ask but....what am I missing?
I'm new to C# and I am re-doing a simple application that I did in VB in order to learn a little. This makes it easier as I already have the logic so I am essentially just converting each line. I'm sorry to register to ask this as I can normally just google and gather things I need but I am a little stumped on this (what I assume is an easy problem).
The application only has the MainForm and is hidden upon startup, it is then activated by pressing both the control keys. I did this in VB with the use of GetAsyncKeyState and a timer:
C#:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim LCtrlKey, RCtrlKey, EscKey As Boolean
LCtrlKey = GetAsyncKeyState(Keys.LControlKey)
RCtrlKey = GetAsyncKeyState(Keys.RControlKey)
EscKey = GetAsyncKeyState(Keys.Escape)
If CapturedKeys = False Then
If LCtrlKey And RCtrlKey = True Then
CapturedKeys = True
Me.Show()
End If
Else
If EscKey = True Then
btnClose_Click(sender, e)
End If
End If
End Sub
This has worked perfectly for over a year - although I'm not sure if that's the best way of achieving what I want but "it worked for me"
Now it comes to C# and I'm taking the same approach, this is the code I am testing with just trying to get the form to show/hide and it won't work from the timer. However a button on the form has no issue hiding it so I was thinking maybe its a threading problem, but if that's the case, why would it work in VB?
C#:
[COLOR=#0000ff][B]private[/B][/COLOR] [COLOR=#ff0000]void[/COLOR] [COLOR=#191970][B]OnTimedEvent[/B][/COLOR]([COLOR=#ff0000]object[/COLOR] source, [COLOR=#004085]ElapsedEventArgs[/COLOR] e)
{
[COLOR=#008000]// If both control keys are pressed at the same time, show the form[/COLOR]
[COLOR=#0000ff][B]if[/B][/COLOR] ([COLOR=#191970][B]GetAsyncKeyState[/B][/COLOR]([COLOR=#004085]Convert[/COLOR].[COLOR=#191970][B]ToInt16[/B][/COLOR]([COLOR=#004085][B]Keys[/B][/COLOR].[I]LControlKey[/I])) != [COLOR=#00008b]0[/COLOR] && [COLOR=#191970][B]GetAsyncKeyState[/B][/COLOR]([COLOR=#004085]Convert[/COLOR].[COLOR=#191970][B]ToInt16[/B][/COLOR]([COLOR=#004085][B]Keys[/B][/COLOR].[I]RControlKey[/I])) != [COLOR=#00008b]0[/COLOR]) {
[COLOR=#008000]// MessageBox.Show(String.Format("2x ctrl: {0}", capturedKeys));[/COLOR]
c[I]apturedKeys[/I] = [COLOR=#008b8b][B]true[/B][/COLOR];
[COLOR=#8b0000]aForm[/COLOR].[COLOR=#8b0000]Show[/COLOR]();
}
[COLOR=#008000]// Or if Esc is pressed, hide the form[/COLOR]
[COLOR=#0000ff][B]if[/B][/COLOR] ([COLOR=#191970][B]GetAsyncKeyState[/B][/COLOR]([COLOR=#004085]Convert[/COLOR].[COLOR=#191970][B]ToInt16[/B][/COLOR]([COLOR=#004085][B]Keys[/B][/COLOR].[I]Escape[/I])) != [COLOR=#00008b]0[/COLOR]) {
[COLOR=#008000]// MessageBox.Show(String.Format("Esc: {0}", capturedKeys));[/COLOR]
[I]capturedKeys[/I] = [COLOR=#008b8b][B]false[/B][/COLOR];
[COLOR=#8b0000]aForm[/COLOR].[COLOR=#8b0000]Hide[/COLOR]();
}
}
The commented out MessageBox's work fine along with the capturedKeys bool however I cannot get the form to show/hide from this timer. I'm afraid to ask but....what am I missing?