Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Control dependent event-firing.

Control dependent event-firing.

Scheduled Pinned Locked Moved C#
questioncsharpcomhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NietzscheDisciple
    wrote on last edited by
    #1

    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 for ctlDSP1 and not for the other controls?

    H 1 Reply Last reply
    0
    • N NietzscheDisciple

      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 for ctlDSP1 and not for the other controls?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      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 use is or a simple type check using GetType and typeof. 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]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups