How do i play sound only once in c#? [duplicate] - c#

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to avoid multiple instances of windows form in c#
I want to show open form only once in an application without creating them as MDI form.

Before opening the form from your code, check the Application.OpenForms property and see if the form exists in the collection.
Something like:
if ((Application.OpenForms["Form1"] as Form1) != null)
{
//Form is already open
}
else
{
// Form is not open
}

You can just use show\hide methods for opening your form. You also need to initialize all form fields after each time you show this.

Use a boolean variable with default value false which you set to true once the form was shown, and check the variable on form opening.

Related

How to move data from child form to parent form? [duplicate]

This question already has answers here:
How to return a value from a Form in C#?
(9 answers)
Closed 5 years ago.
I have two forms.the parent form is form A. in form A I open Form B.I do something then I want to transfer data to form A.I do not know how to do it.
I try to using public variables but it did not work.
The best way is that Form B raises an event when something of interest happens. Form A can listen for this event and then update one of its controls.
If you give more information I'll be happy to provide an example.

Using C# I want to send text to another form's textbox [duplicate]

This question already has answers here:
Passing TextBox's text to another form in C#?
(14 answers)
Closed 8 years ago.
I'm fairly new to C# so please go easy on me.
I have form1 as the main form and another form called "debug". The debug form has two text boxes, dbgBox1 and dbgBox2. What I am trying to do is, when debug.Visible == true, update the text boxes from form1. I don't want this to happen only on button clicks, etc.
For example, it's easy to update a textbox that is on form1 (via textbox1.Text = "";) How can I do something similar but update the textbox in the debug form?
This thread seems close but does not explain itself enough for me to understand.
If you're instantiating the 2nd form yourself from a form, you can just update it via reference like this:
formDebug fd = new formDebug();
fd.Show(this);
fd.dbgBox1.Text = "Box 1 Text";
fd.dbgBox2.Text = "Box 2 Text";
fd.Update();
It is a good practice to implement a method to change the form state externally, so your forms will be less coupled. Accessing the properties of another form directly generally is not a good idea, including the main form.
public partial class debug : Form
{
public debug()
{
InitializeComponent();
}
public void setdbgBox1Text(string text)
{
dbgBox1.Text = text;
}
}
Then you can use:
debug deb = new debug();
deb.Show();
deb.setdbgBox1Text("aaa");

Referencing variables between wpf forms [duplicate]

This question already has answers here:
How can I access one window's control (richtextbox) from another window in wpf?
(4 answers)
Closed 9 years ago.
Alright, so I have a form with textboxes and a button (Form 1), and that button opens a new form (Form 2) consisting of a textbox.
What I want to do is get the contents of one of the textboxes of the first form (like TextboxForm1.Text) and use that text in the second form, like TextboxForm2.Text = {however to reference textbox 1 from form 1}.Text;.
Is there an obvious way that I overlooked?
Thanks.
Edit: Tried both solutions and the both worked well, but making it public was much easier in the case of multiple textboxes.
http://msdn.microsoft.com/en-us/library/aa970905.aspx
<TextBox Name="TextboxForm1" x:FieldModifier="Public" />
Yes, you could use a property to expose the value of the desired textbox from the appropriate form. So, add something like the following to your window class:
public string TextBox1Text {
get { return TextBox1.Text; }
}
And then access it from the instance, as you seem to know, like this:
AnotherTextBox.Text = instance.TextBox1Text;
As for using the access modifier for the control as per nmclean's answer (i.e. FieldModifier="Public"), I would only say make the entire control public if it is needed.

How to create a form without any TitleBar? [duplicate]

This question already has answers here:
Remove the title bar in Windows Forms
(7 answers)
Closed 9 years ago.
I am actually developing a UserControl that requires this kind of form.
Normally a WinForms Form looks like this:
If I set "FormBorderStyle = None", it looks like this:
But, I actually need a window without TitleBar like the following:
Please see the difference at the edge of the window. It actually looks more like a context menu.
myForm.Text="";
myForm.ControlBox= false;
This solution leaves the TitleBar so that the form remains movable. This is a problem.
I actually need this: the user click the button and the form appears like the following:
How to do this?
you can use:
yourForm.Text="";
yourForm.ControlBox= false;
and in properties, change : FormBorderStyle to :FixedDialog
To get like that then do not set any title text and make controlbox visible false
like this
yourForm.Text="";
yourForm.ControlBox= false;

How do I display a messageBox on top of, or near its parent Form? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
center MessageBox in parent form
I am trying to make an C# application and I want to have my messageBox appear near the parent.
I tried:
MessageBox.Show(this,"this operation does not work");
And this doesn't work.
Assuming you're using Windows Forms, create a new class which extends System.Windows.Form to mimic MessageBox. Set the Location property based on your new classes Parent.Location property.

Categories

Resources