Call the name of a Texbox with number changing in Visual Basic 6 - textbox

I'm use Visual Basic 6 to create a table with many Textbox which named txtNo1, txtNo2, txtNo3,...
I want to use the "For...Next..." loop to assign a content to these Textbox.
How can I call all these Textbox in the simplest way?
For i = 1 to 100
txtNo (......) .txt = "ABC"
Next i

Instead of using unique textboxes, each with a unique name, you should use a (textbox) control array:
Place the 1st textbox on the form, name it 'txtNo'
Copy it and paste it onto the form
VB will ask you "There's already a control named 'txtNo'. Would you like to create a control array?". Answer "Yes"
Paste as the textbox as many times as you need it
Then your code looks like
' Control arrays typically start at index 0
For i = 0 to 100
txtNo(i) .txt = "ABC"
Next i
Jim Mack's solution works as well, code for it:
' Assuming your form is named 'Form1'
For each ctrl in Form1.Controls
If TypeOf ctrl Is Textbox
For i = 1 To 100
If ctrl.Name = "txtNo" & CStr(i) Then
ctrl.Text = "ABC"
End If
End If
End If
It's a bit more complex, but therefore more flexible as works with multiple control types (in one loop).

If you need an easiest way to create your textboxes as a table, you can Load the controls at runtime. You have to add only the first TextBox control to your form, set the name to "txtNo", and Index to 0 in the Properties window.
In your code, call Load() to create additional controls, and you can set the Top/Left and other properties
For i = 1 To 100
Load txtNo(i)
txtNo(i).Top = txtNo(i - 1).Top + txtNo(i - 1).Height + 150
txtNo(i).Left = txtNo(i - 1).Left
txtNo(i).Text = "Textbox " & i
txtNo(i).Visible = True
Next i
If you need again to change any control property, from your list of controls, you can iterate only over your control list, instead of all controls of your Form
For i = txtNo.LBound() To txtNo.UBound()
Form1.Controls("txtNo")(i).Text = "New text " & i
Next i

Related

Custom wireshark disector shows value but fieldname is not visible using lua

I am testing some network packets of my Organisation's product. We already have custom plugins. I am trying to add some some more fields into those existing plugins (like conversion of 2 byte code to a string and assign it to a field)
Thankyou in advance for reading my query.
--edit
Wireshark version : 2.4.5 (organization's plugins dont work on latest wireshark application)
--edit
Problem statement:
I am able to add field and show value, but fieldname is not displayed as defined.
I cannot share the entire .lua file but i will try to explain What i did:
Below is the image where I have a field aprint.type. this is a two byte field. In .lua file, for display purpose it is appended with corresponding description using a custom function int_to_enum.
I want to add one more proto field aprint.typetext which will show the text.
What I did:
Added a protofield f_apr_msg_type_txt = ProtoField.string("aprint.typetxt","aprint_type_text") (Tried f_apr_msg_type_txt = ProtoField.string("aprint.typetxt","aprint_type_text",FT_STRING) also)
Below the code where subtree aprint.type is shown, added my required field as subtree:add(f_apr_msg_type_txt, msg_type_string) (Below is image of code extract)
I am able to see the text but field Name is shown as Wireshark Lua text (_ws.lua.text)
Normally displaying strings based on numeric values is accomplished by a value string lookup, so you'd have something like so:
local aprint_type_vals = {
[1] = "Foo",
[2] = "Bar",
[9] = "State alarm"
}
f_apr_msg_type = ProtoField.uint16("aprint.type", "Type", base.DEC, aprint_type_vals)
f_apr_msg_type_txt = ProtoField.string("aprint.typetxt","aprint_type_text", base.ASCII)
... then
local msg_type = tvb(offset, 2):le_uint()
subtree:add_le(f_apr_msg_type, tvb(offset, 2))
subtree:add(f_apr_msg_type_txt, tvb(offset, 2), (aprint_type_vals[msg_type] or "Unknown"))
--[[
Alternatively:
subtree:add(f_apr_msg_type_txt, tvb(offset, 2)):set_text("aprint_type_text: " .. (aprint_type_vals[msg_type] or "Unknown"))
--]]
I'm also not sure why you need the extra field with only the text when the text is already displayed with the existing field, but that's basically how you'd do it.

Transfer a data set from openoffice base to calc

After I did a query in openoffice-base over a customized form I want to transfer a selected set of data into a template openoffice-calc table. I know I can access the data set in openoffice-calc via pressing the Data Source (F4) button but then I only get access over the query. The best solution would be after the database query over a form a button event is required to open a openoffice-calc table from the template and insert the data from the data set.
First go to Tools -> Macros -> Organize Macros -> LibreOffice Basic and add this code. Change the path of the template file.
Sub Copy_Record_To_Calc(oEvent)
Dim oForm
Dim templatePath As String
Dim oServiceManager As Object, oDesktop As Object
Dim oFileProperties As Object
Dim oDoc As Object, oSheet As Object, oCell As Object
Dim column As Integer
oForm = oEvent.Source.getModel().getParent()
If oForm.isAfterLast() Then
Print "Hey, you are after the last element."
Exit Sub
ElseIf oForm.isBeforeFirst() Then
Print "Hey, you are before the first element."
Exit Sub
End If
templatePath = "file:///C:/Users/JimStandard/Desktop/Untitled 2.ots"
Set oServiceManager = CreateObject("com.sun.star.ServiceManager")
Set oDesktop = oServiceManager.createInstance("com.sun.star.frame.Desktop")
Set oFileProperties(0) = new com.sun.star.beans.PropertyValue
oFileProperties(0).Name = "AsTemplate"
oFileProperties(0).Value = True
Set oDoc = oDesktop.loadComponentFromURL( _
templatePath, "_blank", 0, Array(oFileProperties))
oSheet = oDoc.Sheets(0)
For column = 1 to 2
oCell = oSheet.getCellByPosition(column - 1, 0)
oCell.String = oForm.getString(column)
Next column
End Sub
Then in form design mode, right-click on the button and choose Control. In the Events tab, click the three dots next to Execute action. Click Macro... and find the Copy_Record_To_Calc macro that you added.
Now turn design mode off. Go to a record and click the button. It will open the Calc template and copy the first two columns of the current record into column A and B of the spreadsheet.
See also:
Section 4.2.1 of Andrew Pitonyak's Base Macros (PDF)
ResultSet documentation
This thread gives an example of using a Calc template.

Disable/Enable button based on which control has focus

What I'm trying to do here is loop through the different controls and if any of the controls got focus to disable a button. This is a loop that I used for another task, so I'm kinda basing it off of this...
Dim cControl As Control
For Each cControl In Me.controls
If Not cControl.Container Is framClient Then
If Mid(cControl.Name, 1, 3) = "txt" Or Mid(cControl.Name, 1, 3) = "msk" Or Mid(cControl.Name, 1, 3) = "cbo" Then
'WHAT I'm trying to do here is
'if cControl.gotfocus then
'cmdExit.enabled=false
'end if ' but it barks at me
'cControl.Enabled = False
End If
End If
Next
I didn't make these text boxes in an array and I do not want to go through each text box to see if got focus and do whatever. Please take a look.
In vb6 only one control can have focus: the ActiveControl, so there's no point in looping and asking if the control has focus.
If you need to do something based on whether a certain control with a particular name has focus you could do this:
cmdExit.Enabled = (Me.ActiveControl.Name = "MyTextBox")
If you need to do it based on whether any TextBox has focus:
If TypeOf Me.ActiveControl Is TextBox Then
cmdExit.Enabled = false
Else
cmdExit.Enabled = true
End If
You can go ahead with Subclassing of text boxes as suggested in this SO Post.
As the post suggests, you have to do this exercise for every form where you have this requirement. I think, from your post that you have to do it only for one form.

Saving a composite datawindow in PowerBuilder

Is there a way to save results in a composite datawindow as a text or excel spreadsheet? Powerbuilder states for composites the format to save it is PSReport. That doesn't work for what I'm trying to do. Is there any other workaround for this issue?
A composite datawindow may contain any number of nested datawindows. So if memory serves, you cannot save the entire composite datawindow as data in a meaningful way (SaveAs will just give you a one-line bit of meaningless data), but you CAN save each of the nested datawindows inside the composite.
Here is some PFC code I wrote (for inside a menu item) which makes a copy of a nested report and then executes a SaveAs (dialog in this case):
//must create a 'dummy' datawindow,
//and put the data on the nested report into it
u_dw ldw_Temp
Window lw_Parent
String ls_Syntax, ls_Error
If Not IsValid(i_dwo) Then
MessageBox('Unexpected Error', &
'The pointer to the datawindow object was invalid. Contact Systems.')
Return
End If
If i_dwo.Type = 'report' Then
//continue
Else
MessageBox('Unexpected Error', &
'The pointer did not refer to a report. Contact Systems.')
Return
End If
If idw_Parent.of_GetParentWindow(lw_Parent) = 1 Then
ls_Syntax = i_dwo.object.datawindow.syntax
If lw_Parent.OpenUserObject(ldw_Temp) = 1 Then
If ldw_Temp.Create(ls_Syntax,ls_Error) = 1 Then
ldw_Temp.Object.Data.Primary = i_dwo.Object.Data.Primary
ldw_Temp.Event pfc_SaveAs()
Else
If IsNull(ls_Error) Or ls_Error = '' Then ls_Error = '<unknown error>'
MessageBox('Error','Error creating datawindow object: ' + ls_Error)
End If
lw_Parent.CloseUserObject(ldw_Temp)
Else
MessageBox('Error','Error creating datawindow control on ' + lw_Parent.ClassName())
End If
Else
MessageBox('Error','Unable to obtain pointer to the parent window.')
End If
Basically this code gets the syntax of the datawindow object underlying the nested report (ls_Syntax = i_dwo.object.datawindow.syntax above), then creates a datawindow control on the parent form and loads that syntax into it, and then copies the data from the one into the other. Finally it calls SaveAs on the copy, and the user is presented with a dialog asking where they want to save the data from the nested report and in what format.
You could automate this so that it saves each nested report as a separate file, and then if you like you could automate the formation of those separate files into a single file (sheets inside a workbook, appended text files, etc).
There are other ways to do what you are asking, depending on what exactly you are trying to accomplish.
The i_dwo variable was loaded in the right-mouse up even of the datawindow control (the dwo event variable there).
That may get you running, please ask questions if you have any.

Conditionally set OLE definition

In Clarion, I'm looking to set the definition for an OLE dynamically in a window. Right now, this is how I define two OLE objects in a window:
Window WINDOW('Test ActiveX Window'), AT(,,431,92), FONT('MS Sans Serif', 8,, FONT:regular), COLOR(COLOR:White), CENTER, ALRT(F2Key), TIMER(10), GRAY
OLE, AT(10,3,11,7), USE(?MyOLE1), HIDE, CREATE('MyActiveX.MyActiveXCtrl.1'), COMPATIBILITY(021H) END
OLE, AT(30,3,11,7), USE(?MyOLE2), HIDE, CREATE('SomeOtherActiveX.SomeOtherActiveXCtrl.1'), COMPATIBILITY(021H) END
END
Both objects contain the same method definitions (ex. TestMethod), almost like they implement a common interface. I can currently call them conditionally like this:
if (condition)
testStr = ?MyOLE1{'TestMethod(param1)'}
else
testStr = ?MyOLE2{'TestMethod(param1)'}
end
What I'd like to do is only create one object, and then dynamically set the control's definition. I know there are other ways to simplify this (OCX wrappers, etc), but based on the project's requirements, this is how it has to be done. At some point, this code will grow from 2 objects to many more and this will be a lot cleaner.
How can I dynamically set the control's definition (the MyActiveX.MyActiveXCtrl.1 part)?
Here is how to create an OLE object dynamically:
Data
MyOLE Long
Code
MyOLE = 0
MyOLE = Create(0, CREATE:OLE)
! Set the OLE's control definition dynamically:
if (condition)
?MyOLE{PROP:Create} = 'ACTIVEXIDTECH.ActiveXIDTechCtrl.1'
else
?MyOLE{PROP:Create} = 'SomeOtherActiveX.SomeOtherActiveXCtrl.1'
end

Resources