Setting .Text property of a group of textboxes from another control

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

    Setting .Text property of a group of textboxes from another control

    Hello,

    I am trying to figure out how to efficiently do the following:

    Say I have 10 TextBox fields on a form. From each of those, I would
    like to have
    a right click context menu. There are 10 menu items on the context
    menu. Once
    one of those items are selected, I would like the text to be copied
    into the text field
    of the appropriate textbox.

    In the click Handler, I have something like:

    private void myToolStripMenu Item_Click(obje ct sender, EventArgs e)

    {
    ToolStripMenuIt em m = (ToolStripMenuI tem)sender;
    string mystr = m.Text;
    // set the text field


    }

    Can someone tell me the c# construct to set the Text property of the
    "calling" object, in this case a textbox?

    Thanks
    Mike
  • zacks@construction-imaging.com

    #2
    Re: Setting .Text property of a group of textboxes from anothercontrol

    On May 30, 2:54 pm, eljainc <elja...@sbcglo bal.netwrote:
    Hello,
    >
    I am trying to figure out how to efficiently do the following:
    >
    Say I have 10 TextBox fields on a form. From each of those, I would
    like to have
    a right click context menu. There are 10 menu items on the context
    menu. Once
    one of those items are selected, I would like the text to be copied
    into the text field
    of the appropriate textbox.
    >
    In the click Handler, I have something like:
    >
    private void myToolStripMenu Item_Click(obje ct sender, EventArgs e)
    >
            {
                ToolStripMenuIt em m = (ToolStripMenuI tem)sender;
                string mystr = m.Text;
                // set the text field
    >
            }
    >
    Can someone tell me the c# construct to set the Text property of the
    "calling" object, in this case a textbox?
    I'm sure there are probably more "elegant" ways to do it, but you
    might want to consider some creative use of the ToolStripMenuIt em's
    Tag property.

    Comment

    • Peter Duniho

      #3
      Re: Setting .Text property of a group of textboxes from another control

      On Fri, 30 May 2008 11:54:35 -0700, eljainc <eljainc@sbcglo bal.netwrote:
      [...]
      Can someone tell me the c# construct to set the Text property of the
      "calling" object, in this case a textbox?
      You can retrieve the control that caused the context menu to appear using
      the ContextMenuStri p.SourceControl property (after getting the
      ContextMenuStri p parent from the menu item, of course). Then, just set
      the Text property of that Control instance.

      Warning: there's a bug in .NET in which the SourceControl property is only
      valid for top-level menu items. As long as you don't have sub-menus, this
      won't be an issue. If you do, there are work-arounds...a Google search in
      this newsgroup will help you find previous comments on that topic.

      For what it's worth, your subject and your description of the control as
      "the 'calling' object" both seem inaccurate to me, given what appears to
      be the actual goal here. You may find that it's easier to get good
      answers to your questions if you take care to make sure you've described
      the question as simply, and non-prejudicially as possible (on the latter
      point, I mean that the inaccurate parts I mentioned seem to be inaccurate
      because of a presumption on your part as to what the solution might look
      like, rather than simply stating the broader problem being solved).

      Of course, I may have misunderstood the question, and if so then my
      comments about the accuracy of the question may themselves be inaccurate.
      However, even in that case there'd still be a good argument that the
      question was ambiguous. :)

      Pete

      Comment

      • eljainc

        #4
        Re: Setting .Text property of a group of textboxes from anothercontrol


        Pete, thanks! The ContextMenu.Sou rceControl property is just what I
        needed.

        Mike

        Comment

        Working...