-
Save control´s caption in a txt file
hey there,
i´m trying to create some kind of function to save all the labels and buttons caption of all forms of my proyect to a txt file.
I´ve tried taking one by one to this file but the fact is that they are too much and it would be a really long proccess to do it that way so i´m looking for some way to do it automatically
the structure of the file would be:
Name of the form - name of the control - caption
i need this because i´m creating a program that has to be in more than 1 language and so the idea would be to have this in file and select the right captions when i select them through a button
I hope somebody can help me with this,
thanks
-
Re: Save control´s caption in a txt file
Or you could use a resource file which is designed for this scenario, simple example here.
-
Re: Save control´s caption in a txt file
Man i checked the localizable thing but it is the same as taking all the captions to a file by myself
i need something else to do it
-
Re: Save control´s caption in a txt file
Try something like
Code:
For each ctrl as Control in MyForm
' Check if its a label \ button (control gettype)
' Write to a file
Next
Do this for all forms
-
Re: Save control´s caption in a txt file
If you don't want to use the built-in localization mechanisms, then you are going to have to do it manually (which recreates the built-in functionality).
Essentially, you will need to call a method for every control with a caption and set the caption according to a locale. each time you change the locale, you will read the appropriate file.
-
Re: Save control´s caption in a txt file
hey guys i´ve been working on it and i´ve found something that seems to work but not al all i don´t know why maybe you can help me to correct it
(using the code gave by amrita i came to this)
i have a button with click event with this:
( i have a form called clients)
Code:
For Each ctrl As Control In clients.Controls
Dim ctrlname As String = ctrl.Name
Dim ctrltext As String = ctrl.Text
Dim text As String
If ctrl.GetType Is GetType(TextBox) Then
text = "clients" & "." & ctrl.Name & "." & "text" & "=" & ctrl.Text
Const fic As String = "C:\translations.txt"
Dim sw As New System.IO.StreamWriter(fic, True)
sw.WriteLine(text)
sw.Close()
Else
If ctrl.GetType Is GetType(Label) Then
text = "clients" & "." & ctrl.Name & "." & "text" & "=" & ctrl.Text
Const fic As String = "C:\translations.txt"
Dim sw As New System.IO.StreamWriter(fic, True)
sw.WriteLine(text)
sw.Close()
End If
End If
Next ctrl
When I use this code the program save just 1 control in it but in the way i want it so in some point it works.
but there must be some problem with the for because it doesn´t go to the next control
anybody can help me with this
-
Re: Save control´s caption in a txt file
Here's 2 functions:
Code:
Public Sub SaveToFile(ByVal f As Form, ByVal location As String)
Dim sw As New IO.StreamWriter(location)
Dim s As New Stack(Of Control)
s.Push(f)
While s.Count > 0
Dim pc As Control = s.Pop()
For Each c As Control In pc.Controls
sw.WriteLine(GetPath(f, c) & "=" & c.Text)
s.Push(c)
Next
End While
sw.Close()
sw.Dispose()
End Sub
Public Sub RestoreFromFile(ByVal f As Form, ByVal location As String)
Dim sr As New IO.StreamReader(location)
Do
Dim s As String = sr.ReadLine()
If s = "" Then Exit Do
GetControl(f, s.Substring(0, s.IndexOf("="c))).Text = s.Substring(s.IndexOf("="c) + 1)
Loop
sr.Close()
sr.Dispose()
End Sub
Private Function GetControl(ByVal c As Control, ByVal path As String) As Control
Dim i As Integer = path.IndexOf("."c)
If i = -1 Then Return c.Controls(path)
Dim s As String = path.Substring(0, i)
Return GetControl(c.Controls(path), path.Substring(i + 1))
End Function
Private Function GetPath(ByVal f As Form, ByVal c As Control) As String
Dim s As String = c.Name
While c.Parent IsNot f AndAlso c.Parent IsNot Nothing
c = c.Parent
s = c.Name & "." & s
End While
Return s
End Function
To use them in your form:
Code:
'To save
SaveToFile(Me, "data.dat")
'To load
RestoreFromFile(Me, "data.dat")
-
Re: Save control´s caption in a txt file
-
Re: Save control´s caption in a txt file
Quote:
Originally Posted by
minitech
Here's 2 functions:
Code:
Public Sub SaveToFile(ByVal f As Form, ByVal location As String)
Dim sw As New IO.StreamWriter(location)
Dim s As New Stack(Of Control)
s.Push(f)
While s.Count > 0
Dim pc As Control = s.Pop()
For Each c As Control In pc.Controls
sw.WriteLine(GetPath(f, c) & "=" & c.Text)
s.Push(c)
Next
End While
sw.Close()
sw.Dispose()
End Sub
Public Sub RestoreFromFile(ByVal f As Form, ByVal location As String)
Dim sr As New IO.StreamReader(location)
Do
Dim s As String = sr.ReadLine()
If s = "" Then Exit Do
GetControl(f, s.Substring(0, s.IndexOf("="c)).Text = s.Substring(s.IndexOf("="c) + 1)
Loop
sr.Close()
sr.Dispose()
End Sub
Private Function GetControl(ByVal c As Control, ByVal path As String) As Control
Dim i As Integer = path.IndexOf("."c)
If i = -1 Then Return c.Controls(path)
Dim s As String = path.Substring(0, i)
Return GetControl(c.Controls(path), path.Substring(i + 1))
End Function
Private Function GetPath(ByVal f As Form, ByVal c As Control) As String
Dim s As String = c.Name
While c.Parent IsNot f AndAlso c.Parent IsNot Nothing
c = c.Parent
s = c.Name & "." & s
End While
Return s
End Function
To use them in your form:
Code:
'To save
SaveToFile(Me, "data.dat")
'To load
RestoreFromFile(Me, "data.dat")
man i tried to use your code but
but there is an error onthis line :
Code:
GetControl(f, s.Substring(0, s.IndexOf("="c)).Text = s.Substring(s.IndexOf("="c) + 1)
there is an extra parentheses an also the .text is not a property of that
-
Re: Save control´s caption in a txt file
Sorry, I forgot a parenthesis. It should be:
Code:
GetControl(f, s.Substring(0, s.IndexOf("="c))).Text = s.Substring(s.IndexOf("="c) + 1)
-
Re: Save control´s caption in a txt file
Quote:
Originally Posted by
minitech
Sorry, I forgot a parenthesis. It should be:
Code:
GetControl(f, s.Substring(0, s.IndexOf("="c))).Text = s.Substring(s.IndexOf("="c) + 1)
sorry for my ignorance but how should i use your code
cause if it is just using the save to file and restore from file
the result is a file with this text on it : Button1 = Button1
-
Re: Save control´s caption in a txt file
Isn't that right, though? Button1's text is Button1 if you just added it to the form and did nothing to it...
I already told you how to use my code:
Quote:
To use them in your form:
Code:
'To save
SaveToFile(Me, "data.dat")
'To load
RestoreFromFile(Me, "data.dat")
-
Re: Save control´s caption in a txt file
Quote:
Originally Posted by
minitech
Isn't that right, though? Button1's text is Button1 if you just added it to the form and did nothing to it...
I already told you how to use my code:
ok the thing is that the way i need it is this..
for example the control is a textxbox, its name is btnexit and its text is exit
i would like the functions to save the control to the file in this way
Code:
btnexit.text = exit
the same way you write it as code to change a button text so i can have it several times to change the text to another language
anyway thanks for your attention man thanks a lot
-
Re: Save control´s caption in a txt file
Oh, okay, I see what you're saying here - you want it to generate code instead?
The way I designed it is so that you can save and load from your application, not to generate code. Does it really make a difference?
If it does, just add and remove .Text in the methods:
Code:
GetControl(f, s.Substring(0, s.IndexOf("="c)).Remove(s.IndexOf("="c)-5,5)).Text = s.Substring(s.IndexOf("="c) + 1)
and
Code:
sw.WriteLine(GetPath(f, c) & ".Text" "=" & c.Text)
-
Re: Save control´s caption in a txt file
ok your are getting close that is the way i need the file to be saved but it not the idea yet. sorry if this is getting annoying
what i need is a function that recongizes all the controls in a form (textboxes, labels and buttons)
and save them in the file we were talking about in that format do you understand ??
-
Re: Save control´s caption in a txt file
Yes, and the code I posted does exactly that.
-
Re: Save control´s caption in a txt file
ohh ok now it works right i was running it the wrong place.
this is the last thing i ask you you´ve been really kind with me
can you change the function to do that but with all the forms in a project at the same time
i promise it will be the last thing i ask you
hehe thanks man for all this
-
Re: Save control´s caption in a txt file
Unfortunately, I can't - I don't know the names of the forms in your projects. It's possible to loop through all the open forms, but that seems like a bad solution. Is it really so hard to do them all separately?
-
Re: Save control´s caption in a txt file
The code must be in a running form, so you have to create an instance of every form in your project and run this method against it.
-
Re: Save control´s caption in a txt file
Or use the default instance :P Although I hate the default instance, it seems to be what you're using.
-
Re: Save control´s caption in a txt file
Quote:
Originally Posted by
minitech
Unfortunately, I can't - I don't know the names of the forms in your projects. It's possible to loop through all the open forms, but that seems like a bad solution. Is it really so hard to do them all separately?
the problem is that there are like 6 forms right now and i think i´m gonna add a lot more and every form is have a lot of controls and it is really long to do it that way
i can give you the name of the forms right now:
clients
report
login
menu
services
employees
do just one and then i copy it using the other forms names ok?
thanks
-
Re: Save control´s caption in a txt file
If you only have 6 forms I'm telling you good advise, use the built-in localisation method I gave at the start. I have apps with over 100 forms and believe me, laods of them have custom controls that do not just have a .Text property. Also have tab captions, tooltips, menuitems, print controls, filenames.
In your loop above mini, does it check for children of children, I couldn't see that it did.
-
Re: Save control´s caption in a txt file
Code:
SaveToFile(clients, "clients.dat") 'Uses default instance :P
'or, to load
RestoreFromFile(clients, "clients.dat")
EDIT:
@Grimfort: Yes it does, look at the Stack(Of Control) loop.
Every control has a .Text property that can be hidden, but not removed. It'll usually contain either nothing or the name of the control and a number. To save some code and time, I just save every control. It's OK on efficiency. And it is much better to use built-in localization, but for some reason the OP just doesn't want to.
-
Re: Save control´s caption in a txt file
I was just about to edit my post when I saw it, I've not used that method before, might be useful on a treeview as well with massive number of layers. Nice one.
-
Re: Save control´s caption in a txt file
It took me a while to figure it out even though it's so simple :D
-
Re: Save control´s caption in a txt file
so then the best way to do it is to use the localizable attribute in the forms.
I´ve read the link about that but i don´t understand how it works
any idea?
-
Re: Save control´s caption in a txt file
No, it has little to do with the Localizable attribute because that's set for you. Basically, write out all your forms in the language used most. Then, go into your project properties and change the language (My Project > Assembly Info > Language). Then change your forms to that language. Repeat until you've done all the languages you want. Then, you can use different languages by changing the language in your assembly info.
-
Re: Save control´s caption in a txt file
ok so how can i apply that in my proyect.?
i have a form where there is a combox with all the languages available to run the program you select one item from the combox and click accept and the program should run in that language how would it be?
-
Re: Save control´s caption in a txt file
I am guessing you are sending out the list of strings to a translator, there are methods (and add-ins) that do this for you. I have not used any of them but its got to be quicker to set this up now before you have bags of code to change.
-
Re: Save control´s caption in a txt file
Quote:
Originally Posted by
Grimfort
I am guessing you are sending out the list of strings to a translator, there are
methods (and add-ins) that do this for you. I have not used any of them but its got to be quicker to set this up now before you have bags of code to change.
yeah a professor told me there was some kind of plugin that could help with the translation thing or make that proccess easier i think this is what he was talking about
-
Re: Save control´s caption in a txt file
Quote:
Originally Posted by
marcelosoy1
ok so how can i apply that in my proyect.?
i have a form where there is a combox with all the languages available to run the program you select one item from the combox and click accept and the program should run in that language how would it be?
I just gave you step-by-step instructions on how to apply that in your project, really - don't use a ComboBox in your application, have a ComboBox in your installer.
Anyways, you could just keep with the 2 methods I gave you, if they're already working.