By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 865 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

Call Method On Form Load

100+
P: 233
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
Share this Question
Share on Google+
8 Replies


100+
P: 233
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

100+
P: 233
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
Expert 2.5K+
P: 3,256
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

100+
P: 233
@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
Expert 2.5K+
P: 3,256
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

100+
P: 233
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
Expert 2.5K+
P: 3,256
Well, you might want to start a new thread and play the sound there rather than the GUI thread.
Jan 16 '09 #8

Plater
Expert 5K+
P: 7,872
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

Post your reply

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