扩展C#treenode以在其中具有文本框和标签. [英] Extending C# treenode to have a textbox and label in it.

查看:240
本文介绍了扩展C#treenode以在其中具有文本框和标签.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试扩展treeview控件和defalt treenode,使其包含标签和组合框.我只尝试了一个标签.我扩展了treenode并获得了控件.我使用新的treeview添加treenodeEx的节点.

我成功地在标签中设置了数据,但未显示标签.我尝试捕获drawode,onnodedraw和onpaint事件,但未成功.我针对这些事件的代码从未执行过.请帮我.这是代码


Hi,

I am trying to extend the treeview control and the defalt treenode so that it contains a label and a combo box. I experimented with only a label first. I extended the treenode and got the controls in it. I used the new treeview to add nodes of treenodeEx.

I was successfull in geting setting the data in label but the label was not being displayed. I tried catching the drawnode, onnodedraw and onpaint events but was unsuccessful. My code for these events is never executed. Please help me with that. here is the code


public partial class TreeViewEX : System.Windows.Forms.TreeView
    {
    
        public TreeViewEX()
        {
            InitializeComponent();
        }
        protected override void  OnDrawNode(DrawTreeNodeEventArgs e)
        {
 	         base.OnDrawNode(e);
        }
        
    }
    public class TreeNode : System.Windows.Forms.TreeNode
    {
        private Label m_lblAdditionalInfo;
        public TreeNode()
        {
            m_lblAdditionalInfo = new Label();
            m_lblAdditionalInfo.Visible = true;
        }
        public TreeNode(string text, string addnInfo)
        {
            m_lblAdditionalInfo = new Label();
            m_lblAdditionalInfo.Visible = true;
            this.Text = text;
            this.LblText = addnInfo;
        }
        public void EnableLabel(System.Drawing.Point p)
        {
            this.m_lblAdditionalInfo.Visible = true;
            this.m_lblAdditionalInfo.Left = p.X;
            this.m_lblAdditionalInfo.Top = p.Y;
        }

        public string  LblText
        {
            get { return m_lblAdditionalInfo.Text; }
            set { m_lblAdditionalInfo.Text = value; }
        }
	
    }



有人可以告诉我如何使该标签显示在节点文本的旁边.



can someone tell me how can I make this label be displayed right next to the node''s text.

推荐答案

如果有内存,则需要设置DrawMode属性将TreeView设置为OwnerDrawAll或OwnerDrawText(我想.还是很相似),具体取决于您需要对绘制过程进行多少控制.

如果不这样做,则永远不会调用您的OnDrawNode代码.

希望这可以帮助. :)
If memory serves, you need to set the DrawMode property of the TreeView to either OwnerDrawAll or OwnerDrawText (I think. Very similar to that anyway), depending on how much control you need over the drawing process.

If you don''t do this your OnDrawNode code never gets called.

Hope this helps. :)


我这样做了,但是ondraw被调用了很多次,可以将绘图部分添加到其他函数中,使其仅初始化一次.
还有,
一旦标签可见,我就会遇到其他问题.当我滚动时,我无法获得该节点的标签,而该标签不在控件的客户端屏幕中.我能够看到位于(隐藏)在客户区域之上或之外的节点的标签.

您怎么知道某个特定的节点没有显示,而是向上/向下滚动和隐藏?我试图查看节点的位置,但是它保持不变,并且滚动不会改变它.滚动以隐藏该节点时,Visible属性也不会更改.我如何确保仅显示客户端区域(可见)中的节点的标签?
i did that, but ondraw is called a number of times, can the drawing part be added to some other function which initializes it only once.

Also,
Once i got the label visible, I had some other problem. When i scroll and I can not get the label for the node which is out of client screen of the control to go. I was able to see the labels of the nodes which were either above(hidden) the client area or belo it.

How do you know that a particularr node is not being shown and is scrolled up/down and hidden? I''ve tried to see the position of the node but it remains the same and scrolling does not change it. Visible property is also not changed when you scroll to get that node hidden. How can i make sure that the labels of only the nodes that are in teh client area(visible) are displayed?


因为TreeNode实际上不是Control的后代,所以没有显示表面在其上绘制任何控件.因此,当您使用OwnerDraw功能时,实际上是在TreeView本身的表面上绘制,这也是在重绘或滚动节点时标签不会消失的原因.
为了获得类似您想要的行为的东西,每当其中任何一个节点需要重绘时,您都必须重绘每个节点(包括其背景).您还必须重新定位每个Label/TextBox使其与TreeNode对齐,或者删除它们并为每次重绘重新创建它们.

我怀疑所有这些都会带来糟糕的性能.

我花了一些时间在t''Interwebs周围变冰,希望有人已经做了您想做的事,但什么也没找到.

您可能需要重新考虑您的计划,或者变得非常非常有创意.

祝你好运! :)
Because the TreeNode is not actually a Control descendant, it has no display surface on which to draw any controls. Therefore when you utilize the OwnerDraw functionality, you are actually drawing on the surface of the TreeView itself which is also the reason that your label does not go away when the node is redrawn or scrolled.

To get anything like the behaviour you are looking for you would have to redraw every node (including its background) each time any one of them required a redraw. You would also have to reposition each Label/TextBox to align with its TreeNode, or delete them and recreate them for each redraw.

All of this would, I suspect, give awful performance.

I have spent some time Bingling around t''Interwebs in the hope that someone had already done what you want to do but could find nothing.

You might have to rethink your plans, or get very, very creative.

Good luck! :)


这篇关于扩展C#treenode以在其中具有文本框和标签.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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