Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Couldn't change the label's text!
 

Couldn't change the label's text!

I write a program to change from one language to another, the button and tabpage is ok to change, but the label's text couldn't change. Why? Please help me.

String^ vtest;
String^ vText;
String^ SelectCmd="Select * from ChineseToSChinese";
OleDbConnection^ MyConn20=gcnew OleDbConnection(ConnStr);
DataSet^ MyDataSet20=gcnew DataSet();
OleDbDataAdapter^ MyAdapter20=gcnew OleDbDataAdapter(SelectCmd,MyConn20);
MyAdapter20->Fill(MyDataSet20,"ChineseToSChinese");
SelectCmd="Select * from ChineseToEnglish";
OleDbConnection^ MyConn21=gcnew OleDbConnection(ConnStr);
DataSet^ MyDataSet21=gcnew DataSet();
OleDbDataAdapter^ MyAdapter21=gcnew OleDbDataAdapter(SelectCmd,MyConn21);
MyAdapter21->Fill(MyDataSet21,"ChineseToEnglish");
for (int i=0; i<Ctl->Controls->Count; i++) {
if (Ctl->Controls[i]->GetType() == Label::typeid) {
if (vLanguage==1) {
array<DataRow^>^ vRow=MyDataSet20->Tables["ChineseToSChinese"]->Select("Name='"+Ctl->Controls[i]->Text+"'");
if (vRow->Length>0)
Ctl->Controls[i]->Text="test"; <--couldn't change the label's text
//Ctl->Controls[i]->Text=(String^)vRow[0][1];
}
else if (vLanguage==2) {
array<DataRow^>^ vRow=MyDataSet20->Tables["ChineseToEnglish"]->Select("Name='"+Ctl->Controls[i]->Text+"'");
if (vRow->Length>0)
Ctl->Controls[i]->Text=(String^)vRow[0][1];
}
}
else if (Ctl->Controls[i]->GetType() == Button::typeid) {
array<DataRow^>^ vRow=MyDataSet20->Tables["ChineseToSChinese"]->Select("Name='"+Ctl->Controls[i]->Text+"'");
if (vRow->Length>0)
//Ctl->Controls[i]->Text=(String^)vRow[0][1];
Ctl->Controls[i]->Text="Test";<-- can change the text of button
}
else if (Ctl->Controls[i]->GetType() == ComboBox::typeid)
;
else if (Ctl->Controls[i]->GetType() == TabPage::typeid)
;
if (Ctl->Controls[i]->Controls->Count != 0)
TransText(Ctl->Controls[i]);
}
MyConn20->Close();
MyConn21->Close();
energybody  Tuesday, April 21, 2009 2:59 AM
Hi,

Thank you for your reply!

I performed a test based on your sample code but didn't reproduce the problem on my side. In my test, I create a VC++ CLR Windows Forms Application project and add a Label control on the Form1. In the form's Load event handler, I add the following code:

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
for(int i=0;i<this->Controls->Count;i++)
{
if(this->Controls[i]->GetType() == Label::typeid)
{
this->Controls[i]->Text = "hello world!";
}
}
}

When I run the application, the text on the label is "hello world".

Please set a breakpoint on the line of code "Ctl->Controls[i]->Text="test"; <--couldn't change the label's text" in your project and press F5 to run the application. Check whether this line of code is actually executed.

Sincerely,
Linda Liu
  • Marked As Answer byenergybody Wednesday, April 29, 2009 1:48 AM
  •  
Linda Liu  Tuesday, April 28, 2009 3:05 AM
Hi,

You may set the Text property of a Label control at design time and open the form's designer file. Navigate to the InitializeComponent method to see how the Text property of the Label control is assigned.

Hope this helps.

Sincerely,
Linda Liu
Linda Liu  Friday, April 24, 2009 10:31 AM
I use the L"test", but it's the same couldn't change the label's text. I use the direct label's instruction to change the label's text. It works. Does it need some more skill.
energybody  Monday, April 27, 2009 2:41 AM
Hi,

Thank you for your reply!

I performed a test based on your sample code but didn't reproduce the problem on my side. In my test, I create a VC++ CLR Windows Forms Application project and add a Label control on the Form1. In the form's Load event handler, I add the following code:

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
for(int i=0;i<this->Controls->Count;i++)
{
if(this->Controls[i]->GetType() == Label::typeid)
{
this->Controls[i]->Text = "hello world!";
}
}
}

When I run the application, the text on the label is "hello world".

Please set a breakpoint on the line of code "Ctl->Controls[i]->Text="test"; <--couldn't change the label's text" in your project and press F5 to run the application. Check whether this line of code is actually executed.

Sincerely,
Linda Liu
  • Marked As Answer byenergybody Wednesday, April 29, 2009 1:48 AM
  •  
Linda Liu  Tuesday, April 28, 2009 3:05 AM
Thanks a lot. It can work now.
energybody  Tuesday, April 28, 2009 3:32 AM
Can I change the combobox's item same way as above.
energybody  Tuesday, April 28, 2009 3:50 AM

Hi,

> It can work now.
I'm glad to hear the problem is solved now. But could you tell us how you solve the problem?

> Can I change the combobox's item same way as above.
The items in a ComboBox doesn't have a Text property. You can set a string to an item in a ComboBox directly. For example:
this->comboBox1->Items[0] = "item 3";

Sincerely,
Linda Liu

Linda Liu  Tuesday, April 28, 2009 7:06 AM
I don't know why can't I change the text before. But NOW can change the text.It'sthe same as I wrote up.

String^ SelectCmd="Select * from ChineseToSChinese";
OleDbConnection^ MyConn20=gcnew OleDbConnection(ConnStr);
DataSet^ MyDataSet20=gcnew DataSet();
OleDbDataAdapter^ MyAdapter20=gcnew OleDbDataAdapter(SelectCmd,MyConn20);
MyAdapter20->Fill(MyDataSet20,"ChineseToSChinese");
SelectCmd="Select * from ChineseToEnglish";
OleDbConnection^ MyConn21=gcnew OleDbConnection(ConnStr);
DataSet^ MyDataSet21=gcnew DataSet();
OleDbDataAdapter^ MyAdapter21=gcnew OleDbDataAdapter(SelectCmd,MyConn21);
MyAdapter21->Fill(MyDataSet21,"ChineseToEnglish");
for (int i=0; i<Ctl->Controls->Count; i++) {
if (Ctl->Controls[i]->GetType() == Label::typeid || Ctl->Controls[i]->GetType() == Button::typeid || Ctl->Controls[i]->GetType() == TabPage::typeid) {
if (vLanguage==1) {
array<DataRow^>^ vRow=MyDataSet20->Tables["ChineseToSChinese"]->Select("Name='"+Ctl->Controls[i]->Text+"'");
if (vRow->Length>0)
Ctl->Controls[i]->Text=(String^)vRow[0][1];
else {
String^ InsertCmd="Insert into ChineseToSChinese(Name) Values('"+Ctl->Controls[i]->Text+"')";
OleDbConnection^ Conn10=gcnew OleDbConnection(ConnStr);
OleDbCommand^ Cmd=gcnew OleDbCommand(InsertCmd,Conn10);
Conn10->Open();
Cmd->ExecuteNonQuery();
Conn10->Close();
}
}
else if (vLanguage==2) {
array<DataRow^>^ vRow=MyDataSet20->Tables["ChineseToEnglish"]->Select("Name='"+Ctl->Controls[i]->Text+"'");
if (vRow->Length>0)
Ctl->Controls[i]->Text=(String^)vRow[0][1];
else {
String^ InsertCmd="Insert into ChineseToEnglish(Name) Values('"+Ctl->Controls[i]->Text+"')";
OleDbConnection^ Conn10=gcnew OleDbConnection(ConnStr);
OleDbCommand^ Cmd=gcnew OleDbCommand(InsertCmd,Conn10);
Conn10->Open();
Cmd->ExecuteNonQuery();
Conn10->Close();
}
}
}
else if (Ctl->Controls[i]->GetType() == ComboBox::typeid)
;
if (Ctl->Controls[i]->Controls->Count != 0)
TransText(Ctl->Controls[i]);
}
MyConn20->Close();
MyConn21->Close();
energybody  Tuesday, April 28, 2009 8:52 AM

You can use google to search for other answers

Custom Search

More Threads

• Controls are not moving after dropped on the Designer
• How to detect tab selection changed event in TabControl
• DesignSurface design question
• ContainerSelectorGlyph - the only reference on the whole internet!!
• How can I load another Form into the current Form?
• Dropping project components onto a form
• "base class <...> could not be loaded" when compiling with /clr
• Access key. button is invoked without pressing Alt
• Visual Studio 2005 Reinstallation Critical problem
• view designer error - there is no editor available for c:\tmp\windowsapplication1\form1.vb file