我正在开发一个帮助我构建长而重复的HTML表的实用程序。我有从XML文件导入的数据,一个显示名称的listBox,将文本插入包含XML中某些数据的richTextBox的按钮,但我必须手动输入每个名称的信息。
我有一个treeView,列出了委员会(父级)和子委员会(1级孩子)。我可以检查相应的框,单击一个按钮,然后创建所有相应的HTML。但是,每个委员会(和子委员会)都有一名主席和一名排名成员。而不是在每个父母和孩子下面添加这两个额外的节点 - 我有71个父母和孩子。我宁愿不再增加142个节点 - 我希望有一种方法可以实现四次点击"复选框...?第一次点击=选中标记;第二=绿色;第3 =红色; 4号=清除。或类似的。这样我就可以检查"作为会员,"仔细检查"对于椅子,"三重检查"排名,第四个刚刚开始。
我也接受另一种方法的建议。这是我需要努力保存我手工输入2-3K行HTML的最后一点,所以我不在乎如何完成它。感谢。
答案 0 :(得分:0)
好的,抱歉已经有一段时间了。你知道,生活等等。无论如何,我最终得到了自己的TreeNode和TreeView类。这是我第一次使用WinForms进行冒险,所以很多这个看起来很挑剔。结束比我想象的要简单得多。如果有人想要使用它,那就把自己搞得一团糟。
Check * .bmp图像只是一个我选中的复选框。我可能会为此创建自己的四个图像并清除populateStateImageList()方法。此外,我认为这将适用于您需要的多个状态,最多可达StateImageList的14个图像限制。
using System.Drawing;
using System.Windows.Forms;
namespace jkHTMLBuilder
{
public class QuadCheckTreeNode : TreeNode
{
public enum CheckState : int { UnChecked, Checked, Chair, Rank } // The four possible states
private CheckState _cs = CheckState.UnChecked; // The node's current state
public CheckState checkState
{
get
{
return _cs;
}
set
{
_cs = value;
}
}
public QuadCheckTreeNode(string initString) : base()
{
this.Text = initString;
this.checkState = CheckState.UnChecked;
this.Checked = false;
this.StateImageIndex = 0;
}
public void checkAdvance() // This is called from onAfterCheck to set the next consecutive state
{
switch (checkState)
{
case CheckState.UnChecked:
checkState = CheckState.Checked;
break;
case CheckState.Checked:
checkState = CheckState.Chair;
break;
case CheckState.Chair:
checkState = CheckState.Rank;
break;
case CheckState.Rank:
checkState = CheckState.UnChecked;
break;
}
this.Checked = (this.checkState == CheckState.UnChecked ? false : true);
}
}
class QuadCheckTreeView : TreeView
{
private bool clickStop = false;
private bool shouldAdvance = false;
public QuadCheckTreeView() : base()
{
StateImageList = new ImageList();
}
public void populateStateImageList() // I made this a separate method and call it from my Load_Senators method so the images are
{ // set up when the XML file is loaded. Apparently, loading the files ( Check*.bmmp )
for (int i = 0; i < 2; i++) // from the ctor causes problems...? Whatever. This works.
{
Bitmap bmp = new Bitmap(16, 16);
Graphics chkGraphics = Graphics.FromImage(bmp);
switch (i)
{
case 0:
CheckBoxRenderer.DrawCheckBox(chkGraphics, new Point(0, 1), System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
break;
case 1:
CheckBoxRenderer.DrawCheckBox(chkGraphics, new Point(0, 1), System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal);
break;
}
StateImageList.Images.Add(bmp);
}
Bitmap myBitmap = new Bitmap("..\\..\\CheckBlue.bmp");
StateImageList.Images.Add(myBitmap);
myBitmap = new Bitmap("..\\..\\CheckRed.bmp");
StateImageList.Images.Add(myBitmap);
}
public void ClearNodes(TreeNodeCollection nodes) // This is for when I move on to the next record. Unchecks everything.
{
clickStop = true;
foreach (QuadCheckTreeNode qctn in nodes)
{
qctn.Checked = false;
qctn.checkState = QuadCheckTreeNode.CheckState.UnChecked;
if (qctn.Nodes.Count > 0)
{
foreach (QuadCheckTreeNode cqctn in qctn.Nodes)
{
cqctn.Checked = false;
cqctn.checkState = QuadCheckTreeNode.CheckState.UnChecked;
}
}
}
clickStop = false;
}
protected override void OnCreateControl()
{
base.OnCreateControl();
CheckBoxes = false; // Checkboxes off to use my images
ClearNodes(this.Nodes); // Probably not needed.
}
protected override void OnNodeMouseClick(TreeNodeMouseClickEventArgs e)
{
base.OnNodeMouseClick(e);
TreeViewHitTestInfo tvhtInfo = HitTest(e.X, e.Y); // If you didn't click on it, ignore.
if (tvhtInfo == null || tvhtInfo.Location !=
TreeViewHitTestLocations.StateImage)
{
return;
}
QuadCheckTreeNode qctn = (QuadCheckTreeNode)e.Node; // If you right-clicked, set to UnChecked
if (e.Button == MouseButtons.Right)
{
if (qctn.checkState != QuadCheckTreeNode.CheckState.UnChecked)
{
qctn.checkState = QuadCheckTreeNode.CheckState.UnChecked;
qctn.Checked = false;
shouldAdvance = false;
}
}
else
shouldAdvance = true; // Left click sets this var==true so the node's Advance() method is called
qctn.Checked = qctn.Checked; // This fires the onAfterCheck event
}
protected override void OnAfterCheck(TreeViewEventArgs e)
{
base.OnAfterCheck(e);
if (clickStop) // This keeps the event from running if it's called inappropriately
{
return;
}
clickStop = true;
QuadCheckTreeNode qctn = (QuadCheckTreeNode)e.Node;
if (shouldAdvance)
{
qctn.checkAdvance();
shouldAdvance = false;
qctn.StateImageIndex = (int)qctn.checkState;
}
checkParent(qctn); // Calling this method, if it actually checks a parent node, won't call onAfterCheck because of clickStop
clickStop = false;
}
protected void checkParent(QuadCheckTreeNode qctn)
{
QuadCheckTreeNode pqctn = (QuadCheckTreeNode)qctn.Parent;
if (pqctn == null)
return;
if (pqctn.checkState == QuadCheckTreeNode.CheckState.UnChecked) // This checks a parent if it has a checked child
{
bool chkParent = false;
foreach (QuadCheckTreeNode n in pqctn.Nodes)
{
if (n.checkState != QuadCheckTreeNode.CheckState.UnChecked) // Checks all the children. If even one is checked, the parent gets checked
chkParent = true;
}
if (chkParent)
{
pqctn.Checked = true;
pqctn.checkState = QuadCheckTreeNode.CheckState.Checked;
}
}
}
}
}