1

I have Added buttons on my page in a canvas on the screen, so when I click 'Add Title' it will add a text box in a certain position and when I click Add Question it will do the same thing.

enter image description here

Here is the code I have which preforms this action.

public void AddTitle_Click(object sender, RoutedEventArgs e)
{
    TextBox x = new TextBox();
    x.Name = "new_textbox";
    x.TextWrapping = TextWrapping.Wrap;
    x.Height = 20;
    x.Width = 100;
    x.AcceptsReturn = true;
    x.Margin = new Thickness(5, 10, 0, 0);
    canvas1.Children.Add(x);
    Canvas.SetLeft(x, 100);
    Canvas.SetTop(x, 100);
}

private void AddQuestion_Click(object sender, RoutedEventArgs e)
{
    TextBox x = new TextBox();
    x.Name = "new_textbox";
    x.TextWrapping = TextWrapping.Wrap;
    x.Height = 20;
    x.Width = 200;
    x.AcceptsReturn = true;
    x.Margin = new Thickness(5, 10, 0, 0);
    canvas1.Children.Add(x);
    Canvas.SetLeft(x, 140);
    Canvas.SetTop(x, 140);
}

The Title position is the smaller Textbox and the question the bigger Textbox. A title can have any amount of questions it. So when I click 'Add question' I need it to appear below the last question Textbox and when I click add title I need it to appear below the last question but in a different position?

I deleted my old post as I figured out how to add the Textboxes.

Abbas
  • 14,186
  • 6
  • 41
  • 72
  • why use canvas instead of common containers like StackPanel, any specific reason? Positioning newly added TextBox below the last TextBox will be automatic if you switch from canvas to StackPanel.. – har07 Dec 20 '13 at 11:11
  • @har07 There was no specific reason, when I was looking around I found people used canvas more than the other containers. Should I change to a S#tackpanel? – user3110814 Dec 20 '13 at 11:13
  • 2
    i suggest to change. Try it out, `stackpanel1.Childre.Add(x);` will be enough without setting position manually. – har07 Dec 20 '13 at 11:17
  • @har07 I have tried it and I can see what you mean by it adds it underneath manually. The problem is the 'Title' and 'Question' need to be set to the left different distances. `Canvas.SetLeft(x, 100)` for the Title and `Canvas.SetLeft(x, 140)` for the question. – user3110814 Dec 20 '13 at 11:19
  • 1
    Who are these degenerates who use `Canvas` more than other containers? To the gallows with them! – Gusdor Dec 20 '13 at 11:33
  • @Gusdor I was just researching on different websites haha :) I have changed now and StackPanel is so much better. – user3110814 Dec 20 '13 at 11:34
  • 1
    @user3110814 use two stackpanels if you want 'Title' and 'Question' side by side, or simply set margin of 'Title'/'Question' if you really want them to appear in position the like screen capture image shows – har07 Dec 20 '13 at 11:50
  • @har07 I have tried the 2 Stackboxes next to eachother at it works it just looks abit 'Scruffy' How can I set the margin(from the left) for question and title to be different? – user3110814 Dec 20 '13 at 12:00
  • @har07 Also, when you have 2 stackpanels they cant communicate so that the next title added will be under the question. – user3110814 Dec 20 '13 at 12:07
  • @har07 Thanks for your help i figured out the margin! Very helpful Thanks. – user3110814 Dec 20 '13 at 12:24
  • 1
    Never mind, glad to know that was helpful. Sorry to say, but better to post another question if you stuck again somewhere. This discussion getting further from the question title, you have changed to StackPanel from Canvas though. happy coding as always :D – har07 Dec 20 '13 at 12:29

1 Answers1

2

You don't have to add them like that.

You should use a TreeView or at least StackPanels and them add the items to them.

Also, this line:

x.Name = "new_textbox";

is not correct. If you add more than one element with the same name it will produce an error (it should be unique). You should leave that Property empty or give them names dynamically.

I hope you have enough info with this code approaches are multiple and quite longs.

EDIT: To locate a certain element in the window see this question: Get Absolute Position of element within the window in wpf

Community
  • 1
  • 1
javi
  • 159
  • 2
  • 15
  • I wasn't allowed to use a Tree-view for this task, I think I might change to Stack-panels now aswell. How can I give the TextBoxs names dynamically? Do you know of any way of finding the last position of the textbox though? – user3110814 Dec 20 '13 at 11:15
  • 1
    1) If you can't use TreeView use StackPanels 2) To give them names dinamically (i don't think it's a good practice) you could try something like "x.Name = "txtTitle" + index" where index is the number of titles you have (you can go increasing it as you add titles) – javi Dec 20 '13 at 11:23
  • This is a good idea and I am going to add this in but how would index know how many text boxes have been added? – user3110814 Dec 20 '13 at 11:32
  • index should be a property of your class, and you should increase it when you add a textBox. You shoud have two index properties one for titles and other for questions – javi Dec 20 '13 at 11:45