Making picturebox preform a function when clicked VB6 - vb6

I have a picturebox on my form. When I click on it, I want it to preform a function (in this case, setting a value to a variable). I, however, have not found any way to do this.
The picturebox, should during runtime, when clicked, set a value to a variable.
I have considered putting a textbox over the picturebox and making it transparent, but I have not found a way to make textboxes transparent via visual basic. There should be a more direct way available?
Can I get some help? Thanks.

You can double click on the picture box and it will bring up the code. From there insert the code you want
Private sub picture1_click()
End sub

Related

Put a Picturebox over a flow layout panel without it being affected by the flow. I can just put it everywher I want including over the panel

Is it possible to put a picturebox over a flowlayoutpanel without it being affected by the flow?
Yes. You simply have to make sure that it's parent is the form rather then the FLP. If you drag the PictureBox in the designer then you'll probably end up dropping in on the FLP. Instead, add it to the form and then set the Location property manually to move it. You can then manipulate the z-order in the Document Outline window if necessary to make sure the PictureBox is in front, or you can just right-click one of the controls and select "Send to Back" or "Bring to Front".

How to manually center button's caption in VB6

I have problem with button's caption in VB6.
I'm trying to change caption after "_load" event and it works, but new text is not in the center of the button - and it's a problem.
You can see it on the following screen:
https://dl.dropboxusercontent.com/u/3779161/buttons.png
I've tried to use "Refresh" function but without any effect.
Is it possible to refresh button without creating new one?
Thanks for help
I am unable to test VB6 code currently but, apparently, it requires Win-API calls to align the text. code here. Copy the code into a Module and then you can call the function AlignCommandButtonText. (Seems like hard work!)
the new text does center for me.
run the following test project and click on the form:
'1 form with
' 1 commandbutton: name=Command1 caption="Test"
Option Explicit
Private Sub Form_Click()
Command1.Caption = "New Test"
End Sub
the problem is probably caused by something else.
for example: do you have any tight loops?
could you post some of your code?

Excel: conflict between validation, protection, and worksheet_beforeDoubleClick

I'm trying to build a protected worksheet where one of the cells has the following qualities:
Double-clicking the cell populates it with "hello".
The cell can only be blank or contain the word "hello".
So I decided to put validation on the cell, and write a worksheet_beforeDoubleClick() event.
Let's say it's cell A1. Starting with a blank worksheet, in B1 I enter "hello", and I set A1's validation as a list with range B1:B2.
My double-click event code is as follows:
Private Sub worksheet_beforedoubleclick(ByVal Target As Range, Cancel As Boolean)
If Target.Row = 1 And Target.Column = 1 Then
Target.Value = "hello"
End If
End Sub
This code and validation works fine while the sheet is unprotected; the cell populates with "hello" when double-clicked.
However, once the sheet is protected, double-clicking on cell A1 turns the mouse pointer into an hourglass until I press Esc or click on another cell; the cell does not populate with the word "hello".
Any idea what's going on?
When you protect the sheet, check the "Edit Objects" checkbox and it will work. Don't know quite why, but it will.
Thanks Doug for a lead in the right direction. For future reference, a good VBA snippet for this is:
ActiveSheet.Protect UserInterfaceOnly:=True, DrawingObjects:=False
This will protect the sheet, but allow macros to edit the sheet, as well as clear up the conflict described here.
While the answers above with unprotecting the drawing objects works, the end solution with respect to the user being able to right click and edit drawing object shapes is not always acceptable.
My work around was to use a checkbox to the left of the cell and add a macro that did what my double click action originally was doing.

Removing focus from all objects in Visual Basic 6

Is there a method such that a user can click on the form itself, and in doing so remove focus from whatever object (textbox, combobox, etc) currently has it? Basically, can focus be uniformly removed from everything at once?
Setting the focus to the form itself does not work.
I thought about doing the old "hide a placeholder button behind another object" trick, but I'm really not a fan of that.
Thanks!
In VB6 a PictureBox can get focus, even if it does not contain any control.
In your case you can put a PictureBox with TabStop false, BorderStyle set to 0, TabIndex set to 0 behind every other control but not containing any focusable control and stretch it to ScaleWidth by ScaleHeight at run-time.
You have to put the labels and any windowless control in this background PictureBox too.
This way when the user clicks "on the form" the focus will "go away". With "no focus" Tab key will focus first control (the one with TabIndex set to 1).
When a form is active, something generally HAS to have focus. It sounds like you're just wanting to not "show" that a particular control has focus.
If that's the case, it's going to depend on the controls. Some have properties that control whether or not the specific control "indicates" its focus in some way.
But the built in Windows controls will always show their focus state unless you subclass them
Given this problem. I'd probably put a button on the form , then move it offscreen when the form loads. Make sure it's not a tab stop, but then when you want to hide focus, set focus specifically to the button, make sure the button is STILL in the tab order, even though it's not a tab stop, so the user can press tab while on the button and end up somewhere logical.
Don't have VB handy, but could you simply remove TabStop?
for x = 1 to me.Controls.count
me.Controls(x).TabStop = 0
next
I have a picturebox and a control on a form.
Private Sub cmdButton_Click
PictureBox.setFocus
Exit sub
End sub
The control doesn't change its appearance, nor does the picturebox.
Of course you'll need to add an If-Then clause if you want the control to respond normally sometimes.

Textbox anchored to a form on all 4 sides not displayed properly

I'm running into a problem trying to anchor a textbox to a form on all 4 sides. I added a textbox to a form and set the Multiline property to True and the Anchor property to Left, Right, Up, and Down so that the textbox will expand and shrink with the form at run time. I also have a few other controls above and below the textbox.
The anchoring works correctly in Visual Studio 2005 (i.e. I can resize the form and have the controls expand and shrink as expected), but when I run the project, the bottom of the textbox is extended to the bottom of the form, behind the other controls that would normally appear beneath it. This problem occurs when the form loads, before any resizing is attempted. The anchoring of the textbox is correct for the top, left, and right sides; only the bottom is malfunctioning.
Has anybody heard of this and if so, were you able to find a solution?
Thanks!
UPDATE:
Here is some of the designer code as per Greg D's request (I am only including the stuff that had to do with the textbox itself, not the other controls):
Friend WithEvents txtRecommendationText1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.txtRecommendationText1 = New System.Windows.Forms.TextBox
' ...snip...
'txtRecommendationText1
Me.txtRecommendationText1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtRecommendationText1.Location = New System.Drawing.Point(4, 127)
Me.txtRecommendationText1.Multiline = True
Me.txtRecommendationText1.Name = "txtRecommendationText1"
Me.txtRecommendationText1.Size = New System.Drawing.Size(223, 149)
Me.txtRecommendationText1.TabIndex = 10
End Sub
ANOTHER UPDATE:
The textbox I originally posted about was not inherited from a baseclass form (although it was added to a custom User Control class; I probably should have mentioned that earlier), but I recently ran into the same problem on a totally unrelated set of controls that were inherited from a baseclass form. It's easy to blame these problems on possible bugs in the .NET framework, but it's really starting to look that way to me.
Is your Form localized? Check the resource files for an entry with Textbox.Size, delete is and reset the size.
Is your Form inherited and is the Textbox on the baseform? Try setting the Textbox's access modifier to Protected or Public.
Have you implemented custom resize logic? Turn it off and see if the problem is still there.
Have you entered a Textbox.MinimumSize/MaximumSize? Remove or change the value.
It might also be a combination of these things...
Does the form snap back to the expected layout when you resize it after it's been initialized weirdly? Also, have you set a Height or MinimumHeight/MaximumHeight property for the text box?
If possible, a few snippets from the designer code might be useful. :)
One possibility that I've run into in the past is DPI. If you're running/testing your code on a machine with a different DPI setting than the machine that you're developing on, you may observe some strange things.
The anchor functionality essentially establishes a fixed distance between the edge of a control and the edge of the control's parent. Is your textbox embedded within another control (e.g., a panel) that doesn't have its anchors properly set? Right clicking on the text box in the designer should pop up a menu that lets you select any controls that exist underneath it, also.
Does your program include any custom resize logic, or does it modify the size of the textbox programmatically outside of designer-generated code? That might also result in weird behavior. I've assumed maintenance for a number of pieces of software at my organization where the original developers spent a great deal of time implementing (buggy) resize logic that I had to tear out so that I could just let the designer-generated code do the work for me.
The textbox I originally posted about was not inherited from a baseclass form (although it was added to a custom User Control class; I probably should have mentioned that earlier), but I recently ran into the same problem on a totally unrelated set of controls that were inherited from a baseclass form. It's easy to blame these problems on possible bugs in the .NET framework, but it's really starting to look that way to me.
It's very likely because of the 'AutoScaleMode' property being set in InitializeComponent(). Try setting it to 'None' and see if that fixes it. I've had these problem a couple of times now.

Resources