0

I'm trying to create dynamically text box in WPF. It is very essential that I will have the flexibility to determine where the text box will be - in pixel level. I have found many answers which use stackpanel to create "run-time" text box - but couldn't find how to construct it according to specified location.

the textbox has to be "word wrap" and I'm using a button click event to create the text box

this is the code for now, I really don't know which methods or properties will be helpful. thanks :)

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            TextBox x = new TextBox();
            x.Name = "new_textbox";
            x.TextWrapping= TextWrapping.Wrap;
            x.VerticalScrollBarVisibility=ScrollBarVisibility.Visible;
            x.AcceptsReturn = true;
            x.Margin = new Thickness(5, 10, 0, 0);
    }

2 Answers 2

3
 TextBox x = new TextBox();
 x.Name = "new_textbox";
 x.TextWrapping= TextWrapping.Wrap;
 x.VerticalScrollBarVisibility=ScrollBarVisibility.Visible;
 x.AcceptsReturn = true;
 x.Margin = new Thickness(5, 10, 0, 0);
 HouseCanvas.Children.Add(x);
 Canvas.SetLeft(x, 20);
 Canvas.SetTop(x, 20);
Sign up to request clarification or add additional context in comments.

Comments

1

You probably want to place it in a Canvas, if you care about pixel placement of the textbox itself. You'll need to use x.SetValue(Canvas.LeftProperty, pixelX) [and .RightProperty, etc...] to get the position exactly right. Having not done this myself, I'd guess that you need to put the canvas in the right Z-order (on top), and make it transparent. There may also be issues with events, depending on the z-order. Good luck!

-Kev

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.