Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

faking input to a form 1

Status
Not open for further replies.

eward1

Programmer
Feb 24, 2003
18
US

I am trying to simulate someone pressing a button on another form in my program.
When data comes in on the serial port I can read it. Based on what the data is, I need the program to act as if the user pressed a button on a given form which is visible, but does not currently have focus.
Any ideas?
 
On the active form, declare and instantiate the second form. Then execute code that performs the click event of the button on the other form. See the following example code...

' This is the code for the current form...

Private frm As New frmNonCurrent()

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CType(frm.Controls(0), Button).PerformClick()
End Sub

Private Sub frmCurrent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
frm.Show()
End Sub
 
Hello,

Were you able to get this to work using the example illustrated by obrienb? I'm attempting to do the same thing using vb.net, but can't get it to work.

What version of VB was this actually for?


Thanks

 
I ended up using something very similar. But my form was already up, and I have created my own collection of forms open in this project, so I searched my collection for the proper form and then called the correct Button_Click handler on that form.
For this to work I had to get the correct buton as sender.

Dim sender as Object = Nothing
Dim sNameOfButton as String = "PutNameHere"
For I as Integer = 0 to Me.Controls.Count - 1
If Me.Controls.Item(I).Nmae.ToLower = sNameOfButton
sender = me.Controls.Item(I)
Exit For
End If
End For

Then get the eventargs

Dim e as System.EventArgs

These are the 2 parameters needed to pass to your Button_Click Handler.

I have used this in VB.NET with both 1.0 and 1.1 Framework.

Hope this helps!
 
Thanks for the reply.. but I'm a bit confused...

So if I have form1 and within this form I have a text-label1. The affect I'm looking is the following:

Every time that form1 is run, I want to display a value in "text-label1" automatically without having to actually click the field in order for this value to showup in "text-label1" on form1.

The question is, where would you get the information regarding the sender example you kindly shared with me?

Any hint in the right direction would be very much appreciated.

Thanks again..
 
If I understand your question correctly, you would put the name of your control that puts the value in "text-label1" into sNameOfButton. In your application that may be "text-label1" itself or another button on form1.

Hopefully this is helpful. If not, and you can send me a zipped copy of the code you are trying, I will set it up and send it back.
 
Thanks for the offer... for some reason I can find an option where to enclose files in this form to send you the zipfile. But, following is the form1.vb code I'm trying to send you along with all the other files. I guess the most relavent stuff is at the end:

Thanks again..

P.S. Once I figure out how to attach a file in this forum, I can send you the zip files.

---Start----------

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(96, 96)
Me.Label1.Name = &quot;Label1&quot;
Me.Label1.Size = New System.Drawing.Size(104, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = &quot;Account Number:&quot;
'
'Label2
'
Me.Label2.BackColor = System.Drawing.SystemColors.Info
Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Label2.Location = New System.Drawing.Point(232, 88)
Me.Label2.Name = &quot;Label2&quot;
Me.Label2.Size = New System.Drawing.Size(96, 24)
Me.Label2.TabIndex = 1
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(40, 136)
Me.Label3.Name = &quot;Label3&quot;
Me.Label3.Size = New System.Drawing.Size(440, 96)
Me.Label3.TabIndex = 2
Me.Label3.Text = &quot;The effect I would like is to automatically have the account number appear when t&quot; & _
&quot;he form come up for the first time. In this case you have to click in the box ab&quot; & _
&quot;ove in order to have the account number appear&quot;
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(488, 333)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label3, Me.Label2, Me.Label1})
Me.Name = &quot;Form1&quot;
Me.Text = &quot;Form1&quot;
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
Dim AccountNumber = &quot;12345678&quot;
Label2.Text = AccountNumber

End Sub
End Class

---------End-----------------
 
Maybe I am over-simplifying your problem, but from what I think you are trying to do, here is what you ned:

If your program already knows the AccountNumber before this form comes up, then just put the line
Label2.Text = AccountNumber
in the Load handler (Form1_Load.)

On the other hand, if Form1 has to get the data from a database or spreadsheet then the best way is to Bind Label2 to the DataSource.

If this helps, great. If not I need to know where you will be getting AccountNumber from.
 
Actually, I was over complicating the problem.. That worked perfectly.

Thanks very much for all your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top