Setting Text Programmatically
At times you may need to set text programmatically in your application. For example, you might want to set the text of a label dynamically at run time. There are two main ways to retrieve localized text:
- Retrieving the text from the MM .NET Message Manager - This is the best option when you need to retrieve text from code within methods of a non-UI component such as a business object or rule object.
- Retrieving the text from the Windows Form or Web Form's GetMessage method - This is the best option when you need to retrieve text from code within methods of a user interface control or form.
Retrieving Localized Text from the Message Manager
The MM .NET Message Manager's GetMessage() method allows you to retrieve localized text strings from code within methods of non-visual components such as business object and rule objects. You can reference the Message manager object by calling mmAppBase.MessageMgr.GetMessage().The GetMessage method takes a message token you pass in as a parameter and looks this value up in the Message table's MessageKey field for the current or specified language. If a match is not found, GetMessage simply returns the original message key text instead. You can also choose to call the GetMessage overload that contains a returnMessageKeyNotFound parameter to specify whether you want to return the original message key text or a null value if the specified language/key value is not found in the Message table.
GetMessage
Here are the descriptions and signatures of the GetMessage overloads.- Returns the message associated with the specified message key for the current language. If the specified language/message value cannot be found the original messageKey value is returned.
public virtual string GetMessage(string messageKey)Public Overridable Function GetMessage(messageKey As String) As String
- Returns the message associated with the specified message key for the specified language. If the specified language/message value cannot be found the original messageKey value is returned.
public virtual string GetMessage(string messageKey, object languagePK)
Public Overridable Function GetMessage(messageKey As String, languagePK As Object) As String
- Returns the message associated with the specified message key for the specified language. If the specified language/message value cannot be found and the returnMessage property is true, the original messageKey value is returned. If it's false, a null value is returned.
public virtual string GetMessage(string messageKey, object languagePK, bool returnMessageKeyIfNotFound)
Public Overridable Function GetMessage(messageKey As String, languagePK As Object, returnMessageKeyIfNotFound As Boolean) As String
Example
Here's an example of how to use the mmMessageBox.Show method:
In C#:
string EnterValueMessage;
if (mmAppBase.Localize)
EnterValueMessage = mmAppBase.MessageMgr.GetMessage("You must enter a value");
else
EnterValueMessage = "You must enter a value";And in VB .NET:
Dim EnterValueMessage As String
If mmAppBase.Localize Then
EnterValueMessage = mmAppBase.MessageMgr.GetMessage("You must enter a value")
Else
EnterValueMessage = "You must enter a value"
End IfNotice this code checks the value of the application object's Localize flag. If the application is localized, a call is made to the Message Manager's GetMessage method to retrieve a localized version of the text. If it's not localized, a hard-coded English string is simply stored in the variable. You don't need to perform this check if your application is always localized in all deployment scenarios.
Retrieving Text from the Form's GetMessage Method
The MM .NET WPF, Windows Forms and Web base form have a GetMessage method that acts as a wrapper for the Message Manager's GetMessage method. If you have access to an MM .NET Windows or Web form at the point you need to retrieve localized text, you should call the form's GetMessage method rather than calling the GetMessage method of the Message Manager object directly, because:- It automatically performs the check for mmAppBase.Localize and returns the original message text if the Localize flag is false
- It automatically uses the form's CurrentLanguage property value when calling the Message Manager's GetMessage method
This form-level method accepts a single messageKey parameter that it passes through to the Message Manager's GetMessage method. It also retrieves the current user's language and passes this as a second parameter. If a user-level current language has been specified, this language is used to retrieve the message text. If it hasn't been specified, then the application-level language is used instead. For details, see the Help topic Specifying the Current Language.
Here is an example of calling the form-level GetMessage method.
In C#:
this.lblHeader.Text = this.GetMessage("Data Entry Information");And in VB .NET:
Me.lblHeader.Text = Me.GetMessage("Data Entry Information")
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/26/18
Comment or report problem with topic
