Problem saving user-created text boxes using UWP C#

Andrew Holtzman 1 Reputation point
2020-03-19T00:32:29.167+00:00

Hi,

I am currently developing a desk calendar app which allows the user to create text boxes and place them in a certain StackPanel based on which day they desire to add it to.

My problem is that I have little experience with UWP (only using it since February). Currently, the textboxes created by the user are working as I expect, but they disappear if I close the app and re-open it or I navigate to a new page within my app (so pretty much, they do not save).

Does anyone know the solution to saving textboxes so they are unaffected even if the user closes the app?

Any help would be much appreciated. Thank you.

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Roy Li - MSFT 31,826 Reputation points Microsoft Vendor
    2020-03-19T03:18:25.703+00:00

    Hello,

    Welcome to Microsoft Q&A!

    The first idea comes up to my mind is that you could try to save the properties of the custom TextBoxes into local files. Then you could read these properties from files and create these TextBoxes again based on these properties when you launch your app.

    To write text into local files, you could refer to this document: Create, write, and read a file.

    Best regards,
    Roy

    1 person found this answer helpful.

  2. Anonymous
    2020-03-19T20:52:57.223+00:00

    Hi My first post in this forum. My intension was to convert to C# from this VB code to post, but for some reason the code wouldn't convert. It is very small, so you should easily be able to alter to suit. Here is an example (allbeit in VB), that when run first time, will create some TextBoxes and give each a Name and Text/ When the application is closed, will save the data. When next run, will find the data file and recreate the TextBoxes. Just for confirmation, a Combobox will show the data and whether from a new creating of TetBoxes or from the recreation of file data. ![5071-annotation-2020-03-19-204907.png][1] [1]: /api/attachments/5071-annotation-2020-03-19-204907.png?platform=QnA Option Strict On Option Explicit On Public Class Form1 Dim tbs As New List(Of TextBox) Dim FilePath As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFile.txt") Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing ' save the textboxes Using sw As New IO.StreamWriter(FilePath) For Each tb As TextBox In tbs sw.WriteLine(tb.Name & "," & tb.Text) Next End Using End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load If IO.File.Exists(FilePath) Then ' found data,so recreate TBS again Using sr As New IO.StreamReader(FilePath) Dim ln As String = Nothing Do ln = sr.ReadLine If Not ln = Nothing Then Dim ntb As New TextBox Dim a() As String = ln.Split(","c) If a.Length = 2 Then With ntb .Name = ln.Split(","c)(0) .Text = ln.Split(","c)(1) End With tbs.Add(ntb) End If End If Loop Until ln = Nothing End Using Label1.Text = "Textboxes re-created from file" Else ' no saved data found, so ' create 6 textboxes For i As Integer = 1 To 6 Dim ntb As New TextBox With {.Name = "TextBox" & i.ToString, .Text = "Text " & i.ToString} tbs.Add(ntb) Next Label1.Text = "Textboxes created by code" End If With ComboBox1 .DataSource = tbs .DisplayMember = "Text" End With End Sub End Class

    0 comments No comments