Dynamic pushbutton Control set object is getting null when trying to invoke using shortcut key

Hi All,

I have question answer form and I have added below the line of code to add dynamically push button using below lines of code.

Note: ansList - it could be any number

For ansOptions as Integer = 0 To ansList.Ubound
dim b as new PushButtonAnswerSet
b.Visible = true
b.Left = ((ansOptions + 1) *(PushButtonAnswerSet(0).Width + 22)) - 94
b.Top = 190
b.Width = 95
b.Height = 30
b.Bold = true
b.Caption = ansList(ansOptions) + “(” + Str(ansOptions + 1) + “)”
b.Index = ansOptions + 1

mAnsControlsAdded = ansList.Ubound + 1[/code]

mAnsControlsAdded : Defined a property in a module

and when the user clicks the push button, automatically Action handlers will trigger and it moves to the next question. And also I have added a shortcut so that if the user presses the particular key, automatically action event will trigger. So I have added a key down event in the form and make the Action handlers code into a common module so that it will use multiple places(Action handlers and Shortcut key).

And before moving to the next question I have below lines of code to remove the current button

//Remove current button for i as Integer = mAnsControlsAdded DownTo 1 PushButtonAnswerSet(i).Close next

So my question is:
when I click the push button it will remove the current button using the above lines of code and binding the next questions with binding the dynamic button again. However, when I click through the shortcut, the above code is not working and giving a null object exception.

Here PushButtonAnswerSet(i) object is always null whenever I click through a shortcut. Any idea?

Any help would be greatly appreciated.

Where is mAnsControlsAdded defined ? I do see it only once in your question.

You do not delete PushButtonAnswerSet(0) on purpose, I suppose (so you can clone it later).

Have you read that page:


Thanks EMile.

I have updated my post. I have defined the mAnsControlsAdded below the loop of dynamic button loop.

mAnsControlsAdded = ansList.Ubound + 1

mAnsControlsAdded : Defined this property in a module.

PushButtonAnswerSet(0): Every time it will be a different set of push button based on the question and answer.
For example- Question 1: 2 dynamic push button will create and for Next Question 2: it will be 3 dynamic push button with a different caption.

Add a Breakpoint (to stop in the debugger) before the Close loop lines and watch how many entries in the Control Set.

With that information, you will understand what happens.

I’ve created a simple example that fills 5 entries in the COntrol set, but provide 7 entries in the “array”: the empty entries are Nil.

I have a PushButton control set and dynamically pushbutton entries will be created based on the answer options available.
and then click any of the answer buttons, I am able to see the number of entries for push-button control set object like pushbuttonanswerset(0), pushbuttonanswerset(1), etc (2 answers available) in debug mode.

However, when using the short cut and invoke the same method, I am NOT able to see the control set instance(like pushbuttonanswerset(0) and pushbuttonanswerset(1) ) based on the answer options in debug mode.

Any idea why it is NOT creating an object/instance of the push button set when using any shortcut key?

I’m not sure why you aren’t able to find the button from the keydown event, here’s a simple example of it working, maybe you are doing something different?


Thanks Julian for your example.

Sorry for the confusion. I am able to find the key down event.

My question was: I have a PushButton control set and dynamically creating multiple pushbutton entries based on the answer options available. Take an example I have 2 answer options for question 1 then it will create 2 push button set(pushbuttonanswerset(0), pushbuttonanswerset(1)) for question 1.
Once I click any of the answer push-button, it will move to the next question 2 and again it will be created multiple push-button answer set based on the options and it is working as expected and I can see the push button object in debug mode.

I wanted t the same thing when the user clicks any key down event. However, when I press the key down event and I am NOT able to see the control set instance(like pushbuttonanswerset(0) and pushbuttonanswerset(1) ) based on the answer options in debug mode. It is giving always null.

Any idea?

If you want, PM me a copy of your code and I’ll try to point you in the right direction.

Hi All,

I am closing this thread. Thanks for your valuable inputs.
@Julian: I have sent a private message with the sample project. Please verify once. We will communicate using the private thread.