locked
Work with code-created UI-Elements after they were created? RRS feed

  • Question

  • Hey,

    I've got a little more theoretical problem I cannot really solve. It is a Win8 App and written in c#/XAML.

    I have a Grid and the user can create multiple TextBlocks within that grid by marking the upper and lower border of that TextBlock. When the User right-clicks one of his created TextBoxes a menu with a TextBox pops up. That TextBox should enable the user to change the text of the TextBlock. I have two problems now:

    I don't know how to get the text the user enters into that specific TextBlock he right clicked on. One issue there is that the TextBlocks are created anonymously. That means they don't have a name and I don't know how to refer back to that clicked TextBlock.

    Another thing I'd like to implement is to enable the user to go to a special mode that allows him to remove his added TextBlocks by simply right-clicking them. I've the same problem of not knowing which TextBlock to remove.

    I had a few ideas but they all turned out being nonsense. That is why I won't post any code here for now.

    Would you like to give me some hints?

    Thank you very much! FunkyPeanut

    This Post is also on 

    http://stackoverflow.com/questions/19338207/work-with-code-created-ui-elements-after-they-were-created

    Saturday, October 12, 2013 8:48 PM

Answers

  • We can get the text from TextBox by adding ContextMenuOpening event handler to the newly created TextBox. Something like this 

    int _nameIndexer = 1; bool _specialMode = true; private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e) { TextBox txtBox = sender as TextBox; string txt = txtBox.Text; if (_specialMode == true) {

    e.Handled = true; container.Children.Remove(txtBox); } } private void Button_Click_2(object sender, RoutedEventArgs e) { TextBox txt = new TextBox(); //txt.Name = (_nameIndexer++).ToString(); txt.ContextMenuOpening += TextBox_ContextMenuOpening; container.Children.Add(txt); // Container is grid where we are adding text box. }

    Here TextBox_ContextMenuOpening is standard function already present in code.

    Regarding second question we can use some toggle button to switch to special mode and remove it using same event handler (TextBox_ContextMenuOpening). 

    private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e) { TextBox txtBox = sender as TextBox; string txt = txtBox.Text; if (_specialMode == true) {

    e.Handled = true; container.Children.Remove(txtBox); } }

    Hope this helps!


    Thanks, Sachin




    Sunday, October 13, 2013 12:52 AM
  • Yup it is strange as same code is working at my end. If you can show us the more code where you are having this issue, I can try it on my end. 

    Or you can name the textbox at run time and then use that name to remove it from the Children collection using RemoveAt function. 

    Something like this 

    TextBox txt = new TextBox();
                txt.Name = "TextBoxCollection_" + (_nameIndexer++).ToString();

    if (_specialMode == true)
                {
                    e.Handled = true;
                    for (int i = 0; i < container.Children.Count; ++i)
                    {
                        if (container.Children[i] is TextBox && ((TextBox)container.Children[i]).Name == txtBox.Name)
                        {
                            container.Children.RemoveAt(i);
                            break;
                        }
                        
                    }
                }


    Thanks, Sachin



    Sunday, October 13, 2013 1:57 AM

All replies

  • We can get the text from TextBox by adding ContextMenuOpening event handler to the newly created TextBox. Something like this 

    int _nameIndexer = 1; bool _specialMode = true; private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e) { TextBox txtBox = sender as TextBox; string txt = txtBox.Text; if (_specialMode == true) {

    e.Handled = true; container.Children.Remove(txtBox); } } private void Button_Click_2(object sender, RoutedEventArgs e) { TextBox txt = new TextBox(); //txt.Name = (_nameIndexer++).ToString(); txt.ContextMenuOpening += TextBox_ContextMenuOpening; container.Children.Add(txt); // Container is grid where we are adding text box. }

    Here TextBox_ContextMenuOpening is standard function already present in code.

    Regarding second question we can use some toggle button to switch to special mode and remove it using same event handler (TextBox_ContextMenuOpening). 

    private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e) { TextBox txtBox = sender as TextBox; string txt = txtBox.Text; if (_specialMode == true) {

    e.Handled = true; container.Children.Remove(txtBox); } }

    Hope this helps!


    Thanks, Sachin




    Sunday, October 13, 2013 12:52 AM
  • Thank you very much!

    I've got some weird issues now:

    I've tried your second idea as follows:

                if (isRemoveOnBool == true)
                {
                    TextBlock txt = sender as TextBlock;
                    e.Handled = true;
                    WeekGrid.Children.Remove(txt);
                }

    But it does not work. Nothing happens. Thats kinda weird... I had a similar idea a few hours ago. Humpf!

    I am still working on your first idea - maybe I can tweak a few things to make it work.

    Thank you again for your help :)

    Greetings

    FunkyPeanut


    Sunday, October 13, 2013 1:37 AM
  • Yup it is strange as same code is working at my end. If you can show us the more code where you are having this issue, I can try it on my end. 

    Or you can name the textbox at run time and then use that name to remove it from the Children collection using RemoveAt function. 

    Something like this 

    TextBox txt = new TextBox();
                txt.Name = "TextBoxCollection_" + (_nameIndexer++).ToString();

    if (_specialMode == true)
                {
                    e.Handled = true;
                    for (int i = 0; i < container.Children.Count; ++i)
                    {
                        if (container.Children[i] is TextBox && ((TextBox)container.Children[i]).Name == txtBox.Name)
                        {
                            container.Children.RemoveAt(i);
                            break;
                        }
                        
                    }
                }


    Thanks, Sachin



    Sunday, October 13, 2013 1:57 AM