Embedding sounds in C# - c#

I know there are a lot of threads with this same topic, but for a reason I don't understand yet, this is not working for me.
I have this project tree:
I embedded the alarm.wav to the .resx file from the Project->Properties->Resources menu.
I tried different combinations of code but nothing works.
At the moment this is the code I'm trying.
using System;
using System.Media;
using System.Windows.Forms;
using System.Threading;
using System.Globalization;
using System.ComponentModel;
using System.Resources;
using AlarmForm;
namespace Alarm
{
public partial class Form1 : Form
{
private bool estado = false;
private SoundPlayer sonido;
public Form1()
{
InitializeComponent();
ResourceManager resources = new ResourceManager(typeof(Form1));
sonido = new SoundPlayer(resources.GetStream("alarma"));
}
}
}
No error is displayed during the compilation or the runtime, but instead of the sound an error beep is heard.
Edited: Error I found trying to use Alarm.Properties

Why are you trying to use resources.GetStream() while you can link the file directly using Alarm.Properties? I believe that it'd be much easier. I see that you've also forgot to play the Sound file linked to sonido which represents a new SoundPlayer. Here's a simple example that shows how to use SoundPlayer
Example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Media;
using AlarmForm.
using AlarmForm.Properties; //Required to call 'Resources' directly
namespace Alarm
{
public partial class Form1 : Form
{
private bool estado = false;
private SoundPlayer sonido;
public Form1()
{
InitializeComponent();
//ResourceManager resources = new ResourceManager(typeof(Form1)); //We do not actually need this
sonido = new SoundPlayer(Resources.alarma); //Initialize a new SoundPlayer linked to our sound file (or Alarm.Properties.Resources.alarma if Alarm.Properties was not imported)
sonido.Play(); //Required if you would like to play the file
}
}
}
Notice that: You may stop the SoundPlayer from playing anytime by doing sonido.Stop() since sonido which represents a new class of name SoundPlayer was defined under public partial class Form1: Form UNLESS if the void that is trying to call sonido is static.
Thanks,
I hope you find this helpful :)

Related

Control does not exist in current context

I'm sure this is something simple that I am just overlooking, but I can't figure it out. I am new to C# and I'm trying to create a calculator application. I have created my form with all of my buttons/textbox on it. Now I'm creating a new class to handle all of my methods and whatnot. My problem is that whenever I'm trying to reference controls on the form in the second class, I get the "does not exist in the current context" error. How can I solve this?
An example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public class Calculator
{
decimal currentValue = Decimal.Parse(displayValue.text);
}
}
displayValue receives the error. Thank you for any help.
The Controls can be called only from the .cs file which is linked with the form controls.
What you can do is create a parameterized constructor of your Calculator class like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public class Calculator
{
public Calculator(string displayValue)
{
decimal currentValue = Decimal.Parse(displayValue);
}
}
}
Now, you can call this class in the form .cs where you have buttons and textboxes like this:
Calculator calculate = new Calculator(displayValue.Text);
When you create a new Windows Form Application, there's a designer (which you can interact with to add your buttons and textboxes) and the code-behind (a .cs file).
This .cs file is a partial class, meaning it is also defined by the form you are interacting with. (you can see the nitty gritty details in your .Designer.cs file)
Once you name your buttons and textboxes, you can refer to their names in the code in your partial class!
And when you compile this, your button text will change to "Hello World!"
Hope this helps.
You will find this control in partial class of the form. You can take values from it and do your operation
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string s = textBox1.Text;
}
}

How to use Messagebox in class library c#?

How to use MessageBox in class library?
Here is my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MessageBoxes
{
class ShowInfo
{
MessageBox.Show("test");
}
}
i can load MessageBox but can't have show property, MessageBox.Show("test"); <-- fail
You should NOT use a Windows forms MessageBox inside a class library. What if you use this library in an ASP.NET application. The MessageBox will be shown in Webserver. And your webserver will be waiting (hung) untill someone responds to that MessageBox in webserver.
An ideal design would be that you either return the message as string and deal with that string in caller specific way or throw an exception if thats what you want.
If you still want then here is your code corrected
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MessageBoxes
{
class ShowInfo
{
public void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
}
}
You have the call to messagebox outside any method.
This code cannot be compiled at all.
You should write
namespace MessageBoxes
{
class ShowInfo
{
public void ShowUserMessage(string messageText)
{
MessageBox.Show(messageText);
}
}
}
and then call it after instancing an object of type ShowInfo
ShowInfo info = new ShowInfo();
info.ShowUserMessage("This is a Test");
Additional Answer to this Question:
After the Class Library Project has been created.
Right Click your Project Add > New Item > Windows form
it's done by adding reference System.Windows.Forms.dll
Make sure you actually use the the class in the main form.
class ShowInfo
{
public static void show()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
...
public Form1()
{
InitializeComponent();
ShowInfo.show();
}

How can I play the background sound in c#

I already follow the tutorial of adding sound in the c# but it cannot play the sound, but it is no error. How can I do?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SoundPlayer myPlayer = new SoundPlayer(WindowsFormsApplication2.Properties.Resources.sound1);
myPlayer.PlayLooping();
}
}
}
Make sure that you have the Form1_Load() assigned to the Form Load event. I've put in your code, and it works just fine.
To assign the event, just look at the following image..
This will ensure that the Form1_Load() is called when the form loads.
After having tested to see what would happen when you try to pass a non-.WAV file to the SoundPlayer, it WOULD produce an error:
Error 1 The best overloaded method match for 'System.Media.SoundPlayer.SoundPlayer(string)' has some invalid arguments.
This leads me to believe the code isn't being called.

How do I take Hello world console app and refrence the text for display in a WinForm Label?

Basically, I'm trying to move the contents of WriteLine to a Label box in a WinForm, as an intro to object oriented programming. I believe I have some syntax error, and I know the method I have the writeline in is void. So, any help with getting this to work is appreciated. This is just one of the attempts I've made.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleHelloWorld.Program;
namespace WindowsFormHelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string words = new ConsoleHelloWorld.Program.Main(words);
label1.Text = words;
}
}
}
This is the code I'm referencing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
public static class Program
{
public static void Main(string[] args)
{
string words = "hello world";
Console.WriteLine(words);
Console.ReadLine();
}
}
}
Your words variable is a local one, i.e. its scope is the method where it is declared, that is Main. Outside that method you cannot reference it.
For the variable to be accessible, you need to make it a public field (or a property of the class). If you need to access it without having an instance of that class type, than this field should be declared static. Then your code would look like this:
public static class Program
{
public readonly static string Words = "hello world";
//...
}
Assuming the windows application project has a reference to the console application project in the windows application form you may write:
string words = ConsoleHelloWorld.Program.Words;
Also you don't need to launch the console application, besides you won't manage to do it like this (consider the answer by Cuong Le, if you meant to launch both Console and Windows applications).
Got it to work. Turns out I was rooting down incorrectly. (I had thought rooting worked a bit more like the android file structure.) Basically, I created a separate class for holding the Hello World variable, and just called that for whether I wanted to print the strings to a console or WinForm app.
The initial block of code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleHelloWorld
{
public static class Program
{
public static class Hello
{
public static string words = "Hello World";
}
public static void Main(string[] args)
{
string word = ConsoleHelloWorld.Program.Hello.words;
Console.WriteLine(word);
Console.ReadLine();
}
}
}
The WinForm calling the class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ConsoleHelloWorld; //Program.Hello;
namespace WindowsFormHelloWorld
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string word = ConsoleHelloWorld.Program.Hello.words;
label1.Text = word;
}
}
}
In future projects I'll just keep the code being run by various environments in it's own project.

C# Application.ApplicationExit creates need to click 'X' twice

I have had a fairly thorough look around, but couldn't find anything related, so I have decided to ask here :)
I have created a WinForm in Visual Studio, and in its code it has a fair few functions. At this point its all working nicely. I then go and put
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
in the constructor function and when I go and click the little cross in the corner, I have to click it twice for it to close! I would put the code in here, but its 240 lines, so its kinda huge.. If you need to see it though, I can put it up.
Thanks in advance!
Dronnoc
The ApplicationExit event is automaticly called on clicking the cross. So there is actual no need to call it. What do you want to do on closing?
If you want some action between clicking the cross and shutdown, you have to call the FormClosing() event.
I have solved it peoples!
I had a ListBox on the page, and a function running when the SelectedIndex changed. When i closed the form, it passed a SelectedIndex of -1, and then closed the second time. So, in order to fix it, I simply put some simple verification of the value on the ListBox function.
Example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
becomes
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
}
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex == -1)
{
Application.Exit();
}
//Rest of the code goes here.
}
}
}

Categories