Radio buttons not working to select something - c#

I am trying to create a dialogue box that contains 3 radio buttons and a "Present" button to be used with some projector software we have. What I want to do is, after clicking "Present", open a file specific to which room was chosen with the radio buttons.
The issue is, after clicking "Present" nothing happens.
I know Process.Start and present_Click are correct; in a test project I did the "Present" button successfully opens a file. I am sure the CheckedChanged parts are probably incorrect, but I am confused as to what to do with them.
Here's 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.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void present_Click(object sender, EventArgs e)
{
if (room1.Checked)
{
System.Diagnostics.Process.Start(#"room1.txt");
}
else if (room2.Checked)
{
System.Diagnostics.Process.Start(#"room2.txt");
}
else if (room3.Checked)
{
System.Diagnostics.Process.Start(#"room3.txt");
}
}
private void room1_CheckedChanged(object sender, EventArgs e)
{
room1.Checked = true;
}
private void room2_CheckedChanged(object sender, EventArgs e)
{
room2.Checked = true;
}
private void room3_CheckedChanged(object sender, EventArgs e)
{
room3.Checked = true;
}
}
Can someone please help me with these radio buttons?

Your CheckedChanged event handlers are definitely messed up.
In english, your logic says
Anytime a radio button's checked status is changed, set its checked property to true
Honestly I'm surprised you don't get into some infinite loop where every control is fighting to be the "selected one". Removing that logic altogether may well fix your problem. It also seems odd that you are "starting" a text file, but I assume that is intentional.
The important knowledge here is that a user clicking the radio button automatically sets the Checked property. There is no need to do it yourself in code-behind.

I think you need to remove the events which trigger upon checking or unchecking. If I understand properly, you only want 'something' to happen when you click Present. So once someone clicks Present, then verify which checkboxes are on/off and do the appropriate thing:
Sorry if I'm making the wrong assumptions, feel free to correct.
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.IO;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void present_Click(object sender, EventArgs e)
{
if (room1.Checked)
{
System.Diagnostics.Process.Start(#"room1.txt");
}
else if (room2.Checked)
{
System.Diagnostics.Process.Start(#"room2.txt");
}
else (room3.Checked)
{
System.Diagnostics.Process.Start(#"room3.txt");
}
}
}
}

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 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.

Most efficient way to call/access windows form controls in a class

I added a CSharp Class to a form project. Now I want to access the form controls from the Class.
I am using this piece of code with the implicit var as follows:
var form = frmClasses.ActiveForm as frmClasses;
Using this code, I set the form Modifiers to public, place the code is a method/function and I am able to access the form controls in the class.
The problem is each time I need to call a form control I have to use the var and the entire line of code above. Is there a simpler way that is less redundant?
Example: Each method/function in a the class will need to call the form controls thus referencing the form controls using Var.
Take a look at the code below, you will noticed that I am using Var to call each form control.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Classes
{
class ClsProperties
{
//VALIDATE TEXTBOX
public static void validateTextBox()
{
var form = frmClasses.ActiveForm as frmClasses;
if (form.txtMyTextBox.Text.Trim().Length == 0)
{
form.txtMyTextBox.BackColor = Color.Aquamarine;
}
}
//VALIDATE RADIO BUTTON
public static void validateRadio()
{
var form = frmClasses.ActiveForm as frmClasses;
if (form.radBtnColor.Checked == false)
{
form.lblShowError.ForeColor = Color.Aquamarine;
}
}
}
}
Form Code:
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 Classes
{
public partial class frmClasses : Form
{
public frmClasses()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
ClsProperties.validateRadio();
ClsProperties.validateTextBox();
}
}
}
I could put all the validation in one method but I am simply trying to show how I would call multiple methods using a class.
Is there a better way to access the form elements in a class?
Merci

Why isn't my UserControl keeping its initialization?

I have a UserControl for which I think I'm initializing some of the members:
// MyUserControl.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyNamespace
{
public partial class MyUserControl : UserControl
{
private string m_myString;
private int m_myInt;
public string MyString
{
get { return m_myString; }
set { m_myString = value; }
}
public int MyInt
{
get { return m_myInt; }
set { m_myInt = value; }
}
public MyUserControl()
{
InitializeComponent();
MyString = ""; // was null, now I think it's ""
MyInt = 5; // was 0, now I think it's 5
}
// .........
}
}
When I insert this control into my main form, though, and call a function that checks values within MyUserControl, things don't look like they're initialized:
// MainForm.cs
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 MyProgram
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MyButton_Click(object sender, EventArgs e)
{
// this prints 0 rather than 5
MessageBox.Show(this.theUserControl.MyInt.ToString());
}
}
}
I'm guessing this is a really simple error but I don't know where. I've tried prepending this. to things, but this probably isn't the way to go about fixing code. :)
Thanks as always!
EDIT: Stepping into the designer code as Pete suggested showed me where the write-over was happening. First I called the constructor of the user control, and then later, the values got overwritten with default values. I hadn't specified any default values (Sanjeevakumar Hiremath's suggestion) so the default values were those of the primitive types (for int this was 0).
What you're likely seeing here is an artifact of the designer. If you ever opened up MainForm in a designer after you added MyUserControl it likely recorded the default values of MyUserControl in the generated InitializeComponent method of MainForm. These recorded values are re-assigned after the constructor of MyUserControl runs hence they're overwriting the values you set.
You can control this behavior via the use of the DesignerSerializationVisibilityAttribute
http://msdn.microsoft.com/en-us/library/system.componentmodel.designerserializationvisibilityattribute.designerserializationvisibilityattribute.aspx#Y375
Use [DefaultValue] Attribute. It lets you specify default value for a property of a control when the value is not specified in the designer.

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