How to add event handler to index control in VB6
I have indexed control:
txtInput(0), txtInput(1), txtInput(2)
but when i create event to them like txtInput(0)_Click, the error showing...
Please tell me how to fix it. Thanks!
Just add your code to the click event for the control. Since it's an array, the index for the control being clicked will be an argument on the event handler:
Private Sub txtInput_Click(Index as Integer)
If Index = 0 then
'code for first control
ElseIf Index = 1 then
'code for second control
Else
' and so on ....
End If
End Sub
Related
Just wanted to ask, Is there a form_closing event equivalent in VB6? (.bas). the reason I ask is that I wanted to prevent first closing the application if there's an open sub-window.
something like this.
Private Sub MainForm_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If hasSubWindow Then
MessageBox.Show("You currently have active sub-window(s)")
e.Cancel = false
End If
End Sub
There are three events: Form_QueryUnload(Cancel As Integer, UnloadMode As Integer), Form_Unload(Cancel As Integer) and Form_Terminate().
Form_QueryUnload fires before the Form_Unload where setting Cancel to any value other than 0 cancels the unload and UnloadMode indicates the cause of the unload event.
See https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-basic-6/aa445536(v=vs.60)?redirectedfrom=MSDN
Form_Unload takes an integer that if set to any value other than 0, cancels the Unload.
The Unload event procedure is where programmers usually put cleanup code. The Unload event fires after the QueryUnload event. The Unload event procedure takes a single parameter, the Cancel parameter. Unload's Cancel parameter works the same as QueryUnload's Cancel parameter.
The Terminate event happens when the instance of the form has been completely unloaded and all the form's variables have been set to Nothing. The Terminate event happens only when you explicitly set the form to Nothing in your code during or after the Unload event procedure.
Source: https://www.freetutes.com/learn-vb6-advanced/lesson6/p5.html
In your case, you probably want to put code into Form_QueryUnload or Form_Unload that checks if there are any other open forms.
Private Sub Form_Unload(Cancel As Integer)
If hasSubWindow = True Then
MsgBox "You currently have active sub-window(s)", vbInformation, "Confirm"
Cancel = 1
End if
End Sub
I am doing a quiz game in VB6. I need the textbox to automatically capialize the first letter but this code
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
causes the word to invert. So instead of "Trees" it turns into "Seert"
How do I change this?
Pay attention to where the cursor is positioned in the textbox when the event Change occurs: its at the start of the textbox. Add a Debug.Print statement to see what's going on while you type:
Private Sub Anstxt_Change()
Debug.Print StrConv(Anstxt.Text, vbProperCase)
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
End Sub
The output looks like
T
T
Rt
Rt
Ert
Ert
Eert
Eert
Seert
Seert
Two things to notice here: the Change event is triggered twice: once from typing and once from changing the value of the textbox within the Change event. That gives you an idea that manipulating the text of a textbox in its Change event isn't a good idea. I suggest to put this code in the LostFocus event instead.
The second thing to notice is that as the cursor is always at the start of the textbox, the letters you type are inserted there in front of the existing letters. So after you change the .Text property of the textbox, you should position the cursor at the end of the textbox with the .SelStart method:
Anstxt.SelStart = Len(Anstxt.Text)
e.g.
Private Sub Anstxt_Change()
Anstxt.Text = StrConv(Anstxt.Text, vbProperCase)
Anstxt.SelStart = Len(Anstxt.Text)
End Sub
According to Microsoft:
"In Visual Basic 6.0, the Click event is raised when the CheckBox state is changed programmatically. "
and this is exactly what i do not want.
I want click event only raise when i click on the checkbox and not when the state is changed.
Any idea how to do it ?
Thank you
you can set a flag when you are populating the form from code to ignore changes. This can get messy if the code is not organized well.
Form Level:
Public IgnoreChange As Boolean
Form Load:
IgnoreChange = False
Event:
If IgnoreChange Then Exit Sub
Your code:
frmReference.IgnoreChange = True
frmReference.Checkbox1.Checked = True
frmReference.IgnoreChange = False
Code should only respond to user actions
Not sure if it's the best way, but one way would be to have a variable called something like IgnoreEvents and set that to true right before changed the state programmatically.
Then in the event handler if that variable is true, you just exit the event handler without doing anything.
Put your Click event code in the MouseDown event instead. You'll have to manually set the checkstate though:
Private Sub Check1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Check1.Value = IIf(Check1.Value = vbChecked, vbUnchecked, vbChecked)
' run other necessary code here
End Sub
I'm customising the Group Headers on a Telerik RadGrid by injecting a LinkButton into it during the ItemDataBound event. The button renders perfectly, but I can't get it to hit any event handlers.
Here is the code for the button creation:
Private Sub rgWorkRequestItemCosts_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles rgWorkRequestItemCosts.ItemDataBound
If TypeOf e.Item Is GridGroupHeaderItem Then
Dim oItem As GridGroupHeaderItem = DirectCast(e.Item, GridGroupHeaderItem)
Dim lnkAdd As New LinkButton()
lnkAdd.ID = "lnkAdd"
lnkAdd.CommandName = "CustomAddWorkRequestItemCost"
lnkAdd.CommandArgument = DirectCast(oItem.DataItem, DataRowView).Row("nWorkRequestItemID").ToString()
lnkAdd.Text = String.Format("<img style=""border:0px"" alt="""" width=""12"" src=""{0}"" /> Add new cost", ResolveUrl(String.Format("~/App_Themes/{0}/Grid/AddRecord.gif", Page.Theme)))
lnkAdd.Style("color") = "#000000"
lnkAdd.Style("text-decoration") = "none"
AddHandler lnkAdd.Click, AddressOf lnkAdd_Click
Dim tcPlaceholder As GridTableCell = DirectCast(oItem.Controls(1), GridTableCell)
Dim litText As New LiteralControl(String.Format(" {0}", tcPlaceholder.Text))
tcPlaceholder.Text = String.Empty
tcPlaceholder.Controls.Add(lnkAdd)
tcPlaceholder.Controls.Add(litText)
End If
End Sub
This code explicitly adds a handler for the LinkButton, but that handler is never hit. I've also tried events on the RadGrid (ItemCommand, ItemEvent) but none seem to get hit.
Has anyone got any suggestions of other events to try, or ways to make this work?
Thanks!
I wasn't able to find a "nice" solution to this. In the end I did the following:
Created the button in the
ItemCreated event handler, setting
its CommandArgument to a counter
which was incremented for every
group header created
Again created the button in the
ItemDataBound event, again settings
its CommandArgument to the counter
value. At this point I added a
record to a dictionary object
(stored in ViewState) linking the
counter to the actual value of the
group.
Handled the click event of the
button, extracting the group value
from the dictionary in viewstate to
complete the processing.
Ugly, but it works.
I have an application written in VBA for Excel that takes in a live data feed. Whenever there is a change in the data, various events are triggered within VBA.
I also have some UserForms with ComboBoxes on them. My problem is that when I click the down arrow on the ComboBox and try to make a selection, the moment I get an update from the data feed, the ComboBox resets. What I would like to do is pause the events while I am making my selection in the ComboBox and then unpause it when I am done. How do I generate this functionality?
Try this to turn off:
application.enableevents = false
And this to turn back on:
application.enableevents = true
To pause and show a message and to keep working on something during the pause. finally press button
Public Ready As Boolean
Private Sub Command1_Click()
Ready = True
End Sub
Private Sub Form_Load()
Me.Show
Ready = False
Call Wait
Label1.Visible = True
End Sub
Public Function Wait()
Do While Ready = False
DoEvents
Loop
End Function
Maybe you can put a flag to bypass the update event on your combobox until a selection is made.
Private bLock as boolean ' declare at module level
' When a user clicks on the combobox
Private Sub DropDownArrow_Click() ' or cboComboBox_Click()
bLocked = True
End Sub
' This procedure is the one that does the updating from the data source.
' If the flag is set, do not touch the comboboxes.
Private Sub subUpdateComboBoxes()
If Not bLocked then
' Update the comboboxes
End If
End Sub
' When the selection is made, or the focus changes from the combobox.
' check if a selection is made and reset the flag.
Private Sub cboComboBox_AfterUpdate() ' Or LostFucus or something else
if Format(cboComboBox.Value) <> vbNullString Then
bLocked = False
End If
End Sub
Hope that helps.