Dynamic...problem

I am trying to make a dynamic function...

Page 1 of 1

8 Replies - 1755 Views - Last Post: 08 February 2009 - 05:51 PM Rate Topic: -----

#1 krum110487   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Dynamic...problem

Post icon  Posted 07 February 2009 - 06:34 PM

I program often, and I have come across this problem more than once...

I have the following code:
Private Sub RefreshLang()
		Dim cControl As Control

		For Each cControl In Me.Controls
			cControl.Text = My.Resources.Jap.(cControl.name)
		Next cControl
	End Sub



I know this doesn't work and I know why, BUT my question is, is it possible to make a dynamic dot notation?

I have the strings in my resource match the name of my controls, I would like to be able to loop through and change the text according to the resource.

so if I have a string in the resource named "test" and a button named "test" then the code would run as...
test.Text = My.Resources.Jap.test


I have look for hours, I can't seem to find the right words to describe my situation... is it a method, assigning a value, type.

I have seen similar things with vbscript and reflection, but I can not get them to work, if someone could dumb it down for me that would be great.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic...problem

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamic...problem

Posted 07 February 2009 - 08:42 PM

So are your controls bound to the application settings?
You can change any of the settings on code:
My.Settings.text = ...
It almost sound like you want all the controls to change at the same time.
If you make a local database, you could set each controls text property to the field in your database.
Does this sound right or am I in left field?

This post has been edited by hawkvalley1: 07 February 2009 - 09:10 PM

Was This Post Helpful? 0
  • +
  • -

#3 krum110487   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Dynamic...problem

Posted 07 February 2009 - 10:55 PM

Thanks for the response!

I do want all of the controls to change at the same time...

I have to make the program with partial Japanese, so I use a unicode text to show the japanese, and I planned to load them from a resource.

there are 2 resources Eng and Jap, obviously one is english and one is japanese. I figured it would be easiest to name the string name the same as the object it will be applied to.

my program works fine when it comes to changing all the controls to the same thing, but I wish to change all of the controls to their respective text.

I can't seem to make a dynamic expression, or load the resources into a string array... either would do although I would love to know if it is possible to dynamically load certain strings.

Better Example:
resource Eng:
button1 = "Japanese"
button2 = "test"

resource Jap:
button1 = "English"
button2 = "test2"



psuedo code:
loop through all controls
   control.text = resources.[resource].[control.name]
next



result:
   button1.text = my.resources.Jap.button1
   button2.text = my.resources.Jap.button2



which would translate to:
   button1.text = "English"
   button2.text = "test2"



I will choose the resource name by an if-than statement...

if this doesn't work out, I plan to load an array to do so if that means without the resources then so be it... unless you can tell me how to load all the resources into an array.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamic...problem

Posted 08 February 2009 - 04:23 AM

The reason I suggested the database is, in your first column you would have the language name then each column for each control then have two rows
1st row/column 1 English, column 2 text, column 3 text ...
2cd row/column 1 Japanese, column two, column 3 text...
I have something similar where multiple fields change as I scoll thought my database.
You would need a control for changing the language - like a dropdown/radio button etc... . Then when you change that control each of your controls you had bound to the text property would change with it. Changing each of the control via resources would be difficult as you would have to change each one (1 line of code for each one). Even though you don't need the database for huge info staorge it has some other advantages, like being bound to a control. Not that you can't do that with the resources but you can't change the list as easy.
The thing I am not sure about is the Japanese language - it is symbols (or they look like symbols to me) right?
SQL 2008 Express is free. Access is fine too...
Works great with VS.
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamic...problem

Posted 08 February 2009 - 06:35 AM

Ok I know it was early and all, but let me correct myself, you would need a column for each control with a name you would recognize for them, two rows the the English and Japanese text. The only other idea I have is to use the .tag property of each control, then write a sub that searches all the controls that have a .tag of "jap" then change the language unicode to Japanese and then a sub to change them back - though I don't know if that will work - something to check on. Also check this site out:

http://www.iconico.com/UniToolbox/

Don :ph34r:
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamic...problem

Posted 08 February 2009 - 07:13 AM

Also with my last part, could you not change the font the Japanese charaters, never tried anything like this, but might work.
Was This Post Helpful? 0
  • +
  • -

#7 krum110487   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Dynamic...problem

Posted 08 February 2009 - 04:49 PM

View Posthawkvalley1, on 8 Feb, 2009 - 06:13 AM, said:

Also with my last part, could you not change the font the Japanese charaters, never tried anything like this, but might work.


thank you for the reply!

yes I have gotten the japanese text to work by simply changing the font to Unicode.

reading from the resource is not the problem, I believe I would have the same problem with a database. I could change every box via switch statement, but that could get very long...

At the time being, I am able to change all of the controls to a single string, but the problem comes on changing EVERY control to a different string.

cControl.text = - will cycle through every object...


but the right side of that equation can't change dynamically, so even with a database I will have trouble setting every control to a different name.



Is there some way of putting in a place holder so that the right side of the equation can be dynamic?

OR

can someone point me into the right direction to loop through a resource, that way I can simply read it into an array.

EDIT:
I just read your second post again, and I could make it switch the .tag property and the .text property, that is worth a try...but I would still like to know if the right side of an equation is possible to be dynamic like I wanted...if not someone should get on that :-P

This post has been edited by krum110487: 08 February 2009 - 04:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Dynamic...problem

Posted 08 February 2009 - 05:05 PM

I found a way, that was like you asked, you would have to have two My.Settings for each control, i.e. My.Settings.button1 and My.Settings.button1jap, then your code would be :

For Each ctr as control in me.controls
dim i as string = My.Settings(ctr.name)
if ctr.name = i then
else
dim ii as string = My.Settings(ctr.name & "jap")
ctr.text = ii
end if
next

Glad to hear you found an answer.

Don :ph34r:
Was This Post Helpful? 0
  • +
  • -

#9 krum110487   User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Dynamic...problem

Posted 08 February 2009 - 05:51 PM

View Posthawkvalley1, on 8 Feb, 2009 - 04:05 PM, said:

I found a way, that was like you asked, you would have to have two My.Settings for each control, i.e. My.Settings.button1 and My.Settings.button1jap, then your code would be :

For Each ctr as control in me.controls
dim i as string = My.Settings(ctr.name)
if ctr.name = i then
else
dim ii as string = My.Settings(ctr.name & "jap")
ctr.text = ii
end if
next

Glad to hear you found an answer.

Don :ph34r:


thanks!

I ended up doing it like so...

Private Sub RefreshLang()
		Dim cControl As Control
		Dim Temp As String
		Dim menues As New List(Of ToolStripMenuItem)

		For Each t As ToolStripItem In MenuStrip1.Items
			GetMenues(t, menues)
		Next

		For Each t As ToolStripMenuItem In menues
			Temp = t.Text
			If t.Tag = "" Then
				t.Text = "EMPTY"
			Else
				t.Text = t.Tag
			End If
			t.Tag = Temp
		Next

		For Each cControl In Me.Controls
			Temp = cControl.Text
			If cControl.Tag = "" Then
				'Do Nothing
			Else
				cControl.Text = cControl.Tag
			End If
			cControl.Tag = Temp
		Next cControl

		If Me.Tag <> "" Then
			 Temp = Me.Text
			 Me.Text = Me.Tag
			 Me.Tag = Temp
		End if
	End Sub


Public Sub GetMenues(ByVal Current As ToolStripMenuItem, ByRef menues As List(Of ToolStripMenuItem))
		menues.Add(Current)
		For Each menu As ToolStripMenuItem In Current.DropDownItems
			GetMenues(menu, menues)
		Next
	End Sub


this basically will change anything on a menustrip, control and the form to the tag, only if the tag has something within it.

thanks for the help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1