Click here to Skip to main content
15,889,739 members
Please Sign up or sign in to vote.
4.60/5 (2 votes)
See more:
Hi
I made a form with dynamically added textboxes and labels. is there a way to set property bindings to these so when a user closes and reopens the form the controls and user input will still be there.

I know I can do it with a normal added control but cant work it out for dynamic.

Thanks
Posted

No, there is no automatic way to do this: you have to store the information on what controls you added where in nonvolatile storage (ie, on disk) either with the Application settings file, or via a file of your own devising. Then reload the infor and re-add the controls next time you open the form.


"I'm not sure I know what you mean
I know how to use for each loop I think
like this?

VB
For Each ctrl In FlowLayoutPanel1.Controls
     If TypeOf ctrl Is TextBox Then
         If ctrl.Name = ask Then
             ctrl.Dispose()
         End If
     End If
 Next ctrl

I don't really understand how to write that code"


Yep - that's a For Each loop and I'll just digress into them a bit.
For Each is one of the simplest kinds of loop - it works with (almost) every collection from Arrays to Lists, and so on (basically anything which is in the IEnumerable "club" can go in a For Each loop, but don;t worry about that just yet)
What is does is takes each item in the collection in turn, and executes the code in the loop using that instance. It's like a pile of coins: if you want to know how many are Heads and how many are Tails, you take each one of the top of the pile in turn and look at it, write down the Head or Tail and move on to the next.

In your code the Collection is FlowLayoutPanel1.Controls and each Control is put into the ctrl variable in turn.

You can do this with strings as well:
VB
Dim lines As String() = File.ReadAllLines("D:\Temp\myData.txt")
For Each line As String In lines
    ...
Next

So where you want to start is:
VB
Dim lines As String() = File.ReadAllLines("D:\Temp\myData.txt")
For Each line As String In lines
    ' Process each line into two control texts
    Dim input As String = "label data|textbox data"
    Dim parts As String() = input.Split("|"C)
    Dim forTheLable As String = parts(0)
    Dim forTheTextbox As String = parts(1)
    ' And create the controls
    ...
Next
Then all you have to do is extract the code from your click event handler into here and you're done.

Does that make sense?
 
Share this answer
 
v2
Comments
Member 10439491 23-Dec-13 6:28am    
Hmmm how do I do that? sorry still learning :)
OriginalGriff 23-Dec-13 7:10am    
There are a *huge* number of ways to do that.
Do you know how to use settings? Text files, CSV, XML or databases?
We don't know what your skills or experience are, so we can;t just say "Do this" because the solution I would use might confuse you horribly, or not be appropriate to your application...
Member 10439491 23-Dec-13 7:26am    
Well the simple answer would be no I don't know. I've only made 1 pretty basic form for personal use (income and outgoings) it adds up all income and adds up all outgoings and puts the value in a textbox. there's a few other things but that's basically what it does. I have no idea what the best way to store all the data entered would be. I guess a added button for save/load or something like that
OriginalGriff 23-Dec-13 7:47am    
If youre trying to store data, then that is something where you need to be quite careful - because controls are pretty easy: they have fixed properties that you need, like the Location and what type of control it is.
Data gets to be another cup of fun-and-games, because it can be pretty much anything your user wants to type, and that can make it more complex. And the difficulty is never in storing the data, it's in getting it back...:laugh:
There are a couple of simple ways to store and retrieve data, but what I'm going to suggest is all about storing sets of data, rather than individual items - because I think that is what you are trying to do.
Using textboxes and labels dynamically is probably not the best way to do this (as you will learn later there are much better controls for that kind of thing, but I'll not confuse you more than I have to)
So the first thing to do is collect your data together so you can process it using loops of some form. Do you know what an Array is? List? DataTable? The simplest form of storage is to just output your data to a file, with each textbox or label on a separate line.
Assuming you know what an array is, if you have an array of strings, you can store them all easily with:
File.WriteAllLines("D:\Temp\MyFile.txt", myArrayOfStrings)
and read them all back with:
Dim myArrayOfStrings = File.ReadAllLines("D:\Temp\MyFile.txt")


(Sorry if this comes over at all patronising - the problem is that I don't know how much you do know, So I'm trying to find out what I can tell you that will work, but now bog you down in concepts you haven;t met yet.)
Member 10439491 23-Dec-13 8:06am    
I can use arrays sort of, I don't know how to use list or datatable. I originally was using an array of all my textboxes because I thought I would know how many textboxes and labels there would be and made an array of there names but after using my form I realised I wouldn't actually know how many textboxes or labels there would be so that why I changed to dynamically adding them to a scrollable flowlayoutpanel.
Here is my code to add textboxes and labels

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim ask As String
ask = InputBox("Please name your Income", "Income")
If ask = Nothing Then
Exit Sub
End If
Dim label As New Label()
Dim count As Integer = FlowLayoutPanel1.Controls.OfType(Of Label)().ToList().Count
label.Location = New Point(10, (25 * count) + 2)
label.Size = New Size(176, 20)
label.Name = "label_" & (count + 1)
label.Text = ask
FlowLayoutPanel1.Controls.Add(label)

Dim textbox As New TextBox()
count = FlowLayoutPanel1.Controls.OfType(Of TextBox)().ToList().Count
textbox.Location = New System.Drawing.Point(60, 25 * count)
textbox.Size = New System.Drawing.Size(80, 20)
textbox.Name = ask
textbox.Text = CDec(IIf(Decimal.TryParse(textbox.Text, Nothing), textbox.Text, "0"))
FlowLayoutPanel1.Controls.Add(textbox)

AddHandler textbox.KeyPress, AddressOf TextBox_keypress
AddHandler textbox.Click, AddressOf textbox_click

If label.Text <> "" Then
Dim a As String = label.Text
label.Text = (StrConv(a, VbStrConv.ProperCase))
End If
End Sub

Ive done it like that so if a user wants to remove something with my remove button they type the exact name of what the control is example: Wages and the textbox and label will be removed.

I don't know if I should think about redoing things if I have just made things very difficult for myself.
You have to save the Data somewhere otherwise you won't be able to access it again.
So, store in Database or files and read them again while loading the page.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900