• 締切済み

VB.net TreeViewコントロール

VB.net(2010 Express)でTreeViewコントロールを使用し、子ノードと子ノードの間にドラックアンドドロップする際に線を表示したいのですが、どのようにすれば良いのか分かりません。 単純に子ノードのドラッグアンドドロップはできるのですが、子ノード同士の間に線を表示する方法が分からずにハマっています!! 添付しました画像の様な線を表示できる方法など分かる方がいましたら、お力をお貸しください。 宜しくお願い致します。

みんなの回答

  • hirotn
  • ベストアンサー率59% (147/246)
回答No.3

  private void treeView1_MouseMove(object sender, MouseEventArgs e)   {     TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);     if (e.Button == MouseButtons.Left && allowLine == false)     {       allowLine = true;       if (this.VisibleDraggedNodeLabel == true)       {         motionLabel.Visible = true;       }            }     if (allowLine == true)     {       //motionLabel.Location = Cursor.Position;       Point client = treeView1.PointToClient(Cursor.Position);              if (clickedNode != null && this.VisibleDraggedNodeLabel==true)       {         motionLabel.Location = new Point(treeView1.Bounds.X+clickedNode.Bounds.X+(clickedNode.Bounds.Width/2),           treeView1.Bounds.Y+clickedNode.Bounds.Y+(clickedNode.Bounds.Height/2));       }       treeView1.Invalidate();     }   } } TreeViewクラス、TreeNodeクラス、TreeNode.DrawModeプロパティのヘルプを参考になさってください。

  • hirotn
  • ベストアンサー率59% (147/246)
回答No.2

(承前)   private void treeView1_MouseDown(object sender, MouseEventArgs e)   {     treeView1.Capture = true;     TreeNode clickedNode = treeView1.GetNodeAt(e.X, e.Y);     if (clickedNode != null)     {       if (NodeBounds(clickedNode).Contains(e.X, e.Y))       {         treeView1.SelectedNode = clickedNode;         if (this.VisibleDraggedNodeLabel == true) motionLabel.Text = clickedNode.Text;       }            }   }   private void treeView1_DrawNode(object sender, DrawTreeNodeEventArgs e)   {     drawcount++;     this.Text = drawcount.ToString() + " " + e.State;     // Draw the background and node text for a selected node.     if ((e.State & TreeNodeStates.Selected) != 0)     {       e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));       Font nodeFont = e.Node.NodeFont;       if (nodeFont == null) nodeFont = ((TreeView)sender).Font;       // Draw the node text.       e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,       e.Node.Bounds.X + 2, e.Node.Bounds.Y);       using (Pen focusPen = new Pen(Color.Black))       {         focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;         Rectangle focusBounds = NodeBounds(e.Node);         focusBounds.Size = new Size(focusBounds.Width - 1,         focusBounds.Height - 1);         e.Graphics.DrawRectangle(focusPen, focusBounds);       }     }     else     {// Use the default background and node text.       e.DrawDefault = true;     }     if (allowLine == true)     {       Graphics g = e.Graphics;       Point client = treeView1.PointToClient(Cursor.Position);       TreeNode clickedNode = treeView1.GetNodeAt(client);       if (clickedNode != null)       {         int x = treeView1.Width;         int y = clickedNode.Bounds.Y;         //g.DrawArc(pen, -4, y - 4, 8, 8, 0, 360);         g.FillPolygon(Brushes.Black, new Point[]{new Point(0+5,y-4),                                  new Point(0+5,y+4),                                  new Point(4+5,y)});         g.DrawLine(pen, 3 + 5, y, x - 3 - 5, y);       //y = clickedNode.bound.y         g.FillPolygon(Brushes.Black, new Point[]{new Point(x-5,y-4),                                  new Point(x-5,y+4),                                    new Point(x-4-5,y)});       }     }     // If a node tag is present, draw its string representation     // to the right of the label text.     if (e.Node.Tag != null)     {       e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont,         Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top);     }     // If the node has focus, draw the focus rectangle large, making     // it large enough to include the text of the node tag, if present.     if (((e.State & TreeNodeStates.Focused) != 0) ||       (e.State & TreeNodeStates.Selected) != 0)     {       using (Pen focusPen = new Pen(Color.Black))       {         focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;         Rectangle focusBounds = NodeBounds(e.Node);         focusBounds.Size = new Size(focusBounds.Width - 1,         focusBounds.Height - 1);         e.Graphics.DrawRectangle(focusPen, focusBounds);       }     }   }   private Rectangle NodeBounds(TreeNode node)   {     // Set the return value to the normal node bounds.     Rectangle bounds = node.Bounds;     if (node.Tag != null)     {       // Retrieve a Graphics object from the TreeView handle       // and use it to calculate the display width of the tag.       Graphics g = treeView1.CreateGraphics();       int tagWidth = (int)g.MeasureString         (node.Tag.ToString(), tagFont).Width + 6;       // Adjust the node bounds using the calculated value.       bounds.Offset(tagWidth/2, 0);       bounds = Rectangle.Inflate(bounds, tagWidth/2, 0);       g.Dispose();      }     return bounds;   }

  • hirotn
  • ベストアンサー率59% (147/246)
回答No.1

VB.netの環境がなく、C#で申し訳ありませんが…。 treeView1と、motionLabel(Label)を配置します。 treeView.DrawModeは、OwnerDrawTextとします。 VisibleDraggedNodeLabelがTrueの場合はドラッグ時にドラッグをしているノード名がmotionLabelに表示されます。不要ならばfalse。 TreeViewの描画はDrawNode内部で。コンポーネントの操作や再描画契機(Invalidate)の発行はMouseXXXイベントで行なっています。 この事例では、ノード間のドラッグアンドドロップに対応しており、アイコンなどを外側からドラッグしてくる状況を考える場合にはイベントなどを考慮する必要があります。 public partial class Form1 : Form {   int drawcount = 0;   private bool allowLine = false;   Pen pen;   //Graphics g = null;   Font tagFont = new Font("Helvetica", 8, FontStyle.Bold);   private bool _visibleMotionLabel = true;   public bool VisibleDraggedNodeLabel{     get     {       return _visibleMotionLabel;     }     set     {       _visibleMotionLabel = value;     }   }   private void Form1_Load(object sender, EventArgs e)   {     allowLine = false;     motionLabel.Location = new Point(-100, 10);     motionLabel.BackColor = Color.Transparent;     this.VisibleDraggedNodeLabel = true;     pen = new Pen(Color.Black, 2);   }   private void treeView1_MouseLeave(object sender, EventArgs e)   {     treeView1.Capture = false;   }   private void treeView1_MouseUp(object sender, MouseEventArgs e)   {     treeView1.Capture = false;     if (allowLine == true)     {       motionLabel.Location = new Point(-100, 10);       motionLabel.Visible = false;       allowLine = false;     }     treeView1.Invalidate();   }

関連するQ&A

  • TreeViewコントロールについて

    VB2005環境です。 TreeViewコントロールを作成し、ノードごとにクリックイベントを 追加して、タブコントロールを呼び出すようにしたいと考えています。 (親ノードをクリックしたら、親ノード用のタブコントロールが 画面に表示され、さらに子ノードAをクリックすると、ノードA用の タブコントロールが表示される、ノードBをクリックすると…という具合に)。 NodeMouseClickイベントを使えば良いのかとは思うのですが、クリックイベントが 子ノード各々ごとに取得できず困っています。 やり方を教えていただけないでしょうか? 更に、そこから各々専用のタブコントロールを呼び出す方法も 教えていただけるとありがたいです。 よろしくお願いします。

  • VB2008.netのTreeViewについて質問です。

    VB2008.netのTreeViewについて質問です。 質問させて頂きます。 VB2008で作業をしています。 TreeViewで子ノードを選択します。 この時、選択されている子ノードの、親ノードを取得したいのですが 方法がわかりません。 わかる方教えていただけないでしょうか。

  • TreeViewでToolTipTextを表示しない方法

     VB6.0でTreeViewコントロールを使ってプログラミングしています。 このとき、TreeViewのノード上にカーソルを持っていくと、ToolTipTextが 自動的に表示されます。このToolTipTextの表示を禁止する方法を教えて ください。

  • VB2005 TreeViewの任意ノード選択

    お世話になっております。 VB2005でWinアプリを開発中です。 ルートノードは1つで、 そこから子、孫、ひ孫・・・と階段状になるTreeViewを作っています。 親   L子      L孫         Lひ孫             ・・・・ このようなツリーに対し、プログラムで任意のノードを選択状態にしたいのですが、 ノードの名称などで一発で指定する方法はありますでしょうか?? 上の例で、「孫」を選択状態にしたい場合、 TreeView1.SelectedNode = TreeView1.Nodes(0).Nodes(0).Nodes(0) と書けば、できることはできるのですが。 他にもっとよい方法があるはずと思うのですが、見つけられませんでした。 VB6.0なら、下記に方法があったのですが・・・ http://oshiete1.goo.ne.jp/qa732490.html よろしくお願いします。

  • TreeViewで複数ノードの選択は可能ですか?

    VisualBasic2005でwindowsアプリの開発をしています。 今回希望として、複数のノードを一度にドラッグするというのがあるのですが、 TreeViewで複数ノードの選択は可能なのでしょうか? そして、もし可能な場合、それらを一気にドラッグということは可能でしょうか? ドロップ先では、ドラッグ中のノードのtextが取れればよいです。 よろしくお願いします。

  • TreeView の初期表示について

    TreeView に『A』『B』『C』のノードがあるとします。 初期表示を『B』ノードを選択・展開した状態(子ノードあり)にしたいのですが、 方法がわかりません。 何かよい方法はないでしょうか?よろしくお願いします。 説明不足な点はすぐに補います。

  • TreeViewについて

    VisualBasic6.0で自分用のアプリをつくっています。TreeViewコントロールを使っているのですが各ノードにコマンドラインやテキストのデータを付け加えたいのですが方法がわかりません。ご存知の方よろしくお願いします。

  • VBのコントロールを追加するとはどういうこと?

    VBのコントロールを追加するとはどういうことですか? 印刷ができるようにするには、PrindDocumentコントロールが必要です。 コントロールをドラッグ&ドロップするワケは?

  • C# treeviewについて

    VS2017で独学でC#を勉強しています。 treeviewでエクスプローラのようにドライブを表示する方法を教えてください。 それとtreeviewで検索しているとノードという単語がでてきます。 これは何のことでしょうか? treeviewにこだわっているわけではなく、エクスプローラのように表示する方法でしたらなんでもいいです。 よろしくお願いします。

  • TreeViewを反転表示したままTextBoxにカーソル

    VB2005 Expressで開発をしています。 TreeViewでノードを選択して、その値をTextBoxに表示しようと 思っています。 そこで、選択したノードを反転表示したままTextBoxにカーソル をセットすることは可能なのでしょうか。 フォーカスを当てた上体で反転されるので無理な気はするのですが、 ご存知の方がいらっしゃいましたら教えて下さい。 よろしくお願いします。

専門家に質問してみよう