CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Separating Control Properties for multiple User Controls

  1. #1
    Join Date
    Aug 2005
    Posts
    18

    Separating Control Properties for multiple User Controls

    I have written a User Control which has a button and a text box on it. The textbox is updated by using

    UserControlTxtBox.Text= someValue;

    This works fine if there is one User Control on a form.
    If I have 2 or more, the first User Control works ok initially, but once I start to access the Text property of the TextBox on the second control, both Text properties on both controls get updated with the same information from then on. It seems to be updating the User Controls' textBox rather than the base control individually.

    Any idea on how I can fix this?

    Regards
    alias47

  2. #2
    Join Date
    Apr 2005
    Posts
    576

    Re: Separating Control Properties for multiple User Controls

    It is hard to tell with only this information. Can you post the code for the Control implementation? Can you post the code for the Control declarations on the form?

  3. #3
    Join Date
    Oct 2001
    Posts
    80

    Re: Separating Control Properties for multiple User Controls

    The way I do this, in the usercontrol I add a property:

    Code:
    public string SetText 
        {
          set
          {//Textbox1 is the textbox in your usercontrol
            this.textBox1.Text = value;
          }
          get
          {
            return this.textBox1.Text;
          }
        }
    Then in your form:

    usercontrol1.SetText= "Que pasa?" ;
    usercontrol2.SetText= "SomeOtherText";

    Greetzzzz...

  4. #4
    Join Date
    Aug 2005
    Posts
    18

    Re: Separating Control Properties for multiple User Controls

    In the UserControl:
    Code:
     this.txtCell = new System.Windows.Forms.TextBox();
                this.btnCell = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // txtCell
                // 
                this.txtCell.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.txtCell.Location = new System.Drawing.Point(0, 3);
                this.txtCell.Multiline = true;
                this.txtCell.Name = "txtCell";
                this.txtCell.Size = new System.Drawing.Size(143, 21);
                this.txtCell.TabIndex = 0;
                this.txtCell.Click += new System.EventHandler(this.txtCell_Click);
    Code that sets the Text property:
    Code:
    txtCell.Text="Text";
    In the Form:
    Code:
    // Cell1
                // 
                this.Cell1.AllowDrop = true;
                this.Cell1.Location = new System.Drawing.Point(125, 90);
                this.Cell1.Name = "Cell1";
                this.Cell1.Size = new System.Drawing.Size(141, 20);
                this.Cell1.TabIndex = 8;
                // 
                // Cell2
                // 
                this.Cell2.AllowDrop = true;
                this.Cell2.Location = new System.Drawing.Point(125, 50);
                this.Cell2.Name = "Cell2";
                this.Cell2.Size = new System.Drawing.Size(141, 20);
                this.Cell2.TabIndex = 7;

  5. #5
    Join Date
    Aug 2005
    Posts
    18

    Re: Separating Control Properties for multiple User Controls

    To bewa:

    I am attempting to set the Text Property from within the UserControl. The user needs to add my UserControl to their form and should not need to write any code to get it to work correctly.

    Regards

  6. #6
    Join Date
    Apr 2005
    Posts
    576

    Re: Separating Control Properties for multiple User Controls

    Bewa's code shows the principles.

    From your code I have seen nothing wrong so far.

    If I understand everything correctly, you do not update the textbox via code from the form.

    Are the text boxes data bound?

    I think we still need more code, where are the textboxes updated via code?

    Probably you can figure it out by yourself by placing breakpoints in each place where the text box is updated, look how you got there.

  7. #7
    Join Date
    Oct 2001
    Posts
    80

    Re: Separating Control Properties for multiple User Controls

    So basicly, you want the *same* type of usercontrol to behave *differently* without changing any of it's properties? That is, by all means, absurd if you ask me.

    I probably just don't get waht you are trying to do.

  8. #8
    Join Date
    Aug 2005
    Posts
    18

    Re: Separating Control Properties for multiple User Controls

    Thanks for your help so far...
    What I thought was possible:

    A user clicks the text box in one of the User Controls. Some stuff happens. Result is returned to that TextBox.

    A user clicks the text box in another User Control. Some stuff happens. Result is returned to that TextBox.

    Regards
    alias47

  9. #9
    Join Date
    Apr 2005
    Posts
    576

    Re: Separating Control Properties for multiple User Controls

    What you are trying to do should certainly work.

  10. #10
    Join Date
    Aug 2005
    Posts
    18

    Re: Separating Control Properties for multiple User Controls

    Hi klintan,

    Yes that's what i thought. They are separate controls. Surely I can access them separately.

    alias47

  11. #11
    Join Date
    Oct 2001
    Posts
    80

    Re: Separating Control Properties for multiple User Controls

    Ah.. OK. See my prvious code for the property, and then in the usercontrol add a eventhandler for Click or DoubleClick. Then use...
    Code:
     private void textBox1_DoubleClick(object sender, System.EventArgs e)
        {
             //Do your code here, for example I will use Ticks to get different results          but you can do everything
            long ticks =  DateTime.Now.Ticks;
            this.SetText = ticks.ToString();
        }

    I tested it, it worked.

  12. #12
    Join Date
    Aug 2005
    Posts
    18

    Re: Separating Control Properties for multiple User Controls

    Thank you . i have tried this in my app but it still duplicates the entries. I will do a test app tomorrow with just your code and see what happens.

  13. #13
    Join Date
    Aug 2005
    Posts
    18

    Re: Separating Control Properties for multiple User Controls

    You are right bewa. Your code does work correctly.

    However, I had some other Excel Application events firing which were updating all User Controls. I had to add a check to see where any of the controls of the User Control currently had the focus.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)