Cant change active textbox and set focus after mouse click on it - vba

After second click on textboxes i cant write nothing after click on button "1". Setfocus is working only at first click (img).
Can You help?
Greetings
Private activeTextbox As Control
Private Sub Textbox_sr_Enter()
Set activeTextbox = Controls("Textbox_sr")
End Sub
Private Sub CommandButton1_Click()
If Not activeTextbox Is Nothing Then
activeTextbox.Text = activeTextbox.Text & "1"
activeTextbox.SetFocus
End If
End Sub
Private Sub TextBox1_Enter()
Set activeTextbox = Controls("TextBox1")
End Sub
Private Sub TextBox2_Enter()
Set activeTextbox = Controls("TextBox2")
End Sub
Private Sub TextBox3_Enter()
Set activeTextbox = Controls("TextBox3")
End Sub

Related

Array in command Button VBA

I'm doing a code where, when i click button 1, it will load the array form the range B8:C17, and when I click button 2, it should print the array in range E8:F17.
Private Sub CommandButton1_Click()
arr = Range("B8:C17")
Range("B8:C17") = Clear
End Sub
Private Sub CommandButton2_Click()
Range("E8:F17") = arr
End Sub
Private Sub UserForm_Click()
Dim arr As Variant
End Sub
The button 1 works fine but the button 2 does not. What did I do wrong here ?
In your code = Clear will yield an error, it should be used as method like this:
Option Explicit
Public arr
Private Sub CommandButton1_Click()
arr = Range("B8:C17")
Range("B8:C17").Clear
End Sub
Private Sub CommandButton2_Click()
Range("E8:F17") = arr
End Sub
'Private Sub UserForm_Click()
'
'End Sub

Navigation between pages stops working

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.

Checklistbox Multiple Selection

I have been working on a application that uses a Checkedlistbox so I can allow the user to select multiple boxes.
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each item As Object In Me.CheckedListBox1.CheckedItems
Dim text As String = Me.CheckedListBox1.GetItemText(item)
Next
If text = "Line 1" Then
CreateLine1()
End If
If text = "Line 2" Then
CreateLine2()
End If
If text = "Line 3" Then
CreateLine3()
End If
If Text = "Line 4" Then
CreateLine4()
End If
If Text = "Line 5" Then
CreateLine5()
End If
It goes all the way to "Line 10". When the app runs it use cmd.exe to connect to telnet and send commands. If I have Line 1 and Line 2 selected Line 1 has no problems, but when Line 2 run it opens a cmd does, nothing for a few seconds, open another cmd, and run just the commands while not connected to the telnet. Several more widows open afterwords and the four or fifth window connected to telnet.
How can I make it so if one line if selected after it has run telnet it separates out that line as "Has been ran" before going to the next line to avoid my problem.
Addition info:
This app has a select-all and deselect-all buttons so I can not have anything that will interfere with them.
I have try using socket to replace cmd.exe.....it did not go so well and I will pass on it.
Each sub the lines go to it basically the same except to the IP address and a few commands.
I hope the original code you posted isn't actually what you're using...it doesn't seem quite right.
Perhaps something like this might be more useful:
Imports System.Reflection
Public Class Form1
Private Methods As New List(Of MethodInfo)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim MI As MethodInfo
For i As Integer = 1 To 10
mi = Me.GetType.GetMethod("CreateLine" & i, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
If Not IsNothing(MI) Then
Methods.Add(MI)
End If
Next
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
For Each Index As Integer In Me.CheckedListBox1.CheckedIndices
Methods(Index).Invoke(Me, Nothing)
Next
End Sub
Private Sub CreateLine1()
Debug.Print("CreateLine1()")
End Sub
Private Sub CreateLine2()
Debug.Print("CreateLine2()")
End Sub
Private Sub CreateLine3()
Debug.Print("CreateLine3()")
End Sub
Private Sub CreateLine4()
Debug.Print("CreateLine4()")
End Sub
Private Sub CreateLine5()
Debug.Print("CreateLine5()")
End Sub
Private Sub CreateLine6()
Debug.Print("CreateLine6()")
End Sub
Private Sub CreateLine7()
Debug.Print("CreateLine7()")
End Sub
Private Sub CreateLine8()
Debug.Print("CreateLine8()")
End Sub
Private Sub CreateLine9()
Debug.Print("CreateLine9()")
End Sub
Private Sub CreateLine10()
Debug.Print("CreateLine10()")
End Sub
End Class
There are lots of other ways to do this as well...

Iterate Through Devexpres TextEdit Controls in VB.NET

Could someone help iterating through DevExpress TextEdit controls within an XTRAFORM in vb.net?
What I am actually trying to do is to intercept any value changes at FormClosing event by using EditValue and OldEditValue properties.
I meight need to tell that my controls are contained in XtraTab and XtraPanel Containers.
the following is what I tried:
Public Function TextEditChangesOccured(frm As XtraForm) As Boolean
Dim result As Boolean
For Each ctrl As BaseEdit In frm.Controls
If TypeOf ctrl Is TextEdit Then
If ctrl.EditValue <> ctrl.OldEditValue Then
result = True
Else
result = False
End If
End If
Next
Return result
End Function
Private Sub MyXtraForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If TextEditChangesOccured(Me) Then
DevExpress.XtraEditors.XtraMessageBox.Show("Changes have occured!", My.Application.Info.AssemblyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
but it says unable to cast XtraTab control to TextEdit control.
Your help will be much appreciated.
To make your code works just change your code snippet as follows:
Public Function TextEditChangesOccured(container As Control) As Boolean
Dim result As Boolean
For Each ctrl As Control In container.Controls
Dim bEdit As BaseEdit = TryCast(ctrl, BaseEdit)
If bEdit IsNot Nothing Then
Dim tEdit As TextEdit = TryCast(ctrl, TextEdit)
If tEdit IsNot Nothing Then
result = result Or (bEdit.EditValue <> bEdit.OldEditValue)
End If
Else
result = result Or TextEditChangesOccured(ctrl)
End If
Next
Return result
End Function
To detect changes for all the editors within a Form use the following approach:
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
SubscribeTextEditValueChanged(Me)
End Sub
Private Sub SubscribeTextEditValueChanged(ByVal container As Control)
For Each ctrl As Control In container.Controls
Dim tEdit As TextEdit = TryCast(ctrl, TextEdit)
If tEdit IsNot Nothing Then
AddHandler tEdit.EditValueChanged, AddressOf tEdit_EditValueChanged
Else
SubscribeTextEditValueChanged(ctrl)
End If
Next ctrl
End Sub
Private IsEditValueChanged As Boolean
Private Sub tEdit_EditValueChanged(ByVal sender As Object, ByVal e As EventArgs)
IsEditValueChanged = True
End Sub
Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
If IsEditValueChanged Then
' do some stuff
End If
MyBase.OnClosing(e)
End Sub
End Class

Close userform with escape button

I have 2 questions.
When I pressed esc button then close Userform1
When I input open in TextBox1 then Userform2 should show. Also clear TextBox1 in Userform1 automatically.
I have tried the below code:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If textbox1.value = "open" then
userform2.show
textbox1.value =""
End If
End Sub
Insert a new Command Button
Switch its Cancel property to True
You May Name it as cmdClose
Add next code:
Private Sub cmdClose_Click()
Unload Me
End Sub
5.Set height and widht of the button to 0
that's it
Close userform1 with Esc
If you don't have any controls on userform then simply use this code
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
If you have say a TextBox and a Command Button then use this
Private Sub UserForm_Initialize()
CommandButton1.Cancel = True
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub UserForm_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii = 27 Then Unload Me
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
If you have any other control that can take focus then you will have to use the KeyPress event of that control like I did for TextBox
when I input "open" to textbox1 then userform2 showed also clear textbox1 in userform1 automatically.
KeyPress will capture only one key. Use the Change event to compare what is there in the textbox.
Private Sub TextBox1_Change()
If LCase(TextBox1.Value) = "open" Then
TextBox1.Value = ""
UserForm2.Show
End If
End Sub
if you have a button the closes the form, just set the (Cancel) property to True and that will fire the cancel button on (Esc)..
Cheers.