vb6 changing colors of SSTab at runtime using label in the background - vb6

I am trying to update an old VB6 application. I am struggling with changing the backcolor of the SSTab control at runtime.
I know that I cannot just set the Backcolor to the required colour.
At designtime I have created a label on each tab and then resized the label to the sstab dimensions. Then I set the label backcolor. This works fine.
However, what I really want to do is to do this at runtime so that if a new tab control is added on a form in the future I dont need to manually do this at designtime each time.
I would be grateful if someone can point me in the right direction to create and display a label on every tab of a SSTab control at runtime?
I dont have frames on all the tabs so I dont want to use the frame container to hold the label. I would rather just have the label placed on the tab container at runtime.
Thanks and I look forward to your replies.

As svinto says, you can load new controls to a Control Array, but there's a bit more to it than that.
Firstly the new controls are not Visible, so you must set .Visible=True
You also need to set the current tab before creating the control to get it sited on that tab.
Assuming you already have the first label on the first tab:
For i = 1 To Me.SSTab1.Tabs - 1
Me.SSTab1.Tab = i
Load lblOne(i)
lblOne(i).Visible = True
Next
Also, this doesn't work in the Form_Load event, but does in Form_Activate
There is another method to create the Labels from scratch rather than using a Control Array, which might be better as you don't need to add the first Label to the form:
Private Sub Form_Activate()
Dim lbl() As Label
Dim i As Integer
Dim name As String
ReDim lbl(SSTab1.Tabs - 1)
For i = 0 To Me.SSTab1.Tabs - 1
Me.SSTab1.Tab = i
name = "MyLabel" & CStr(i)
Set lbl(i) = Form1.Controls.Add("VB.Label", name)
Set lbl(i).Container = Me.SSTab1
lbl(i).Visible = True
lbl(i).Caption = name
lbl(i).Move 40, 345
lbl(i).BackColor = vbGreen
Next
End Sub

Create one label at design-time, and set it's index to 0. You can later on do Load labelname(newindex) to create more instances of the label. Position them once created, set the background color and you're all done.

Related

How to replace text in Powerpoint with activeX textbox value?

I'm sorry if I can't explain this very well. Language barrier and all that. Might explain why I wasn't able to google a solution... Anyway.
I'm trying to make an interactive Powerpoint where in the first slide the user writes their name in an ActiveX textbox and the name then repeats in several other slides. Because the name would be in the middle of different texts my solution was to add a placeholder word that would then be replaced with the textbox value. I was able to Frankenstein a code that replaces one name with another that works well enough but I can't get the textbox to work with it. Best I can do is replace the placeholder name with some variation of TextBox1.Value and while I know what causes that issue I can't work out a solution.
Not sure if it makes a difference but the plan is to add some sort of "OK" button next to the active textbox that runs the macro.
Sub Findandreplace()
Dim sld As Slide
Set sld = ActivePresentation.Slides(1)
Dim shp As Shape
For Each sld In ActivePresentation.Slides
For Each shp In sld.Shapes
If shp.HasTextFrame Then
If shp.TextFrame.HasText Then
shp.TextFrame.TextRange.Text = Replace(shp.TextFrame.TextRange.Text, "word1", "word2")
End If
End If
Next shp
Next sld
End Sub
ActiveX objects have a different set of properties/methods than regular PPT shapes. Assuming you have a reference to the AX textbox in a variable oSh,
osh.OLEFormat.Object.Text
will give you the text in the ActiveX textbox

How to edit GUI elements on a specific frame in VB6

How I can get access to the nested controls of a control?
I have serveral frames on my user interface and every frame contains serveral other controls (like labels, buttons, ...). I have to iterate over the frames and change content of the children of a specific frame (e.g. set another text in a label).
So far I iterate over all controls of my frame and I check if the control in the loop control variable is the frame where the changes should be.
Dim cntrl As Control
For Each cntrl In Controls
'Debug.Print cntrl.Name // here I get all controls on the form
If cntrl.Name = "Frame_Name" Then
If cntrl.Index = index Then
Debug.Print "true" ' here the caption of nested components should be changed
End If
End If
Next
Now I have the frame in the control variable but the problem is that I get no access to the nested label to change the label's caption. What can I do?
You need to look at the Container property of each control. The following code should give you the idea:
Dim cntrl As Control
For Each cntrl In Controls
If cntrl.Container.Name = "Frame_Name" Then
Debug.Print cntrl.Name & " is nested in the specified frame"
End If
Next

How could I use VBA to change a button's picture to one of the embedded default images?

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.

Start by displaying print-layout in VS10 Report Designer when running the form

When I run my report from VS10, I have to switch everytime to print-layout. Because I need to make 100+ small adjustment to my big table, I have to press the print-layout button everytime to see the result.
Is there a way, I can set it up, so I start by seeing the print-layout when running the form?
It´s an old post but found the exact answer. Setting Print-Layout instead of setting Zoom mode for the ReportViewer.
ReportViewer1.SetDisplayMode(DisplayMode.PrintLayout)
It looks like you can adjust report viewer properties. I found a adjustment that sets the zoom, in your case (print layout = whole page) so the default would need to be changed.
Here is the VB script for it from MS.
'Declaration
<CategoryAttribute("Appearance")> _
<DefaultValueAttribute(ZoomMode.Percent)> _
Public Property ZoomMode As ZoomMode
'Usage
Dim instance As ReportViewer
Dim value As ZoomMode
value = instance.ZoomMode
instance.ZoomMode = value
I don't know if you have to use a # like value = 50 or if you can use value = Whole Page, it seems like the latter can be used since it bases the figures dimensions on the logical page to assume the view size.
Goood luck and check http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportviewer.zoommode.aspx for further guidance and different code options.

Width property of form is not what it's set to, though looks OK

The form's borderstyle is "fixed single" so the user cannot alter the form's dimensions manually.
There is a menu item that allows the user to select 1 of 2 width settings for the form, choosing wide (width = 11715) displays extra controls.
During the load event a sub is called to set various values and which includes this code:
If rs.Fields("ShowAll").Value = True then
Me.Width = 11715
During the form unload event a sub is called to record the settings for the next time and which includes this code:
If Me.Width = 11715 Then
rs.Fields("ShowAll").Value = True
Else
rs.Fields("ShowAll").Value = False
End If
One user has reported a situation where even though he always closes with the form "wide" it always opens "narrow".
I have never seen this happen before and nobody has ever reported it either, which is not to say it's not happening elsewhere.
His machine is windows 7 and his screen resolution is 1280 X 1024.
Edit :
Also when I checked his database the value for "ShowAll" is False.
I suggest using a form level boolean variable rather than a specific number. You can also wrap it in a property if you like to make it easier to understand. Initialize the variable with the database value when you load your app and toggle the value when the button is clicked to choose the form size.
Private m_blnExpanded As Boolean
Private Property Let Expanded(ByVal vExpanded As Boolean)
m_blnExpanded = vExpanded
End Property
Private Property Get Expanded() As Boolean
Expanded = m_blnExpanded
End Property
If you are going to hard-code the width then you have to choose a number of twips that is multiple of 15 for small-fonts and multiple of 12 for large-fonts. The least multiple of both is 60, so the closest to your hard-coded value is 11700 which is 780px in small-fonts and 975px in large-fonts exactly.
Mind that win7 comes with more DPI sizes besides 96 (aka "small-fonts") and 120 (aka "large-fonts"), which totally fail VB6 forms subsystem. So you are safe to assume that in VB6 pixel size is either 15 or 12 twips.

Resources