i'm tring to create some label programmatically, the code doesn't return any error but i cannot see any label in my window.
dim dr As DatabaseRecord
dim sql As String
sql = "SELECT * FROM pack WHERE applicabilita_modello LIKE '%" + versione + "%'"
dim rs As RecordSet = database.SQLSelect(sql)
dim i As Integer = 1
dim test(10) As Label
while not rs.EOF
test(i) = new Label
test(i).Text = rs.Field("descrizione").StringValue
test(i).Left = me.Left
test(i).Top = me.Top * i
test(i).Enabled = true
test(i).Visible = true
rs.MoveNext
i = i + 1
wend
rs.Close
i've verified that the recordset contain some data, the loop work correctly but no label is shown and cannot understand why.
thanks for any help
There are two ways to create controls at runtime in Real Studio. The first is to create a control array. You could name the control MyLabel and give it an index of zero. Then your code would be:
test(i) = new MyLabel
The second is to use a ContainerControl. This container would contain a label and because you can add them to your window (or other container) using the NEW command and using the ContainerControl.EmbedWithin method.
I generally prefer the ContainerControl approach for many reasons, but mostly because control arrays make the logic a big more convoluted. The only drawback with containers is that it requires Real Studio Professional or Real Studio Enterprise.
http://docs.realsoftware.com/index.php/UsersGuide:Chapter_5:Creating_New_Instances_of_Controls_On_The_Fly
http://docs.realsoftware.com/index.php/ContainerControl
Related
I'm a bit in need for some help. I'm trying to scroll a PDF in Patagames PDFium .net control using the keyboard. Unfortunately I can't get it to scroll correctly and am looking for some sample code (VB or C#) to scroll 'line wize (lets say 10 pixels per keypress)' and 'page wize'. Can anybody help me out here? TIA a lot. Ole
Never mind. Found an answer.
Private Sub PdfViewer1_KeyDown(sender As Object, e As KeyEventArgs) Handles PdfViewer1.KeyDown
Dim pageStep As Integer = PdfViewer1.AutoScrollMinSize.Height / PdfViewer1.Document.Pages.Count
Dim lineStep As Integer = pageStep / 30
Dim asp As Point = PdfViewer1.AutoScrollPosition
Select Case e.KeyCode
Case Keys.Up
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y - lineStep)
Case Keys.Down
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y + lineStep)
Case Keys.PageUp
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y - pageStep)
Case Keys.PageDown
PdfViewer1.AutoScrollPosition = New Point(asp.X, -asp.Y + pageStep)
End Select
End Sub
this is my code
Function GenerateInterface()
Dim ObjectsArray() As VB.Control
Dim TmpCtrl As VB.Control
ReDim ObjectsArray(1)
For Each TmpCtrl In Me.Controls
If TmpCtrl.Container Is frConfigVars(0) Then
Set ObjectsArray(UBound(ObjectsArray) - 1) = TmpCtrl
ReDim Preserve ObjectsArray(UBound(ObjectsArray) + 1)
End If
Next TmpCtrl
For i = 1 To UBound(Variables) - 1 'global array containing how many frames I need
Load frConfigVars(i)
frConfigVars(i).Left = 0
frConfigVars(i).top = frConfigVars(i - 1).top + frConfigVars(i - 1).Height
frConfigVars(i).Visible = True
For x = 0 To UBound(ObjectsArray) - 1
Set TmpCtrl = ObjectsArray(x)
Load TmpCtrl(i) '<-- crashes here
'stuff to move and view new object
Next x
Next i
End Function
It basically loads into a controls array the 0-indexed objects present in the frame to make me dinamycally load them how many times I need but I can't manage to load a new control from a variable itself.
I kinda got why that loading is crashing, I'm guessing the TmpCtrl contains (example) txtbox(0) and not txtbox, which I'd need to load the new object, correct?
If so, how can I load the new control?
I can't create objects from scratch because there are many of them and positioning would be hell
I can't call them by their name because with time I will add/remove some stuff so I don't want to touch this function again once it works
Thanks
Ok, I actually made it myself!
To access the object array itself I just need to change
Set TmpCtrl = ObjectsArray(x)
into
Set TmpCtrl = Me.Controls(ObjectsArray(x).Name)
I'm using the Janus GridEx grid control in Visual Basic 6 and I'm trying to change one of the properties (DatabaseName) programmatically before it uses the connection. The one I set in the properties window can be wrong and I'd like to set it dynamically before it's used. I've tried to set it in Form_Initialize() or Form_Load() and neither of them override the on in the properties window before it's used.
Any ideas?
Create one custom recordset and set to ADORecordset property's grid
gData.HoldFields
Set gData.ADORecordset = rstData
if you created one recordset with equal schema to database you can do
saved data
IDataRepository.Save(gData.ADORecordset)
Save skeleton
function Save (rs as adodb.recordset)
Dim cn As ADODB.Connection
Set cn = new ADODB.Connection
cn.CursorLocation = adUseClient
cn.Open Cnstr
Set rs.ActiveConnection = cn
If rs.LockType = adLockBatchOptimistic Then
rs.UpdateBatch
Else
rs.Update
End If
Set rs.ActiveConnection = Nothing
cn.Close
Set cn = Nothing
end function
for property with mask you can for example
gData.Columns("Status").ValueList.Clear
gData.Columns("Status").ValueList.Add "A", "Accepted"
gData.Columns("Status").ValueList.Add "R", "Rejected"
Interval value is preserved and user can view some more human readable
I have an Excel 2007 Worksheet with many buttons and labels that act as menu options (i.e. user clicks the buttons, labels with images) and is presented with forms, or some thing else.
These images / icons for the buttons and labels are loaded in VBA by assigning the Picture property of the Control and calling LoadPicture() method with the full image file path as parameter, like So.
With SomeFormObject
.cmdOpenFile.Picture = LoadPicture("F:\projectname\images\fileopen.BMP")
End With
This method of loading images for buttons, other controls is causing 2 issues.
1) It creates a dependency on the image files and physical location for every user, so if a user does not have the drive mapped and files present, the VBA fails with runtime error of file or path not found.
2)
The app gets very slow if the images are on a shared drive (which is the case)
I want to eliminate both issues and somehow load icons, images into control internally, without any external dependencies on external image files.
What is the best way to achieve this in Excel 2007 VBA?
I could not file any Visual Basic 6.0 / Visual Studio style "Resource File Editor" / feature with which to accomplish this.
Please advice! thank you
-Shiva #
mycodetrip.com
I really hope that there is a easier way to do this, but this is the only one I found:
The Idea is:
You keep the Pictures embedded in a Sheet and every time you want to set the pictures for the Command you export them from your worksheet to a file and load them through LoadPicture. The only way to export an embedded Picture through VBA that I found is by making it a Chart first.
The following code is based on 'Export pictures from Excel' from johnske
Option Explicit
Sub setAllPictures()
setPicture "Picture 18", "CommandButtonOpen"
setPicture "Picture 3", "CommandButtonClose"
End Sub
Sub setPicture(pictureName As String, commandName As String)
Dim pictureSheet As Worksheet
Dim targetSheet As Worksheet
Dim embeddedPicture As Picture
Dim pictureChart As Chart
Dim MyPicture As String
Dim PicWidth As Long
Dim PicHeight As Long
Set pictureSheet = Sheets("NameOfYourPictureSheet") ' <- to Change '
Set targetSheet = Sheets("NameOfYourSheet") ' <- to Change '
Set embeddedPicture = pictureSheet.Shapes(pictureName).OLEFormat.Object
With embeddedPicture
MyPicture = .Name
PicHeight = .ShapeRange.Height
PicWidth = .ShapeRange.Width
End With
Charts.Add
ActiveChart.Location Where:=xlLocationAsObject, Name:=pictureSheet.Name
Set pictureChart = ActiveChart
embeddedPicture.Border.LineStyle = 0
With pictureChart.Parent
.Width = PicWidth
.Height = PicHeight
End With
With pictureSheet
.Select
.Shapes(MyPicture).Copy
With pictureChart
.ChartArea.Select
.Paste
End With
.ChartObjects(1).Chart.Export Filename:="temp.jpg", FilterName:="jpg"
End With
pictureChart.Parent.Delete
Application.ScreenUpdating = True
targetSheet.Shapes(commandName).OLEFormat.Object.Object.Picture = LoadPicture("temp.jpg")
Set pictureChart = Nothing
Set embeddedPicture = Nothing
Set targetSheet = Nothing
Set pictureSheet = Nothing
End Sub
Sub listPictures()
' Helper Function to get the Names of the Picture-Shapes '
Dim pictureSheet As Worksheet
Dim sheetShape As Shape
Set pictureSheet = Sheets("NameOfYourSheet")
For Each sheetShape In pictureSheet.Shapes
If Left(sheetShape.Name, 7) = "Picture" Then Debug.Print sheetShape.Name
Next sheetShape
Set sheetShape = Nothing
Set pictureSheet = Nothing
End Sub
To Conclude:
Loading the Images from a Mapped Networked Drive seems less messy, and there shouldn't be that much of a speed difference.
2 alternatives i can think of: form.img.picture=pastepicture , and = oleobjects("ActiveXPictureName").object.picture.
i have written some Macros for Visio. Now I copied these to a Stencil called Macros.vss
How can I call my Macros now?
It all depends on what the macros do and how you'd like to call them. I'm going to assume they're simply macros that will execute something within the active Visio page.
By default in Visio VBA, any public subs with no arguments get added to the Visio Tools->Macros menu, in a folder named by the document holding the macros (in this case Macros) and then separated into folders by module name. If you're the only person using the macros then you probably don't need to do anything else.
However, since you put them in a vss file I'll assume you'd like to distribute them to other people.
There's something funny (and by funny I mean irritating) about Visio and how toolbars and buttons work, when added programmatically. Unfortunately, when you create a toolbar using the UIObject and Toolbar and ToolbarItem classes, Visio is going to assume the code you're calling resides in the active drawing, and cannot be in a stencil. So I can give you a little guidance on using those classes, but basically it consists of distributing a .vst template along with your .vss files, with just a single required sub in the .vst file.
So, instead of using a custom toolbar, you can attach code to shape masters in your .vss file that execute the code when they get dropped on a drawing document (using CALLTHIS and the EventDrop event in the shapesheet). With this method I just have a sub that gets called using callthis that takes a shape object as an argument, executes some code, then deletes the shape (if I don't want it around anymore).
And lastly, you can manipulate the Visio UI programmatically to add a toolbar and buttons for your macros. Below is some sample code, basically the way I do it with a solution I developed. As I mentioned above, the most important part of using this method is to have a document template (.vst) that holds a sub (with the below code it must be named RunStencilMacro) that takes a string as an argument. This string should be the "DocumentName.ModuleName.SubName". This sub must take the DocumentName out of the string, and get a Document object handle to that document. Then it must do ExecuteLine on that document with the ModuleName.SubName portion. You'll have to step through the code and figure some things out, but once you get the hang of what's going on it should make sense.
I'm not sure of any other ways to execute the macros interactively with VBA. I think exe and COM addons may not have this issue with toolbars...
Private Sub ExampleUI()
Dim UI As Visio.UIObject
Dim ToolbarSet As Visio.ToolbarSet
Dim Toolbars As Visio.Toolbars
Dim Toolbar As Visio.Toolbar
Dim ToolbarItems As Visio.ToolbarItems
Dim ToolbarItem As Visio.ToolbarItem
Dim TotalToolBars As Integer
Dim Toolbarpos As Integer
Const ToolbarName = "My Toolbar"
' Get the UIObject object for the toolbars.
If Visio.Application.CustomToolbars Is Nothing Then
If Visio.ActiveDocument.CustomToolbars Is Nothing Then
Set UI = Visio.Application.BuiltInToolbars(0)
Else
Set UI = Visio.ActiveDocument.CustomToolbars
End If
Else
Set UI = Visio.Application.CustomToolbars
End If
Set ToolbarSet = UI.ToolbarSets.ItemAtID(visUIObjSetDrawing)
' Delete toolbar if it exists already
TotalToolBars = ToolbarSet.Toolbars.Count
For i = 1 To TotalToolBars
Set Toolbar = ToolbarSet.Toolbars.Item(i - 1)
If Toolbar.Caption = ToolbarName Then
Toolbar.Visible = False
Toolbar.Delete
Exit For
End If
Next
' create toolbar
Set Toolbar = ToolbarSet.Toolbars.Add
Toolbar.Caption = ToolbarName
Dim IconPos As Long ' counter to determine where to put a button in the toolbar
IconPos = IconPos + 1
Dim IconFunction As String
IconFunction = """Macros.Module1.SubName"""
Set ToolbarItem = Toolbar.ToolbarItems.AddAt(IconPos)
With ToolbarItem
.AddOnName = "RunStencilMacro """ & IconFunction & """"
.Caption = "Button 1"
.CntrlType = Visio.visCtrlTypeBUTTON
.Enabled = True
.state = Visio.visButtonUp
.Style = Visio.visButtonIcon
.Visible = True
.IconFileName ("16x16IconFullFilePath.ico")
End With
' Now establish the position of this toolbar
With Toolbar
.Position = visBarTop 'Top overall docking area
.Left = 0 'Puts it x pixels from the left
.RowIndex = 13
.Protection = visBarNoCustomize
Toolbar.Enabled = True
.Visible = True
End With
Visio.Application.SetCustomToolbars UI
Visio.ActiveDocument.SetCustomToolbars UI
End Sub