有关TreeViews和TreeNodes的问题 [英] Question on TreeViews and TreeNodes

查看:59
本文介绍了有关TreeViews和TreeNodes的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个大型程序.除1行代码外,所有其他功能都有效,对于我的一生,我不明白为什么.该程序中有一棵树,其中有1个节点.程序启动时,将选择一个节点.我想要一个使文本框在用户检查节点时读出一些文本的函数(我编写该函数只是为了测试它是否起作用).但是我一直收到C2065和C2227错误.以下是相关代码:

I am writing a huge program. It all works except 1 line of code and for the life of me I don''t understand why. The program has a tree with 1 node in it. When the program starts, the one node is selected. I wanted a function that make a text box readout some text when the node is checked on by the user (this is a function I wrote just to test if it works or not). But I keep getting C2065 and C2227 errors. Here is the relevant code:

//relevant declarations amongst thousands (which all work)
private: System::Windows::Forms::TreeNode^  treeNode1 = (gcnew System::Windows::Forms::TreeNode(L"Tree Node to be Tested"));
private: System::Windows::Forms::TreeView^  treeView1;
this->treeView1 = (gcnew System::Windows::Forms::TreeView());
	
this->treeView1->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(255)), static_cast<System::Int32>(static_cast<System::Byte>(224)), static_cast<System::Int32>(static_cast<System::Byte>(192)));
this->treeView1->CheckBoxes = true;
this->treeView1->ContextMenuStrip = this->rightClickMenu;
this->treeView1->LineColor = System::Drawing::Color::White;
this->treeView1->Location = System::Drawing::Point(0, 0);
this->treeView1->Name = L"treeView1";
treeNode1->Checked = true;
treeNode1->Name = L"Nodev0";
treeNode1->NodeFont = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(0)));
treeNode1->Text = L"Tree Node to be Tested";
this->treeView1->Nodes->AddRange(gcnew cli::array< System::Windows::Forms::TreeNode^  >(1) {treeNode1});
this->treeView1->RightToLeftLayout = true;
this->treeView1->Size = System::Drawing::Size(347, 295);
this->treeView1->TabIndex = 33;
this->treeView1->AfterSelect += gcnew System::Windows::Forms::TreeViewEventHandler(this, &Program::treeView1_AfterSelect);

// a method later in the program.

private: System::Void treeView1_AfterSelect(System::Object^  sender, System::Windows::Forms::TreeViewEventArgs^  e) {
	 if (treeNode1->Checked==true){ //<-- line 2124 that gives me the error
		 richTextBox1->Text=L"IT WORKED!!!"; // The rich text box works fine with other functions, so this isn't the problem.
	 }
 }



错误1错误C2065:``treeNode1'':未声明的标识符行2124
错误2错误C2227:``->已检查''的左侧必须指向类/结构/联合/泛型类型2124行

我究竟做错了什么?我的if语句写得不好吗,还是我遗漏了一些东西?使用按钮,单选按钮或组合框之类的其他所有东西,我所做的都会奏效.为什么这次没有呢?



Error1 error C2065: ''treeNode1'' : undeclared identifier Line 2124
Error2 error C2227: left of ''->Checked'' must point to class/struct/union/generic type Line 2124

What am I doing wrong? Did I write the if statement poorly, or am I missing something? With everything else, like a button, radio button or combobox, what I did would''ve worked. Why didn''t it this time?

推荐答案

就是这样:您没有声明treeNode1.您可能会说它是在显示的代码中声明的,但没有.您只是没有显示此代码的上下文(查看任何人都应该看到的代码是某些方法的一部分,但您没有显示它),所以这是其他方法的不同上下文,不是treeView1_AfterSelect.在这种方法中,诸如treeNode1之类的东西不存在.问题解决了.

还有什么?这是胡言乱语:
This is what it is: you did not declare treeNode1. You might say it''s declared it in the code shown, but no. You just did not show a context of this code (looking at the code anyone should see it''s the part of some method, but you don''t show it), so this is a different context, of some other method, not treeView1_AfterSelect. In this method, such thing as treeNode1 does not exist. Problem solved.

What else? This is gibberish:
if (treeNode1->Checked = true) { //...


如果要比较是否已选中,则应使用if (treeNode1->Checked),并且"="是赋值,而不是比较运算符("==").

我有什么建议?也许,停止编写一个庞大的程序";在可以精简一些基础知识的地方写些小东西-您确实需要它.如果您度过了愉快的时光,您将很快赶上……:-)

祝你好运,

—SA


If you want to compare if it''s checked or not, you should use if (treeNode1->Checked), and "=" is assignment, not comparison operator ("==").

What would I advice? Perhaps, stop writing "a huge program"; write something small where you can lean some basics — you really need it. If you spend your time well, you will catch up pretty fast… :-)

Good luck, best wishes,

—SA


这篇关于有关TreeViews和TreeNodes的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆