Winforms User COntrol

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sara

    #1

    Winforms User COntrol

    Hello there,

    Iam creating a user control with 2 buttons, And i have a public
    static variable i'll be changing the value of the variable in the
    button click events.


    When i consume the user control in the winforms application how to
    automatically get the value of the public variable on click of the user
    control.

    user control code

    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows. Forms;

    namespace WindowsControlL ibrary2
    {
    public partial class UserControl1 : UserControl
    {
    public static string msButtonClicked ;

    public UserControl1()
    {
    InitializeCompo nent();
    }

    public UserControl1(De legate p)
    {
    InitializeCompo nent();
    }


    private void Button1_Click(o bject sender, EventArgs e)
    {
    msButtonClicked = "Val1";
    }

    private void Button2_Click(o bject sender, EventArgs e)
    {
    msButtonClicked = "Val2";
    }

    private void Button3_Click(o bject sender, EventArgs e)
    {
    msButtonClicked = "Val3";
    }

    private void Button4_Click(o bject sender, EventArgs e)
    {
    msButtonClicked = "Val4";
    }

    }
    }


    consuming page
    i want value to be popped up in userControl11_c lick event
    private void userControl11_L oad(object sender, EventArgs e)
    {

    MessageBox.Show (WindowsControl Library2.UserCo ntrol1.msButton Clicked);
    //EventHandler handler = new EventHandler(a) ;
    //userControl11.C lick += handler;
    }


    Regards
    Saravanan K

  • iwdu15

    #2
    RE: Winforms User COntrol

    wrong newsgroup, try the C# newsgroup:

    http://msdn.microsoft.com/newsgroups...nguages.csharp

    they can prob help you out
    --
    -iwdu15

    Comment

    • Jim Wooley

      #3
      Re: Winforms User COntrol

      It sounds like you are breaking encapsulation. As an alternative, I would
      recommend that you have a single public property against which you program
      your consuming classes. Perhaps you could just override .Text and return
      a private string msButtonClicked (which should not be static). Public fields
      are typically considered bad OOP practice.

      Alternatively, you could just have the .Text accessor check the value of
      the internal boxes and return the appropriate string, thereby eliminating
      the need for msButtonClicked as a string anyway. On property Set, you can
      set the private string and click the appropriate internal button. All of
      the internal buttons should be scoped private (or protected), not public
      to maintain the encapsulation.

      For your messaging back to the consuming form, your control should raise
      a custom event (perhaps "Changed") whenever an internal button is clicked.
      The form would add a handler to the user control's Changed event rather than
      any internal event handlers. Once you make these changes, your form will
      only need to be concerned with the following: .Text (property) and .Changed
      (event). Everything else will be encapsulated inside of the user control.

      Jim Wooley

      Hello there,
      >
      Iam creating a user control with 2 buttons, And i have a public
      static variable i'll be changing the value of the variable in the
      button click events.
      >
      When i consume the user control in the winforms application how to
      automatically get the value of the public variable on click of the
      user control.
      >
      user control code
      >
      using System;
      using System.Collecti ons.Generic;
      using System.Componen tModel;
      using System.Drawing;
      using System.Data;
      using System.Text;
      using System.Windows. Forms;
      namespace WindowsControlL ibrary2
      {
      public partial class UserControl1 : UserControl
      {
      public static string msButtonClicked ;
      public UserControl1()
      {
      InitializeCompo nent();
      }
      public UserControl1(De legate p)
      {
      InitializeCompo nent();
      }
      private void Button1_Click(o bject sender, EventArgs e)
      {
      msButtonClicked = "Val1";
      }
      private void Button2_Click(o bject sender, EventArgs e)
      {
      msButtonClicked = "Val2";
      }
      private void Button3_Click(o bject sender, EventArgs e)
      {
      msButtonClicked = "Val3";
      }
      private void Button4_Click(o bject sender, EventArgs e)
      {
      msButtonClicked = "Val4";
      }
      }
      }
      consuming page
      i want value to be popped up in userControl11_c lick event
      private void userControl11_L oad(object sender, EventArgs e)
      {
      MessageBox.Show (WindowsControl Library2.UserCo ntrol1.msButton Clicked);
      //EventHandler handler = new EventHandler(a) ;
      //userControl11.C lick += handler;
      }
      Regards
      Saravanan K

      Comment

      Working...