473,505 Members | 15,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call Method On Form Load

233 New Member
I am certain this is simple, but I am not used to working with forms. I have a very simple app that I want to have load a form containing a button, and then I want to call a method that will play a sound. My question is where do I call the method?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public partial class Form1 : Form
  3. {
  4. public Form1()
  5. {
  6. InitializeComponent();
  7. PlaySound();
  8. }
  9.  
This code plays the sound, but does not bring up the form.
Jan 15 '09 #1
8 12916
mcfly1204
233 New Member
Nvm. Highlighted the form in VS, selected the action button, and then selected the method I wanted to run for Load.
Jan 15 '09 #2
mcfly1204
233 New Member
I spoke too soon. The sound now plays, but the form never appears on the screen. Considering I am just trying to have the simple form with one button appear, and have the sound play until the button on the form is selected, what do I need to do here?
Jan 16 '09 #3
Curtis Rutland
3,256 Recognized Expert Specialist
In your designer window, double click on the form's title bar. This will auto generate an event handler method for the Load event. Put your code there.
Jan 16 '09 #4
mcfly1204
233 New Member
@insertAlias
I should clarify. I understand how to generate an event handler for the Load event, but while the Load event executes, the form never actually appears. So I hear the sound repeating, but I do not see the form.
Jan 16 '09 #5
Curtis Rutland
3,256 Recognized Expert Specialist
So it doesn't work in the Load event either? I thought the problem might be because you called the method in the constructor, not the Load handler.

Let me think about this and I'll get back to you.
Jan 16 '09 #6
mcfly1204
233 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2. namespace EmailAlert
  3. {
  4. public partial class EmailAlert : Form
  5. {
  6. public EmailAlert()
  7. {
  8. InitializeComponent();
  9. }
  10. private void button1_Click(object sender, EventArgs e)
  11. {
  12. Application.Exit();
  13. }
  14. private void EmailAlert_Load(object sender, EventArgs e)
  15. {
  16. int i = 0;
  17. while (i < 200)
  18. {
  19. SoundPlayer simpleSound = new SoundPlayer(@"c:\Windows\Media\chimes.wav");
  20. simpleSound.Play();
  21. Thread.Sleep(5000);
  22. i++;
  23. }
  24. }
  25. }
  26. }
  27.  
With this line added to InitializeComponent:

Expand|Select|Wrap|Line Numbers
  1.  
  2. this.Load += new System.EventHandler(this.EmailAlert_Load);
  3.  
Jan 16 '09 #7
Curtis Rutland
3,256 Recognized Expert Specialist
Well, you might want to start a new thread and play the sound there rather than the GUI thread.
Jan 16 '09 #8
Plater
7,872 Recognized Expert Expert
Yes the sound should be played in a seperate thread (actually I thought generally the sound interop was non blocking and you made like mms calls or whatever)
Anyway, you should also have an Application.DoEvents() inside that for loop to keep it from being a "busy loop". Although the thread.sleep will release CPU time, it doesn't allow the form to processes windows messages.

If your sound is short enough, adding that Application.DoEvents() might be enough to solve your problem
Jan 16 '09 #9

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

Similar topics

3
4016
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: ...
3
11857
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...
4
4272
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...
1
2616
by: keith.walter | last post by:
My first asp.net app is almost "done" and I am stuck. Here is my situation: I have a "mother" page add_customer.aspx and a"child" user control add_group.ascx. On the mother page is an "add...
33
1829
by: Partha Protim Roy | last post by:
Hello, I have a Customer form say A to enter/update customer details. In the Form A I have a button which opens another form say B. In the Form B, I am providing user with a option to search...
7
10039
by: Steve_Black | last post by:
Hello, I'm toying with the idea of loading a MenuStrip (VB.Net 2005) dynamically based on who is logged into my system. Every user has different security settings and I want to customize the...
6
3613
by: roland.bali | last post by:
Hi, Here is the basic setup, my base class is Shoe which has a child class called Sandal. I would like to create objects by calling Sandal.Load. But without overloading Load in Sandal and...
5
1688
by: tcomer | last post by:
Hello, I'm working on an application that grabs some data from the web via HttpWebRequest. I'm using a local objects method to get the data, but the problem is that my form doesn't load until...
0
7216
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.