Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save WPF StackPanel Children

I have following codes and I dynamically created Stack panel with its children inside the List box. The Stack Panel's children value is getting from the text box entry

however I'm wondering whether the I can save each children value to the text or xml file and read them from the form load event.

I know how to add items to the Regular list box item save and load from the text flies. so, i really wanted to do the same procedure to the stack panel children value too.

according the following codes the it will over write the last textbox entry

for example , if I add text to the text box and press button it will add to the stack panel children and save it to the file. but if i enter text again save file date will overwrite.

instead of show number of text box entry , it show only last text value only.

I have less knowledge about the data binding so and I don't want to use Data Base & just want to save value on each children to the file.

Hope you guys can help me. Thank you!!

here are my codes

Class MainWindow

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
    Dim st As New StackPanel()
    Dim tb As New Label()

    tb.Content = TextBox1.Text

    st.HorizontalAlignment = Windows.HorizontalAlignment.Left
    'st.Height = 40
    'st.Width = 40
    'st.Margin = New Thickness(45, 45, 45, 0)

    Me.ListBox1.Items.Add(st)
    st.Children.Add(tb)



    IO.Directory.CreateDirectory("D:\save")
    Dim w As New IO.StreamWriter("D:\save\test.text")

    ' Dim i As Integer

    For Each tb In st.Children
        w.WriteLine(tb.Content)

    Next


    w.Close()
End Sub

End Class

here is my xmal codes

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="509" Width="762">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="320*" />
        <ColumnDefinition Width="420*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="49*" />
        <RowDefinition Height="262*" />
    </Grid.RowDefinitions>
    <TextBox Height="25" HorizontalAlignment="Left" Margin="59,244,0,0" Name="TextBox1" VerticalAlignment="Top" Width="121" Grid.Row="1" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="59,275,0,0" Name="TextBox2" VerticalAlignment="Top" Width="121" Grid.Row="1" />
    <Button Content="Button" Height="24" HorizontalAlignment="Left" Margin="77,308,0,0" Name="Button1" VerticalAlignment="Top" Width="83" Grid.Row="1" />
    <Label Content="Label" Grid.Column="1" Height="29" HorizontalAlignment="Left" Margin="38,12,0,0" Name="Label1" VerticalAlignment="Top" Width="120" DataContext="{Binding}" Visibility="Hidden" />
    <ListBox Height="297" HorizontalAlignment="Left" Margin="12,12,0,0" Name="ListBox1" VerticalAlignment="Top" Width="294" Grid.RowSpan="10" FontSize="14" FontStretch="Expanded" MinHeight="10"></ListBox>
</Grid>

like image 662
Roshi End Avatar asked Jan 19 '26 21:01

Roshi End


1 Answers

You need to use the overloaded StreamWriter constructor and specify that you want to append :

Dim w As New IO.StreamWriter("D:\save\test.text",True)

If you don't do that, the file will be overwritten :

true to append data to the file; false to overwrite the file. If the specified file does not exist, this parameter has no effect, and the constructor creates a new file.

like image 96
Abdusalam Ben Haj Avatar answered Jan 22 '26 15:01

Abdusalam Ben Haj