Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi. i am creating a win form. in my form i have added do while loop.. i have added TABLE LAYOUT PANEL ..in this, the text box is declared and it creates text boxes inside the layout panel.. but my problem is i can read file but if i want to change the text written in text box, i am not able to save the file.. i want to replace the old content of a file and then overwrite it..plz help me
Posted
Comments
ZurdoDev 15-May-13 7:19am    
What does table layout panel have to do with saving a file? You need to post relevant code.
shashank 1068 15-May-13 13:00pm    
Dim path As String = "c:\temp\MyTest.txt"

Try
Dim sr As StreamReader = New StreamReader(path)
Dim line As String = ""

Do While sr.Peek() >= 0
line = sr.ReadLine()
Dim newTextBox As TextBox = New TextBox()
newTextBox.Text = line
Panel1.Controls.Add(newTextBox)
Loop
sr.Close()
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.ToString())
End Try
End Sub
this is my code for displaying data in textbox
but if i want to modify the tex in textbox
so i want to save the data after writing new text in textbox
ZurdoDev 15-May-13 13:29pm    
I would suggest what was suggested in Solution 1.
shashank 1068 15-May-13 13:37pm    
u mean bout this code??
File.WriteAllLines(path, myTextBox.Lines)
ZurdoDev 15-May-13 13:38pm    
Yes, just use 1 textbox.

1 solution

I assume that you do something like read each line from the file into a separate TextBox?
If so, then you need to loop through all the Textboxes in the panel (you can use its Controls collection to access them) and write each line back to a separate file.

But the question is: why are you doing that? Why not just set a single TextBox to MultiLine=true and use that for the whole file? Then it is just a case of:
VB
myTextBox.Lines = File.ReadAllLines(path)
And
VB
File.WriteAllLines(path, myTextBox.Lines)
 
Share this answer
 
Comments
shashank 1068 15-May-13 7:09am    
actually i want that the textbox should b displayed according to the content present in the file..suppose a file is having 20 URL so it should display only 20 textbox... so for this i have added loop...
but now i want to save the file like if i change any content of textbox i want to replace the older file and overwrite the new file
OriginalGriff 15-May-13 7:13am    
So loop through them, and write it out.
What part of that is giving you difficulty?
shashank 1068 15-May-13 7:15am    
saving part...
i don't know how to save because in this the textbox is not declared..
i am not having textbox id or name
so i am facing problem in saving part
OriginalGriff 15-May-13 7:23am    
So as I said: use the panels Controls property to access all the controls it contains.
Cast them to TextBoxes and you have them ready to rock'n'roll.
shashank 1068 15-May-13 10:47am    
i have used table layout panel
i am able to read the file but i am not able to save them

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