The last 2 articles in this series discussed creation of a Custom Form and addition of a Text Box Control to the form. This article shows how to add a Button control to the form using DOTNetFactory. Since most of the basics have been discussed in the previous articles, this one will be quite fast-paced.
Similar to creating a Form and Text Box, we will use .CreateInstance to create a Button Control.
Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms")
Also, just like a Text Box, the button will have to be positioned somewhere in the form, thus, we will again define a Point object:
Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y) With oPoint .x = 20 .y = 50 End With oButton.Location = oPoint
When adding a Button, you might also want to add some Text to it, so its easily distinguishable:
oButton.Text = "Close Form"
In this example, the button we are adding will be a Cancel button type that will exit the form once clicked.
oForm.CancelButton = oButton
Putting it all together
Let’s add a Text Box Control, and use QTP to output the entered Text using MsgBox.
Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") Set oButton = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms") Set oText = DotNetFactory.CreateInstance("System.Windows.Forms.TextBox", "System.Windows.Forms") Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point", "System.Drawing", x, y) 'Text Box With oPoint .x = 20 .y = 20 End With With oText .Location = oPoint .Width = 100 End With 'Button With oPoint .x = 20 .y = 50 End With With oButton .Location = oPoint .Width = 100 .Text = "Close Form" End with 'Add Controls With oForm .Controls.Add oText 'Adding a Cancel Button .CancelButton = oButton .Controls.Add oButton End with 'Show Dialog oForm.ShowDialog 'Output the entered Text: MsgBox oText.Text
Executing the code above will display a form control:
I entered Relevant Codes in the text box and clicked the button to get the following output from QTP:
In the next topic in this series, we will learn how to create a CheckBox control using DOTNetFactory.
I hope you find this article useful.
In this Series
- QTP & DOTNetFactory – Creating a Custom Form
- QTP & DOTNetFactory – Adding a Text Box Control
- QTP & DOTNetFactory – Adding a Button Control {Currently viewing}
- QTP & DOTNetFactory – Adding a CheckBox Control
- QTP & DOTNetFactory – Creating a Custom Form with Multiple Objects
- QTP & DOTNetFactory – Adding a RadioButton
- QTP & DOTNetFactory – Adding a ComboBox – Coming Soon!
{ 4 comments… read them below or add one }
Hi All,
Can anyone tell me, If a form consist more than 10 button with the different name. Here how to identify which button is clicked.
Thanks in advance
Hi Anshoo
I have created a form using dotnetfactory and added a listbox object and textbox object.say on selectiing a particular value from the list box,i wud like to have the selected listbox value to be displayed in my textbox object
Hello,
I think the articles are great. They helped me a lot with using custom controls in QTP.
I also have a question regarding buttons. Lets assume we have a button which is not assigned to form action(like in the example assignment is to oForm.cancelButton):
Is it possible to detect click on “Open another dialog” button? OR i need to assign it to another form action i.e.
Hi Picia,
Thank you for your kind words. I’m happy that you found them useful.
To answer your question, yes, it is possible to create such a scenario. The only extra bit here is creating a “DialogResult” object to retrieve the result of the buttons that are added to the form. Please see below:
Set oForm = DotNetFactory.CreateInstance("System.Windows.Forms.Form", "System.Windows.Forms") Set oButtonOK = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms") Set oButtonCancel = DotNetFactory.CreateInstance("System.Windows.Forms.Button", "System.Windows.Forms") Set DiagReturn = DotNetFactory.CreateInstance("System.Windows.Forms.DialogResult", "System.Windows.Forms") Set oPoint = DotNetFactory.CreateInstance("System.Drawing.Point","System.Drawing", x, y) With oPoint .x = 5 .y = 5 End With With oButtonOK .Text = "OK" .Location = oPoint .DialogResult = DiagReturn.Ok End With With oPoint .x = 5 .y = 35 End With With oButtonCancel .Text = "Cancel" .Location = oPoint .DialogResult = DiagReturn.Cancel End With With oForm .Height = 500 .Controls.Add oButtonOK .Controls.Add oButtonCancel .ShowDialog End With If oForm.DialogResult = DiagReturn.Ok Then 'Code to open another form MsgBox "Opening another form.." End If If oForm.DialogResult = DiagReturn.Cancel Then 'Code to do something else MsgBox "Doing something else.." End If Set oForm = Nothing Set oButton = Nothing Set DiagReturn = Nothing Set oPoint = NothingI hope this helps..
{ 4 trackbacks }