cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Questions on scripting a list box or combo box

seetheforest
7-Bedrock

Questions on scripting a list box or combo box

Hi all,

I need to make an automatically updating list box and I've provided an example worksheet to show how far along I am and where I would like to go. The biggest issue is that I just started learning VB today. I understand everything in the current script that I am using, which I got from a previous thread.

Here's my wishlist:

1. By default the listbox displays multiple options, but I would like to make it like a pulldown menu that shows a single option.

2. Currently the cursor selection value is given as the output. It would be handy to output the text or a value that the text represents.

I need Mathcad to do this because other users may modify the data in the worksheet and the listbox needs to respond to that change.

Please see the attached Mathcad worksheet and thank you in advance for any help or advice that you can provide. Even if someone could direct me to a place that I can go online to learn about scripting commands that would help me script better in Mathcad, that would be extremely appreciated.

ACCEPTED SOLUTION

Accepted Solutions

I am trying to take in two vectors as inputs. One vector containing a list of names and another vector that has numeric values for those names. I want to create a list box or combo box that mimics the "What I Would Like:" in the previous script that I posted. Basically that the listbox contains list of names and then outputs a unique value that is linked to each name (by way of a second vector that I can input into the list box.

ListBox.CurSel gives you the index into the list. So just pick off the correct entry in Values as the output:

Outputs(0).Value = Values(ListBox.CurSel), as you show below.

I saw that an array "ListBox" was created and populated with the list of names from a vector input.

ListBox is not an array, it is an object. In fact, it is the listbox object that is on the Mathcad worksheet.

I also noticed the hanging next as well, but I thought it had some use that was above my comprehension. I've deleted it from my script.

The problem was not that you had a Next, it's that you didn;t have two of them. All loops are terminated by a Next statement (just as all Ifs are terminated by an End If statement).

  • How to create the array: Dim Values()?
  • How to populate the array... a) 1-dimensional vectors: Values.Addstring(Inputs(1).Value(i)? b) From a 2-D array... no clue?
  • How to output the correct value: Outputs(0).Value = Values(ListBox.CurSel)?

Dim Values(). This creates the array, with an undefined number of elements

To populate it

Values = Inputs(0).Values

Your output statement will work fine.

I'm lost in translation, I think.

I hate to say it, but I think perhaps you are lost in the basics. I get the impression that you are not just new to VBscript, but new to programming in general. If you want to program in any language that works with objects (which is just about every modern language) then you need to understand what objects are. You also need to learn the syntax of the specific language: the different variable types, the syntax for loops, the syntax for conditional statements, etc. You need to get a book and read up on this stuff, because without the basics you will have difficulty even modifying existing scripts, let alone writing ones from scratch.

View solution in original post

18 REPLIES 18

Andrew Hirsch wrote:

...Even if someone could direct me to a place that I can go online to learn about scripting commands that would help me script better in Mathcad, that would be extremely appreciated...

Hello!

Maybe this will help you (Visual Basic page on Microsoft site): http://msdn.microsoft.com/en-us/vstudio/hh388568

RichardJ
19-Tanzanite
(To:VladimirN)

Maybe this will help you (Visual Basic page on Microsoft site): http://msdn.microsoft.com/en-us/vstudio/hh388568

VB comes in many flavors. There is VBscript, VBA, the older VB6, etc, and VB in Visual Studio. VB in Visual Studio is quite different to the others, and very different to VBscript.

Richard has uploaded many worksheets which may be of help. Please see link below.

http://communities.ptc.com/docs/DOC-1071#comment-1335

Mike

1. By default the listbox displays multiple options, but I would like to make it like a pulldown menu that shows a single option.

What you show in the second example is not a listbox, it's a combobox. Unfortunately, the only Mathsoft combobox control is a web control, which cannot be programmed. One option is to shrink the listbox and add a scrollbar (I didn't say it was a good option). The only other thing you could do would be to program a different combobox control, such as the MSForms 2.0 combobox. The problem with doing that is that the control will not work on any computer that does not have the corresponding ActiveX control installed (MSForms 2.0 controls are installed with Office). If you do that, bear in mind that the properties and methods of MSForms 2.0 controls do not have the same names as the Mathsoft controls!

2. Currently the cursor selection value is given as the output. It would be handy to output the text or a value that the text represents.

That's easy enough.

Please see the attached Mathcad worksheet and thank you in advance for any help or advice that you can provide. Even if someone could direct me to a place that I can go online to learn about scripting commands that would help me script better in Mathcad, that would be extremely appreciated.

Mike has pointed you to a document with many examples. Mostly, anyway. He actually pointed you to a comment, so here's a link to the document itself: Extra Components.mcd

You can read about VBscript here: http://msdn.microsoft.com/en-us/library/t0aew7h6

I also recommend the book "VBscript in a Nutshell", by Matt Childs et. al.

Richard Jackson wrote:

...I also recommend the book "VBscript in a Nutshell", by Matt Childs et. al...

It's available online on "Google books": http://books.google.ru/books?id=NLpuZSatG3QC&printsec=frontcover&hl=ru

Thanks for the help and replies. I went through everything that was posted as best I could and I took a stab at writing a script that could give the correct value corresponding to the menu item that was selected, but alas I failed.

First, I've kept my failed attempt at the script so you can see what I did... Please see the attached worksheet and the questions below. Be forwarned that it will throw an error at you when you open it because I kept my failed script in it.

1. Why does my script fail? I see that the array "ListBox" gets populated from Input(0).Value so I assume that the second input values would be Input(1).Value, correct? Maybe my output command is wrong? I'm clueless.

2. Is there a way to do this by using a single input that is a 2D array? I feel like that is more elegant.

3. This doesn’t necessarily apply to the current case, but I would like to hide the input and output variables to the menus if possible. In this case the user would only see the menu/pick box and not the variables that are associated with it.

4. In Extra Components, it looks like the ‘Nearest_Value’ example could be modified for what I need. Is this an example of the MS Forms 2.0 element that you were talking about?

5. I’ve elected to go with the dynamically filled list boxes script from the Extra Components worksheet. The other script I was using would throw an error from ‘GetText’ when the input array was changed.

Thank you so much for your help. I really appreciate it and I’m sorry if all of these questions are overwhelming. I just have a lot of questions when I don’t understand something.

1. Why does my script fail? I see that the array "ListBox" gets populated from Input(0).Value so I assume that the second input values would be Input(1).Value, correct? Maybe my output command is wrong? I'm clueless.

There is no object called "ValueBox". You can't just address objects that don't exist. Where did this come from, and what are you trying to do with it?

Also, your loop has no terminating "Next".

Yes, the second input is Inputs(1).Value. You can have up to 4 inputs.

2. Is there a way to do this by using a single input that is a 2D array? I feel like that is more elegant.

I don't know what you are trying to do, but yes, you can have a 2D array on an input.

3. This doesn’t necessarily apply to the current case, but I would like to hide the input and output variables to the menus if possible. In this case the user would only see the menu/pick box and not the variables that are associated with it.

Right click, "Hide Arguments"

4. In Extra Components, it looks like the ‘Nearest_Value’ example could be modified for what I need. Is this an example of the MS Forms 2.0 element that you were talking about?

Yes, that is an MSForms 2.0 Combobox.

5. I’ve elected to go with the dynamically filled list boxes script from the Extra Components worksheet. The other script I was using would throw an error from ‘GetText’ when the input array was changed.

Because you have nothing selected in the listbox, so GetText fails. You can fix this by ensuring something is always selected (which is what the piece of code that handles OnLoad does) or you can trap the error so it's ignored, and the output is set to something else.

Thank you so much. The right click --> Hide Arguments recommendation is such a fantastic, delightful tip.

On the subject of the script, I will expand as far as I can.

I am trying to take in two vectors as inputs. One vector containing a list of names and another vector that has numeric values for those names. I want to create a list box or combo box that mimics the "What I Would Like:" in the previous script that I posted. Basically that the listbox contains list of names and then outputs a unique value that is linked to each name (by way of a second vector that I can input into the list box.

So looking at the following code:

Rem Initialize List Box

ListBox.ResetContent()

Rem Initialize Selection If desired

ListBox.CurSel = 0

Sub ListBoxEvent_Start()

End Sub

Sub ListBoxEvent_Exec(Inputs,Outputs)

sel = ListBox.CurSel

ListBox.ResetContent()

For i = 0 to UBound(Inputs(0).Value)

ListBox.AddString(Inputs(0).Value(i))

ListBox.CurSel = se

Outputs(0).Value = ListBox.CurSel

End Sub

Sub ListBoxEvent_Stop()

Rem TODO: Add your code here

End Sub

Sub ListBox_SelChanged()

ListBox.Recalculate()

End Sub

Sub ListBox_DblClick()

ListBox.Recalculate()

End Sub

I saw that an array "ListBox" was created and populated with the list of names from a vector input. My thinking was that if I mimicked the steps for 'ListBox' with an array for values, dubbed 'ValueBox' then I would be able to pass a second vector as an input and populate that by changing the input from Input(0) to Input(1).

You are right that I didn't intialize any object called 'ValueBox', but that is just because I didn't see where 'ListBo'x was initialized. I was expecting to see "dim ListBox" somewhere that I would mimic to create my own new variable to populate.

I also noticed the hanging next as well, but I thought it had some use that was above my comprehension. I've deleted it from my script.

So in my mind I can either pass two vectors or pass a single array with the corresponding information. To be honest I would like to do it both ways just for the practice.

I just have no feel for....

  • How to create the array: Dim Values()?
  • How to populate the array... a) 1-dimensional vectors: Values.Addstring(Inputs(1).Value(i)? b) From a 2-D array... no clue?
  • How to output the correct value: Outputs(0).Value = Values(ListBox.CurSel)?

I'm a blind fool when looking at documentation on VB Scripts. Any information I glean from other people's examples seems to get lost in the specifics of what they are doing. I'm lost in translation, I think.

I am trying to take in two vectors as inputs. One vector containing a list of names and another vector that has numeric values for those names. I want to create a list box or combo box that mimics the "What I Would Like:" in the previous script that I posted. Basically that the listbox contains list of names and then outputs a unique value that is linked to each name (by way of a second vector that I can input into the list box.

ListBox.CurSel gives you the index into the list. So just pick off the correct entry in Values as the output:

Outputs(0).Value = Values(ListBox.CurSel), as you show below.

I saw that an array "ListBox" was created and populated with the list of names from a vector input.

ListBox is not an array, it is an object. In fact, it is the listbox object that is on the Mathcad worksheet.

I also noticed the hanging next as well, but I thought it had some use that was above my comprehension. I've deleted it from my script.

The problem was not that you had a Next, it's that you didn;t have two of them. All loops are terminated by a Next statement (just as all Ifs are terminated by an End If statement).

  • How to create the array: Dim Values()?
  • How to populate the array... a) 1-dimensional vectors: Values.Addstring(Inputs(1).Value(i)? b) From a 2-D array... no clue?
  • How to output the correct value: Outputs(0).Value = Values(ListBox.CurSel)?

Dim Values(). This creates the array, with an undefined number of elements

To populate it

Values = Inputs(0).Values

Your output statement will work fine.

I'm lost in translation, I think.

I hate to say it, but I think perhaps you are lost in the basics. I get the impression that you are not just new to VBscript, but new to programming in general. If you want to program in any language that works with objects (which is just about every modern language) then you need to understand what objects are. You also need to learn the syntax of the specific language: the different variable types, the syntax for loops, the syntax for conditional statements, etc. You need to get a book and read up on this stuff, because without the basics you will have difficulty even modifying existing scripts, let alone writing ones from scratch.

Richard Jackson wrote:

I hate to say it, but I think perhaps you are lost in the basics. I get the impression that you are not just new to VBscript, but new to programming in general. If you want to program in any language that works with objects (which is just about every modern language) then you need to understand what objects are. You also need to learn the syntax of the specific language: the different variable types, the syntax for loops, the syntax for conditional statements, etc. You need to get a book and read up on this stuff, because without the basics you will have difficulty even modifying existing scripts, let alone writing ones from scratch.

There's no problem in stating that Richard. It's true! I said in my first post that I'm completely new to VBScript. If you think that a knowledge of object oriented programming is basic, then I am lacking in the basics as well. In my world, I've been able to do everything that I need to do with c++, bash, or python scripts without needed to delve into the world of objects at all.

Thanks for the explanation on 'Next'. I probably should have just googled that because I didn't understand it but I would have never figured it as the termination of a for loop. Very unintuitive.

I may get a book on VBscripting. At the moment I am just trying to do something very small and relatively simple so I was hoping that I could just get some questions answered so I can move forward. I'll do my best to implement your advice.

In my world, I've been able to do everything that I need to do with c++, bash, or python scripts without needed to delve into the world of objects at all.

How can you program anything in C++ or Python (I am not familiar with bash) without knowing about object oriented programming? It's fundamental to both languages.

Bash is just a scripting language that I use to automate file movement and creation in my scripts. I think I might be using the word "programming" to losely... or you have a very different expectation to what the word entails. I just use those programming languages to write simple scripts, analyze data, or perform calculations. None of these have required me to use object oriented programming. I read a little bit on OOP for python before and all of the examples I read about were database management. My understanding is that an object is akin to a variable that you want to maintain based on various user inputs. So OOP hasn't been something that I have pursued just because I haven't had a need for it until now... and I didn't even realize that the VB script was using objects so this is my entrence into the subject.

From my experience it's easy to 'program' many things without having any knowledge of OOP. In the Python book I read, OOP didn't even get introduced until Chapter 18.

It's true that you can do a fair amount without objects, but I am surprised you have avoided them altogether. In Python, if you want to read or write a file you do it using a file object, which has read, write, open, close methods, etc (VBscript is similar: you use the scripting.filesystem object). If you call an ActiveX control, for example for data acquisition from some instrument or board, that's an object. If you use any user interface components such as buttons, etc, they will be implemented as objects. There are also mathematical operations that may have been implemented as objects (Matrix operations, FFTs are two I have seen that come to mind immediately). Objects are used for much more than just databases.

That's true. Up until I learned about objects initially I just thought that the file.command was just a convention for reading files. It really didn't require an understanding of what an object was... only that there was a different syntax convention for working with files. That type of thinking has obviously been a disadvantage to me now because I don't really have a knowledge of the differences between how you treat a variable or object. I'll get the hang of it over time though. I just didn't realize that the listbox inherently was an object and had a designated name in that script.

An object can store data, just as a variable can, but it is much more than just that. Perhaps this very brief description will help:

An object can have properties, methods, and events. It can also have child objects (and therefore a parent object). An object will have some of these, but not necessarily all of them. Also, an object is an instance of a class. A class may be predefined as part of the language (for example, the file class in Python), or you can create one yourself.

As an example, let's say we have a class called "cars", which represents (surprise!) cars. I can create an object, a car, based on that class. Let's say I call that object "MyCar". The object will have properties such as make, color, etc. Some of those are fixed when the object is created (such as make), others can be changed later. I can get or set the properties using the syntax MyCar.Color, MyCar.Make, etc (this syntax is not universal, but it applies to VBscript and Python). The properties are stored inside the object in local variables. The car also has methods, which make it do things. Examples might be MyCar.Accelerate and MyCar.Brake. It can also respond to events. So you can watch for the MyCar.Crash event, after which you may need to invoke the MyCar.Repair method . It can have child objects, for example "Wheels", which can have their own properties, methods, events, etc. So maybe you could get what the wheels are made of using MyCar.Wheels.Metal, which might return either "Steel" or "Alloy". Or, since there are four wheels, perhaps you can access them individually: MyCar.Wheels(i).Pressure for i =0 to 3 would return the pressure in each tire.

There are other important aspects of objects that are language dependent, but the above is the essence of them. One interesting one is that in Python all variables are in fact objects. If you haven't already read it,read "A Byte of Python": http://www.swaroopch.com/notes/Python. It's brief, to the point, and very importantly, free.

Thank you Richard.

I will definitely try to do my homework and read up on objects in the future. I'm currently on a job site so I am a little bit tied up in what I can do right now.

I'll look this up on my own, but are the methods and events that you speak of akin to functions? Do you pass any variables when you say MyCar.Accelerate? You would have to predefine (in the class?) what object.Accelerate means... correct?

I will definitely try to do my homework and read up on objects in the future. I'm currently on a job site so I am a little bit tied up in what I can do right now.

I really recommend "A Byte of Python". It introduces these concepts, and it's a very short book. If you are already familiar with Python you could probably read the whole thing in a few hours.

I'll look this up on my own, but are the methods and events that you speak of akin to functions? Do you pass any variables when you say MyCar.Accelerate? You would have to predefine (in the class?) what object.Accelerate means... correct?

You could pass arguments to a method, yes. ListBox.AddString is an example. Events are something you respond to, such as ListBox.SelChanged, which is fired when the user changes the selection, so they don't have arguments. You are correct that the behavior of methods and events are defined in the class definition.

Thanks for all of your help Richard. I'd just like to share that I got the script working! I've attached the working file.

One curious thing I noticed was when I tried to port the code to the Combo Box I got an error because the MS Forms 2.0 Combo Box does not support the same methods. Is there some place that I can go to to see what Methods are available for the Combo Box? How does one know what methods they can and cannot use when they run into a new object in the wild?

Announcements

Top Tags