AndrewHoltzman-0185 avatar image
AndrewHoltzman-0185 asked ·

Problem saving user-created text boxes using UWP C#


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.

10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Any updates about this issue?

0 Votes 0 · ·

Hi Roy,

Sorry for not getting back to you sooner. I am going to attempt to save each textbox's text into a file and then read it by creating new textboxes (with the stored text) upon reopening the program.

I will get back to you once I have tried implementing this.

Thanks for your help,


0 Votes 0 · ·
RoyLi-MSFT avatar image RoyLi-MSFT AndrewHoltzman-0185 ·

Never mind. Please let me know if you still have any problems with this issue.

0 Votes 0 · ·
RoyLi-MSFT avatar image
RoyLi-MSFT answered ·


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,

2 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Roy,

Thanks for the input on my problem. I agree with having to save the properties of the TextBoxes into local files. However, I checked out the documentation you linked in your answer and I can't seem to understand how to relate it to my program.

In my program, I create a TextBox upon the user clicking a button. The properties are as follows:

 TextBox textBox1 = new TextBox(); 
 textBox1.Text += day1T.Text; 
 textBox1.Text += day1T2.Text; 
 textBox1.VerticalAlignment = VerticalAlignment.Top;

This allows me to give the TextBox properties and add it to a certain StackPanel in my XAML page.

My question now do I write the code to save these properties so I can later read them? I have checked out the ApplicationData UWP sample and have implemented the local folder, but still do not know how to apply saving to my program.

Thanks for any help in advance.

0 Votes 0 · ·
RoyLi-MSFT avatar image RoyLi-MSFT AndrewHoltzman-0185 ·

I need to confirm one question first. Are all the TextBoxes created as same as the code you posted? If all of the TextBoxes are created with the same style, then you will only need to save the different text into local files. You need to save the text in your TextBox into files and then load it when you need to re-create the TextBoxes.

If you don't have much text, you could try another way - saving text into local settings. Here is the document: Save app settings. This way might be more clear for you because you could save dictionary- key(TextBox name) & value(text) into the settings. But if you have much text, it would be better to save them into local files.

0 Votes 0 · ·
$$ANON_USER$$ avatar image
$$ANON_USER$$ answered ·

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]: /answers/storage/attachments/5071-annotation-2020-03-19-204907.png 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

10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.