Foreach(reader.readline()中的line.contains(“id”)

时间:2014-08-21 09:08:32

标签: c# treeview streamreader

最近我开始基于.nui文件创建一个用户界面管理器,

他们看起来像这样:

begin genwnd
id = addinformation_creature;
spr = ui_frame.spr;
rect = 0,0,220,319;
style = KSTYLE_NOCLOSE | KSTYLE_NOMINIMIZE | KSTYLE_NORESIZE | KSTYLE_NOTITLE | KSTYLE_NOSTATUSBAR | KSTYLE_NOTOPFRAME | KSTYLE_MOVE_BY_CUSTOM | KSTYLE_VERTICAL_REPEAT ;
end

begin static
id = addcreature_subtitle01;
spr = ui_frame.spr;
ani = background_infocommon_subtitle2;
pos = 17,0;
flag = KFLAG_NO_GET_MESSAGE ;
end

现在正在做我的读者,我发生了一个问题(由于静态)我到目前为止在树视图中动态地编码了所有内容,并且我已经成功地将它们添加到正确的节点下。

然而;如果不是更多,则静止发生约60至70次,其下方的id,spr,ani,pos,flag为' childs'。

我无法在每个动态创建的ID(头节点)下添加它们,我尝试了各种方法。

我使用的代码简单明了,它需要一些调整我似乎无法弄明白。

#region Begin Static
case "begin static":
/// <summary= begin static> here we read the window <see static>
/// <param= end> read till end of the file where <end> is the variable to stop at.
/// <param= id> read <id> for the name in <treeview>.
/// <param= pos> read <rect> for the sizes in <treeview> (X,Y).
/// <param= ani> read <ani> for the animation script in <treeview>.
#region reading...
while ((line = reader.ReadLine()) != "end")
{
int a = 0;
if (line.Contains("id = "))
{
string str = line;
int startIndex = str.IndexOf("id = ") + "id = ".Length;
int endIndex = str.IndexOf(";");
Form1.Instance.treeView1.Nodes[0].Nodes.Add("Picture: " + str.Substring(startIndex, endIndex - startIndex).ToString());
a = a + 1;
}

if (line.Contains("ani = "))
{
string str1 = line;
int startIndex1 = str1.IndexOf("ani = ") + "ani = ".Length;
int endIndex1 = str1.IndexOf(";");
Form1.Instance.treeView1.Nodes[0].Nodes[1].Nodes.Add("Animation: " + str1.Substring(startIndex1, endIndex1 - startIndex1).ToString());

}


if (line.Contains("pos = "))
{
string str2 = line;
int startIndex2 = str2.IndexOf("pos = ") + "pos = ".Length;
int endIndex2 = str2.IndexOf(";");
Form1.Instance.treeView1.Nodes[0].Nodes[1].Nodes.Add("Position");
//Form1.Instance.treeView1.Nodes[i].Nodes.Add("Position: " + str.Substring(startIndex, endIndex - startIndex).ToString());

}


if (line.Contains("flag = "))
{
string str3 = line;
int startIndex3 = str3.IndexOf("flag = ") + "flag = ".Length;
int endIndex3 = str3.IndexOf(";");
Form1.Instance.treeView1.Nodes[0].Nodes[1].Nodes.Add("Flags: " + str3.Substring(startIndex3, endIndex3 - startIndex3).ToString());
}
 }
 break;

如你所见,它完美地放置了节点,但是所有的孩子都在节点1内但是孩子们必须重复,例如(spr,ani,pos,flag as childs)节点

enter image description here

1 个答案:

答案 0 :(得分:0)

你的意思并不是很清楚,但我怀疑你只想保留对“当前节点”的引用 - 当你获得一个新的ID时,你会改变它。所以:

TreeNode node = null;

while ((line = reader.ReadLine()) != "end")
{
    if (line.Contains("id"))
    {
         ...
         // Note how you don't need to call ToString() on the result of Substring
         node = Form1.Instance.treeView1.Noodes[0].Nodes.Add("Picture: " + 
             str.Substring(startIndex, endIndex - startIndex));
    }
    else if (line.Contains("ani = "))
    {
        ...
        node.Add(...);
    }
}

如果你在 id行之前得到一行,那么这将失败,但是......但可能是id始终是一个部分中的第一行。