Add custom control to toolbox and have its properties show up in the properties window - vba

To illustrate what I'm asking, let's say I have a custom TextBox that has 2 modes. Mode 1 only allows numbers, and mode 2 only allows dates in a particular format.
Using a class module I can create this custom TextBox, and I can use a loop when the userform initialises to set which TextBoxes are custom.
What I'd like to happen is have the custom TextBox, or what ever custom control I want, show up in the toolbox. And I also want its custom properties, if they exist, to show up in the property window.
So far, I've been unable to find a way to do this. In fact, I've been unable to find out if it's even possible. It seems, to me anyway, that it's something that should be possible, but maybe I'm barking up the wrong tree. If it's possible I'd really appreciate being pointed to a resource.

Related

Trouble traversing Visual Tree in C++/WinRT

I'm implementing an app w/ Excel-style functionality where I have a ListBox of baskets, each containing a ListBox of Items, each containing a StackPanel-nested Button inside it.
XAML, What the layout looks like
Goal: I want to click on the button, such that the item entry (StackPanel) is highlighted via SelectedIndex(). I try to accomplish this in the GotFocus="ItemGotFocus" event handler, by traversing the visual tree to find the parent ListBox, so I can call SelectedIndex() on it.
However, I am getting errors whenever I call VisualTreeHelper::GetParent() or other functions from the namespapce in general:
Do I need to define a template definition somewhere, so it can figure out the type I want it to return, or is there a better way to go about this?
Thank you, it worked!
Turns out I had included <winrt/Microsoft.UI.Xaml.Media.h> in "pch.h", when what I really needed was <winrt/Windows.UI.Xaml.Media.h>.

VBA Adding a User Defined Control to Userform at Runtime

So I've made the following control in Design Mode then added the control to a new page Custom in my toolbox.
Now, what would I use for the progID parameter in Controls.Add()? How do I look up this value? Controls.Add("Forms.Frame.1") adds a regular frame, not the desired custom control.
As far as I am aware, you do not generate a new progID with a custom control. Per the MSDN documentation for custom/modified controls:
Note: When you drag a control onto the Control Toolbox, you only transfer the advanced property values.
So only the properties are transferred; the actual control is still of the same type/s as the one you created it from. Further, it looks like this progID is "a unique system-wide string that the Windows operating system can use to identify your control's type." So unless you are up to coding your own control, it looks as though you're just passing properties to the custom toolbox controls you create there.
You'll have to just replicate the custom control each time you want to add it. Just create a sub with all the correct properties and call it. Not what you were looking for, but it'll get the job done.

VBA OOP extending existing classes

I'm a rookie at VBA(Excel) but I know some OOP stuff from java. What I want to do is extend the class MSForms.TextBox to add my own constructors and properties. I've tried using the implements keyword but my class won't compile. It would also be helpful to add these fields into some kind of container variable since they're arranged in a 9x9 pattern. Here's what I want my custom textbox to do. (It's for a game)
I need to be initiate it and place it on the form and put initial text into the box based on what is passed to the constructor. I also would like to have it check the values at runtime with an event to make sure they comply with the game rules. If it does not comply I would like to change the background color to red.
I'm able to do all of this when I just make a bunch of form controls the old fashioned way and use a sub controlled by a button to check the text box values.
Any help on extending classes would be much appreciated.
Thanks
I don't think you can do that in VBA. I would keep two arrays, one array of TextBox and one array of a new class representing your extended properties. Then you keep them in sync. e.g. Textbox #71 and extended properties #71.
You can look at the following link for making and working with control arrays.
http://www.siddharthrout.com/index.php/2018/01/15/vba-control-arrays/

Create a shared copy and paste menu for my grids

I have 20 or so grids in my application suite. I'd like to create a global copy/paste context menu which I can bind to every single grid rather than code in each form.
I am unsure what is the best way to achieve this, I have started to create a class with my menu in it, but get stuck at the point of adding the actual menu options. For example I know I'll need to call a "copy" event, but I also know I'll need to tell it what I am copying, and I cannot see how that is done in vb.net when you can only add the address of a method minus parameters.
e.g.
.MenuItems.Add("Copy Cell", New System.EventHandler(AddressOf CopyCell))
Obviously I want "CopyCell" to only be coded in one place as well, rather than repeated in each form. I will always be copying the same object (SelectedCellCollection).
I am not sure how to make the menu have an event with parameters, or how to make it "know" that I want to always copy the selected items. I'm aware that I'd have to do some coding in the form but just trying to work out the way to minimize it.
I have created my own context menu class (via inheritance) with specific copy and paste functionality / options tailored to the grid I am using. It works fine and only needs one line of code per form/grid to activate.

combining very similar controls in silverlight

I have two controls. The XAML's are big and very similar. One difference is this: they contain a listbox, in one control, it's bound to {StaticResource X}, and is multiselect, the other is bound to {StaticResource Y}, and is not multiselect. The code-behinds are also very similar. How should I combine these two classes into one? I thought about creating a base class and deriving my 2 controls from it, but I have no idea how to do that with XAML. I know I could make it easier if I set the differing properties in code instead of XAML (in which case the XAML's would become identical), but let's consider that plan B. Silverlight has no StyleSelector, it seemed like a possible solution though. Maybe VisualStateManager could do it, except it sounds bad, because my problem has nothing to do with visuals, but maybe I could define 2 states anyway. Except I think SL doesn't support binding in style definitions. Tough question for a beginner like me...
You should look into creating custom controls and using AlternateContent properties. Look these up and you'll find hundreds of tutorials.
Here's a quick google search to get you started with alternate content.
So, to sum it up, I want one control which can work in somewhat different modes, or states. The mode can affect XAML properties and code logic, too.
It seems like VisualStateManager is very limited in which properties it can manipulate. But when the differences are only visual, it's the best choice.
When there are other differences in XAML, then the obvious choice is to omit those properties from XAML and set them in code, like in the ctor. A nicer way is to expose those properties as dependency properties in code, bind to those properties in the XAML of the user control, and then you can specify those properties in other XAML's where you use this user control. When your control doesn't care what's in those properties, then it's a good design choice, too. In my case, though, when setting up those differing properties should be the responsibility of the user control itself, not its parent, and I want to expose a single mode property only, it's not good.
For this case, the best way I found so far is this:
create a normal user control (XAML+code), expose the differing properties (simple, not DP's) and bind to them in XAML
make this user control abstract, and possibly some properties, too
for each different mode the control needs to support, derive a class from this base control (code only, no XAML), provide implementations for the abstract properties
instead of using the base control in other places, use one of the derived implementations
This way, you can easily specify from outside which mode you want your control to work in. The drawback is that it's not easy to change the mode, since it's not a property you need to change but the type and instance of the control.
And finally, when there are code logic differences, too, then one way is exposing a mode property, or using the abstract class method I described above. For example, a button click handler function can be abstract, too.