1

I am working on a WPF project which allows one to enter a comment in one text box, and when s/he presses a button the comment written is posted into another textbox, aside from this the last comment posted needs to be posted on the previous. So if for example comment 1 is posted at 10pm and the other at 11pm the 11 pm comment is put on top of the 10 pm comment. How can I do this?

<TextBox x:Name="txtWriteComments" MinLines="4"/> 
<Button x:Name="btnPostComments" Content="Post Comment" Click="btnPostComments_Click"/>

Code in Main Window.cs

private void btnPostComments_Click(object sender, RoutedEventArgs e) 
{
    int i = 0; 
    TimeSpan time = DateTime.Now.TimeOfDay; 
    do 
    { 
        i++; 
        ???
    } while (i < 6); 
} 

I did the counter because it will only allow a maximum of 5 comments

Rachel
  • 130,264
  • 66
  • 304
  • 490
Chanter08
  • 151
  • 1
  • 2
  • 9
  • Can you share some code with us? Helpful code would be the XAML for your entry and display textboxes and any code behind functionality related to this. I'm thinking that a textbox is not the control you want to use for display. – Danielle Nov 25 '15 at 20:07
  • Code in Main Window – Chanter08 Nov 25 '15 at 20:15
  • Code in Main Window.cs private void btnPostComments_Click(object sender, RoutedEventArgs e) { int i = 0; TimeSpan time = DateTime.Now.TimeOfDay; do { i++; } while (i < 6); } I did the counter because it will only allow a maximum of 5 comments – Chanter08 Nov 25 '15 at 20:17
  • I've suggested an edit for your original question to include this code. You can do this by indenting by 4 spaces. From this snippit it only looks like you have one text box that is for comment entry and a button to post them. Where do the comments go after that? – Danielle Nov 25 '15 at 20:19
  • I would recommend adding your Click method to your original post. It sort of looks like your click method does nothing right now. Is that accurate? – Danielle Nov 25 '15 at 20:23
  • yeah exactly, i have got no idea of how im going to code the posting of the comments. i was actually going to make it create textblock in a stack panel, but dont know how to link between the method and the target ( the stack panel) – Chanter08 Nov 25 '15 at 20:28
  • You can either bind to a ViewModel or set the properties in the code behind. – Danielle Nov 25 '15 at 20:38
  • I would treat each comment as a separate object in a collection (data object can store text, datetime, user, etc), then draw the collection using an `ItemsControl`. Then you can set the sort to whatever you want, and you have more control of the UI layout of each item or the details displayed. But if you do want to keep what you have now of appending everything to one TextBox, [be sure to set IsUndoEnabled to False](http://stackoverflow.com/q/8733887/302677) – Rachel Nov 25 '15 at 20:40
  • So the code would look how exactly ? Sorry im a beginner at this – Chanter08 Nov 25 '15 at 20:46

1 Answers1

1

I would use an ItemsControl, maybe a ListBox, to display all of the comments. You'd put this in your XAML with the TextBox and Button. The ItemsSource of your control should be something like an ObservableCollection. I'll leave that up to you, but let's call this collection 'Comments'. You can add new comments to the top and continually delete comments from the bottom (5).

private void btnPostComments_Click(object sender, RoutedEventArgs e) 
{
    string comment = string.Format("{0}: {1}", DateTime.Now.TimeOfDay, txtWriteComments.Text);
    Comments.Insert(0, comment);
    if(Comments.Count > 5)
        Comments.RemoveAt(5);
} 

Additionally, you might want to clear out the value of the TextBox after it's been added to the ListBox. You could do this here as well.

Danielle
  • 472
  • 4
  • 20