473,383 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

How to call an event in one form from another?

My problem is that on button click of one form(Form1) I want to change the background color of checkbox in another form(Form2) ..One way I found is to call the form and pass the variable through constructor..Bt I don't want to create new form...The form in which I want 2 send the key is already opened n I don want to call the constructor.. Is it possible to call an event on form2 through the pressing of button on form 1....If yes then how?
*****I am working on windows application in C# .NET********
Sep 5 '08 #1
6 2840
balabaster
797 Expert 512MB
There are a couple of ways you could do this...in the constructor of Form2 you could pass Form1 as a parameter...and hence you'd be able to access it's properties. Another way is to declare a public property in Form2 called parent which accepts the type Form and pass Form1 into it... again, you'd be able to call it's properties. The other way is with the use of singletons... I've never really found much use for singletons so I don't know all the ins and outs, but essentially it is this.

There is also a property "Parent" that will return a reference to the parent form (assuming you open Form2 as a child of Form1) - if not you can mimic this behaviour yourself easily enough:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.   Private _Stuff As Boolean
  4.   Public Property Stuff() As Boolean
  5.     Get
  6.       Return _Stuff
  7.     End Get
  8.     Set (ByVal Value As Boolean)
  9.       _Stuff = value
  10.       RaiseEvent ValueChanged(Me, New System.EventArgs)
  11.     End Set
  12.   End Property
  13.  
  14.   Public Event ValueChanged(ByVal Sender As Object, ByVal e As System.EventArgs)
  15.  
  16.   Public Sub Form2Loader()
  17.  
  18.     Dim oFrm2 As New Form2()
  19.     oFrm2.Show(Me)
  20.  
  21.   End Sub
  22. End Class
Form2 Class
Expand|Select|Wrap|Line Numbers
  1. Public Class Form2
  2.  
  3.     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  4.  
  5.         CType(Me.Owner, Form1).Stuff = CType(sender, CheckBox).Checked
  6.  
  7.     End Sub
  8.  
  9. End Class
Sep 5 '08 #2
There are a couple of ways you could do this...in the constructor of Form2 you could pass Form1 as a parameter...and hence you'd be able to access it's properties. Another way is to declare a public property in Form2 called parent which accepts the type Form and pass Form1 into it... again, you'd be able to call it's properties. The other way is with the use of singletons... I've never really found much use for singletons so I don't know all the ins and outs, but essentially it is this.

There is also a property "Parent" that will return a reference to the parent form (assuming you open Form2 as a child of Form1) - if not you can mimic this behaviour yourself easily enough:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.   Private _Stuff As Boolean
  4.   Public Property Stuff() As Boolean
  5.     Get
  6.       Return _Stuff
  7.     End Get
  8.     Set (ByVal Value As Boolean)
  9.       _Stuff = value
  10.       RaiseEvent ValueChanged(Me, New System.EventArgs)
  11.     End Set
  12.   End Property
  13.  
  14.   Public Event ValueChanged(ByVal Sender As Object, ByVal e As System.EventArgs)
  15.  
  16.   Public Sub Form2Loader()
  17.  
  18.     Dim oFrm2 As New Form2()
  19.     oFrm2.Show(Me)
  20.  
  21.   End Sub
  22. End Class
Form2 Class
Expand|Select|Wrap|Line Numbers
  1. Public Class Form2
  2.  
  3.     Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
  4.  
  5.         CType(Me.Owner, Form1).Stuff = CType(sender, CheckBox).Checked
  6.  
  7.     End Sub
  8.  
  9. End Class



Thank you sir,
Now I am trying to do this
Sep 6 '08 #3
balabaster
797 Expert 512MB
Thank you sir,
Now I am trying to do this
No problem, I hope it was useful...
Sep 7 '08 #4
No problem, I hope it was useful...


But one problem is coming I am using usercontrol as form1(in which I want to change the background color of checkbox) and System.Windows.Forms.Form as form 2(from which I want to raise an event of usercontrol(Form1)). Form1 and Form2 are not related to each other,means they are parent and child...
The coding you gave is related to the concept of parent and child...and it is not working ..I have tried a lot...
One thing I tried is I used a label in User Control(Form1) and I want to change the value of that label from Form2.Now in Form1 on the event LABLEVALURCHANGED I want to change the Background color of the checkbox.....To accomplish this task I want to assess the object of the Form1 which is opened at the instance...The main problem I am facing is that I am not able to find how to use that opened instance of the Form1
Sep 7 '08 #5
jg007
283 100+
if I understand this is quite straightforward and I have given as really quick example below which just needs 2x forms 2x buttons on the first one and a checkbox on the second one

lol you can change the colour to whatever u want !

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class Form1
  3.  
  4.     Dim checkcol As System.Drawing.Color
  5.  
  6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  7.         checkcol = Color.Blue
  8.         Form2.CheckBox1.BackColor = checkcol
  9.         Form2.Show()
  10.  
  11.     End Sub
  12.  
  13.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  14.         checkcol = Color.Chocolate
  15.         Form2.CheckBox1.BackColor = checkcol
  16.         Form2.Show()
  17.  
  18.     End Sub
  19. End Class
  20.  
  21.  
Sep 7 '08 #6
jg007
283 100+
just realised that this is c# the concepts may be similar but I have not got c# installed to check this.
Sep 7 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
3
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a...
13
by: jac | last post by:
Hae, I have a windows form with a ComboBox an other things. On that combobox I have an eventhandler on de selectedindexchanged. But somewhere in my code want to do excecute the same code that...
4
by: John | last post by:
Hi all, This really is quite an urgent matter. I have a page with multiple, dynamically-loaded user controls and when a user clicks on a button, the whole form is submitted. Now at this stage...
3
by: Borr | last post by:
Hi, I have an ASP .NET page, that runs client side timer that does something on the Server side and after that loads another page. So I have on client side something like : function...
6
by: grist2mill | last post by:
I want to create a standard tool bar that appears on all pages that is a control. The toolbar has a button 'New'. What I wolud like when the user clicks on 'New' depends on the page they are on. I...
8
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.