Click and CheckStateChanged VB6 - vb6

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

Related

Form closing Event in VB6?

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

vb2013 exit from local sub closes form

I have a local sub that allows the user to move a row of a datagridview, triggered by a button click. The sub works fine in debugger but when it exits control is transfered to the calling form, i.e. the current form is closed. This also happens when no row is moved, i.e. when one of the abort conditions on entrance are met. Simply: exiting this sub will close the form!?!
Private Sub btnMove_Click(sender As Object, e As EventArgs) Handles btnMove.Click
Dim rowToGo As DataGridViewRow
Dim rtgIndex As Integer = 0
If (dgvAuftrag.RowCount <= 1) or (dgvAuftrag.CurrentRow Is Nothing) Then
Beep()
Exit Sub
End If
rowToGo = dgvAuftrag.CurrentRow
rtgIndex = rowToGo.Index + 1
If (rtgIndex >= dgvAuftrag.RowCount) Then rtgIndex = 0
Try
dgvAuftrag.Rows.Remove(rowToGo)
dgvAuftrag.Rows.Insert(rtgIndex, rowToGo)
Catch ex As Exception
IssueErrorMessage(ex)
End Try
End Sub
All other local subs and functions work normal, just this one behaves strange. Any ideas how to fix/avoid this bug?
This is not a solution of the problem but a functioning workaround based on the sugestion of Hans. I have introduced a global boolean var named OKtoExit which is intialized to false.
private OKtoExit as boolean = false
Then I have a new FormClose event handler that checks that var. If OKtoExit is false then e.Cancel = true and the handler exits. The regular Exit functions (Save and Quit) set OKtoExit to true, any other code leaves the values unchanged.
Private Sub Current_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not exitOK Then
e.Cancel = True
Exit Sub
End If
End Sub
As I said, this is just a workaround that has the same effect as a normal functioning VB-Code. I would appreciate if somebody could present a real solution!
After many months I discovered the true reason for the problem and I must give all credits to Hans Passant: I had a button on one of the first forms that had the Dialog Result property set to Cancel. This was a really beautyful button so many other buttons in the appliaction were a copy of this first button where I just modified the label. Thus they all led to the unwanted behavior that a form was closed as soon as a user clicked one of them no matter what the label said... After months I discovered that just by chance. Thanks to Hans again, I obviously overlooked his last hint "And look at the button's DialogResult property."!

How to Add Event Handler to Indexed Control in VB6?

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

Outlook 2013 Addin: Change Ribbon Button Image on click of button using c#

I have created Ribbon button for Outlook 2013 using c#.
And i have also set image for the ribbon.
Now on click of Ribbon button i want to change Ribbon Image.
Is it possible to achieve this using c#.?
Not sure how exactly you want it to work, but this could do the trick.
bool callback {get;set}
public Bitmap GetImage(IRibbonControl control)
{
switch (control.Id)
{
case "FooButtonId":
{
if(callback== true){
callback = false;
return new Bitmap(Properties.Resources.someimage1);
}else
callback =true;
return new Bitmap(Properties.Resources.someimage2);
}
}
}
}
This question is 3 yo but helped me to go further and I want to share this with you.
First, I accomplished this in VB.net so my code will be in VB.net. There are some online tools to convert the code into C#.
Second, I used a Toggle button instead of a Simple button.
Third, I used OnOff as a project setting in order to save the state of the Toggle button.
Step 1: Ribbon.xml file, code to place the toggle button on the ribbon.
Assuming you already have set up the tab and group tags in the file.
<toggleButton id="onoffTBTN" label="ON/OFF" showImage="true" onAction="OnOffToggle" getImage="OnOffImage"/>
Step 2: Ribbon.vb file, code to change the OnOff setting based on the Toggle button status(pressed or not) and forces to invalidate the custom control
Public Sub OnOffToggle(ByVal control As Office.IRibbonControl, ByVal pressed As Boolean)
My.Settings.OnOff = pressed
My.Settings.Save()
myRibbon.InvalidateControl("onoffTBTN")
End Sub
Step 3: Ribbon.vb file, reads the OnOff setting and changes the image accordingly. Have in mind that your images must have been added to your project resources in order to use them at My.Resources.*. I used png files that support transparent pixels. This function is called in two occasions, first when the Outlook starts and second when the toggle button is pressed and specifically with the command myRibbon.InvalidateControl("onoffTBTN").
Public Function OnOffImage(ByVal control As Office.IRibbonControl) As Drawing.Bitmap
Dim onoff As Boolean = My.Settings.OnOff
Select Case control.Id
Case "onoffTBTN"
If onoff = True Then
Return New Drawing.Bitmap(My.Resources._on)
Else
Return New Drawing.Bitmap(My.Resources.off)
End If
End Select
End Function
The only weird behaviour is when the OnOff setting has been set to TRUE. The correct image is displayed but the Toggle button looks unpressed. You have to click twice in order to set the OnOff setting to False.

Disable MDI child Form buttons

Is there possible to disable child's form buttons from parent form?
For example, I have 2 radio-buttons in parent form, one is True second False, when I choose one of them fires radiobutton.CheckedChanged event and there I have code what goes like this, but it's not working:
ChildForm.Button1.Enabled = False
where seems to be the problem? Can anyone help with this?
You would need to create an instance of the childform.
So...
ChildForm cf = new ChildForm();
cf.Button1.Enabled = false;
You have to keep in mind though, it could be a different instance than the child form that is currently being shown.
To be sure, depending on your code (which I can't see) and how your program is laid out I would probably do something like this...
ChildForm cf = new ChildForm();
cf.show();
cf.Button1.Enabled = false;
so here I know that the ChildForm that is showing is the one that has the button disabled.
In VB6 the following project works:
1 MDI form:
Option Explicit
Private Sub MDIForm_Click()
Form1.Option1.Enabled = False
End Sub
Private Sub MDIForm_Load()
Form1.Show
End Sub
Form1 is a MDI child form with 2 radio buttons on it

Resources