I'm writing a vb6 user control (for my sins) and all has gone well up until now. I'm struggling to get the height and width of the parent/container.
The User-control is exported as an activeX control (.ocx), registered on the target machine using Regsvr32 and then embedded into an existing app.
I don't have access to the existing apps code so I can't inject anything that way.
I've tried accessing the UserControl.Parent object but receiving errors.
Does anyone know how to do this or can set me down the right path. My main language is c# so be kind to me please.
Most likely you are trying to fiddle with Parent before the control has been sited. Doing this instead works just fine:
Option Explicit
Public Sub Update()
'Call from parent container's Resize event handler, etc.
Cls
With Parent
Print .ScaleWidth; " × "; .ScaleHeight
Print .Width; " × "; .Height
End With
End Sub
Private Sub UserControl_Initialize()
AutoRedraw = True 'We're using Print here.
End Sub
Private Sub UserControl_InitProperties()
Update
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Update
End Sub
Related
I have simple code in the LostFocus event of a textbox control
that changes the text to upper case:
Private Sub txtIntegration_LostFocus()
If Trim(txtIntegration.Text) <> "" Then
txtIntegration.Text = UCase(Trim(txtIntegration.Text))
End If
End Sub
When the application is installed, the code to uppercase the text does not work in.
I'm creating the installer with the "Package and Deployment Wizard" of Visual Basic 6.
As a troubleshooting step and potential solution for what you are trying to achieve, you can force the text to uppercase as it is being typed:
Private Sub txtIntegration_Change()
Dim iPosition As Integer
With txtIntegration
iPosition = .SelStart ' Save cursor position
.Text = UCase$(.Text)
.SelStart = iPosition ' Restore cursor position
End With
End Sub
You can at least try this approach and see if it works once installed. There should be no reason the installation process breaks any of your code.
The solution was, when you add or remove the program, the removal program does not uninstall the .EXE completely
Simply delete the Directory and solve it!
I'm trying to get a button to change from the "Arrow Left" to "Arrow Right" picture when I click it, but I'm not sure how to assign the images through VBA. I tried Me!btnCollapseUnscheduled.Picture = "Arrow Left", but I get an error message this way.
In Access 2010 or later, you can store images in the MSysResources system table. Then choose Shared for your command button's Picture Type property, and select one of those shared images.
Afterward, it's easy to toggle between the shared images from the command buttons's click event. I named my command button "cmdArrow" and the shared images "Arrow Left" and "Arrow Right" ...
Option Compare Database
Option Explicit
Private Sub cmdArrow_Click()
Dim strDirection As String
If Me!cmdArrow.Picture Like "*Left" Then
strDirection = "Right"
Else
strDirection = "Left"
End If
Me!cmdArrow.Picture = "Arrow " & strDirection
Me!txtImage.Value = Me!cmdArrow.Picture
End Sub
Private Sub Form_Load()
Me!txtImage.Value = Me!cmdArrow.Picture
End Sub
Screenshots:
The biggest challenge was loading the images into MSysResources. I created a throwaway form and added a command button. Next I chose Embedded as the button's Picture Type property and selected Arrow Left from the available choices. Then I changed Picture Type from Embedded to Shared, which added a row to MSysResources with the image data in an attachment field and my command button's name in the Name field. So I opened the table, changed Name to "Arrow Left" and closed the table. Note MSysResources will not be visible in the Navigation pane unless you enable "Show System Objects" in the Navigation Options.
I repeated those steps for "Arrow Right". It may sound fiddly, but it's not really a huge hurdle.
The trick is to use the full file path of your icon.
MS-Access might be using some icon map for its native icons, so you might not have much luck getting a path to one of those.
If you definitely can't find the path to the native MS-Access icons, you could always just do a google image search for arrow icons (specify exact size of 16 x 16 pixels).
You can then just save these icons to a folder of your choosing and then use those paths in the following illustrations.
If you just want the button to change its image on the first click and stay that way thereafter:
Make sure the button does not have a picture attached in Design View.
Assign the default image using its full path to your command button using the form's Open event:
Private Sub Form_Open(Cancel As Integer)
Me.cmdButton.Picture = "C:\myFolder\myDefaultIcon.png"
End Sub
Then in the button's click event, assign the .picture property to the icon path you want to change it to:
Private Sub cmdButton_Click()
Me.cmdButton.Picture = "C:\myFolder\myChangedIcon.png"
End Sub
If you just want the button to toggle between 2 images on each click:
Use a toggle button control instead of a command button control
Make sure the button does not have a picture attached in Design View.
Write a sub that will handle the different on/off states of your toggle button:
Public Sub togImg()
If _
Me.togButton = -1 _
Then
Me.togButton.Picture = "C:\myFolder\myChangedIcon.png"
Else
Me.togButton.Picture = "C:\myFolder\myDefaultIcon.png"
End If
End Sub
Call your sub from both the form's Open event and the toggle button's Click event:
Private Sub Form_Open(Cancel As Integer)
togImg
End Sub
Private Sub togButton_Click()
togImg
End Sub
You could create two buttons where only one is visible at a time, both calling the same code.
When one is clicked, unhide the other, set focus to this, then hide the first button. And vice-versa.
In order to use the SlideShowBegin event in Powerpoint, you have to have a Class Module configured the following way:
Public WithEvents App As Application
Private Sub App_SlideShowBegin(ByVal Wn As SlideShowWindow)
MsgBox "SlideShowBegin"
End Sub
Then, inside of a non-class module, you have to create an object of that type and set the App to Application.
Dim X As New Class1
Sub InitializeApp()
Set X.App = Application
End Sub
Now, the only issue I have is, if you don't manually called InitializeApp with the Macro Menu in Powerpoint, the events don't work. You have to call this sub before anything can called at the beginning of a slideshow INCLUDING this sub.
How can I go about calling this sub before running my powerpoint? Is there a better way to do this?
EDIT:
I've tried using Class_Initialize but it only gets called once it is first used or you make a statement like Dim X as Class1; X = new Class1
Usually event handlers are installed as part of an add-in, where you'd initialize the class in the Auto_Open subroutine, which always runs when the add-in loads. If you want to include an event handler in a single presentation, one way to cause it to init is to include a shape that, when moused over or clicked fires a macro, which inits your event handler and goes to the next slide.
Answering to an old question, but I hope my solution might helpt somebody ending up at this question.
The general advice for this issue is using a plug-in or placing some element on the slide and when that is clicked or hovered perform the initialization. Both are not always desired so I have the following approach:
In some module:
Dim slideShowRunning As Boolean
-----------------------------
Sub SlideShowBegin(ByVal Wn As SlideShowWindow)
' Your code for start-up
End Sub
-----------------------------
Public Sub OnSlideShowPageChange(ByVal Wn As SlideShowWindow)
If TypeName(slideShowRunning) = "Empty" Or slideShowRunning = False Then
slideShowRunning = True
SlideShowBegin Wn
End If
End Sub
----------------------------
Public Sub OnSlideShowTerminate(ByVal Wn As SlideShowWindow)
slideShowRunning = False
End Sub
For me this works perfectly. NOTE I am by no means a vba expert, actually I might have less than 50 hours of vba programming (maybe only 8 in powerpoint). So this might be an horrible solution. I don't know, but for me it works so I liked to share.
In fact, OnSlideShowPageChange runs when slideshow begins. Just make sure it doesn't work in the subsequent page changes if not needed using a global variable. See answer from C. Binair for details.
I have a VB.net test application that clicks a link that opens the Microsoft Word application window and displays the document. How do I locate the Word application window so that I can grab some text from it?
You can use the Word COM object to open the work document and then you manipulate it. Make sure to add a reference for Microsoft Word first.
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strFileName As String
Dim wordapp As New Microsoft.Office.Interop.Word.Application
Dim doc As Microsoft.Office.Interop.Word.Document
Try
doc = wordapp.Documents.Open("c:\testdoc.doc")
doc.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Word document.")
End Try
End Sub
End Class
The doc object is a handle for the instance of Word you have created and you can use all the normal options (save, print etc). You can do likewise with the wordapp. A trick is to use the macro editor in Word to record what you want to do. You can then view this in the Macro Editor. This give you a great starting point for your VB code.
Also, be sure to dispose of the Word COM objects at the end.
I've done something similar with a SourceSafe dialog, which I posted on my blog. Basically, I used either Spy++ or Winspector to find out the window class name, and make Win32 calls to do stuff with the window. I've put the source on my blog: http://harriyott.com/2006/07/sourcesafe-cant-leave-well-alone.aspx
Are you trying to activate the word app? If you want full control, you need to automate word from your vb.net app. Check here for some samples: 1, 2
In some VB6 code, I have a handler for a TreeView's Collapse event:
Private Sub MyTree_Collapse(ByVal Node as MSComCtlLib.Node)
This is called whenever a node in the tree is collapsed, whether by the user or programmatically. As it turns out, through some roundabout execution, it may happen that this handler will wind up telling a node to collapse, leading to infinite recursion.
I can think of multiple ways to skin this cat, but what seems simplest to me is to tell the TreeView not to raise events for some period of time. I can't find a simple call to let me do this, though. Has anyone successfully done this, or do I need to keep track of state in some other manner so I can respond appropriately when recursive events come along?
#Phil - I came to the same conclusion. My implementation of MyTree_Collapse now looks something like this (where m_bHandlingCallback is a member variable):
Private Sub MyTree_Collapse(ByVal Node as MSComCtlLib.Node)
If m_bHandlingCallback Then Exit Sub
m_bHandlingCallback = True
DoSomeStuff
m_bHandlingCallback = False
End Sub
Another way in VB6 is to have an alternate WithEvents reference to the control:
Private WithEvents alt as TreeView
and in Form_Load:
Private Sub Form_Load()
Set alt = MyTree
End Sub
Now alt will receive lots of events like this:
Private Sub alt_Collapse(ByVal Node as MSComCtlLib.Node)
Set alt = Nothing
'DoSomeStuff'
Set alt = MyTree
End Sub
But, during the DoSomeStuff, the events are unhooked - which also applies to all other event Subs for alt without the need for them to have intrusive changes.
I think that like many events in VB, it can't be switched off.
Just set a boolean flag as you've suggested.
I would declare the flag variable as STATIC in the Sub. This avoids making the variable global and makes it keep its value between calls.