Control dependent event-firing.
-
I've written a DirectSound application in C# that controls the playback/streaming of audio data. In my application, I have three UserControls. The code for each of these Controls performs a particular DSP task. The following is added in the main form's constructor:
// Tags for DSP Function Radio Buttons
rbDSP1.Tag = new ControlFactory(ctlDSP1.Create);
rbDSP2.Tag = new ControlFactory(ctlDSP2.Create);
rbDSP3.Tag = new ControlFactory(ctlDSP3.Create);On my main form, I have a panel that is used to display these controls. Clicking one of three radio buttons results in the corresponding control being displayed in the panel area. The code for this is as follows:
// On the main form
private void DisplayControl(object sender, System.EventArgs e)
{
RadioButton rb = sender as RadioButton; // cast sender as RadioButton
if (rb != null) // Ensure successful casting
{
if (rb.Checked) // Proceed if checked
{
if (m_DSPControl != null)
{
panelControl.Controls.Remove(m_DSPControl);
m_DSPControl.Dispose();
}
ControlFactory cf = rb.Tag as ControlFactory;
m_DSPControl = cf();if (m_DSPControl != null)
panelControl.Controls.Add(m_DSPControl);
}
}
}// In ctlDSP1 code
public static Control Create()
{
return new ctlDSP1();
}Just as an example, let's say UserControl
ctlDSP1
just has a trackbar that acts as a volume control, and I need to obtain the value of the trackbar to control the volume of the playback buffer (whose code is in the main form). I used Tom's suggestion (http://tinyurl.com/46yov), and it works. However, I still face the following problem: In the main form, how do I ensure that I am creating the Event Handler forctlDSP1
and not for the other controls? -
I've written a DirectSound application in C# that controls the playback/streaming of audio data. In my application, I have three UserControls. The code for each of these Controls performs a particular DSP task. The following is added in the main form's constructor:
// Tags for DSP Function Radio Buttons
rbDSP1.Tag = new ControlFactory(ctlDSP1.Create);
rbDSP2.Tag = new ControlFactory(ctlDSP2.Create);
rbDSP3.Tag = new ControlFactory(ctlDSP3.Create);On my main form, I have a panel that is used to display these controls. Clicking one of three radio buttons results in the corresponding control being displayed in the panel area. The code for this is as follows:
// On the main form
private void DisplayControl(object sender, System.EventArgs e)
{
RadioButton rb = sender as RadioButton; // cast sender as RadioButton
if (rb != null) // Ensure successful casting
{
if (rb.Checked) // Proceed if checked
{
if (m_DSPControl != null)
{
panelControl.Controls.Remove(m_DSPControl);
m_DSPControl.Dispose();
}
ControlFactory cf = rb.Tag as ControlFactory;
m_DSPControl = cf();if (m_DSPControl != null)
panelControl.Controls.Add(m_DSPControl);
}
}
}// In ctlDSP1 code
public static Control Create()
{
return new ctlDSP1();
}Just as an example, let's say UserControl
ctlDSP1
just has a trackbar that acts as a volume control, and I need to obtain the value of the trackbar to control the volume of the playback buffer (whose code is in the main form). I used Tom's suggestion (http://tinyurl.com/46yov), and it works. However, I still face the following problem: In the main form, how do I ensure that I am creating the Event Handler forctlDSP1
and not for the other controls?If I understand you correctly, you just need some type checking. You're already doing it with
as
(a safe cast), but you can also useis
or a simple type check usingGetType
andtypeof
. You might also consider using polymorphism instead of class factories, since activation in the .NET Framework - both for local and remote classes - is already handled by the Framework. IF all your controls implement the same interface you wouldn't have to worry about differences in types as long as your interface exposes the properties, methods, and events you need. This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Sustained Engineering Microsoft [My Articles] [My Blog]