WinForms TreeView - 如何手动“突出显示”节点(就像点击它一样)

时间:2009-12-03 09:41:17

标签: c# winforms treeview

我需要知道如何让编程选择的节点在“选中”状态下以图形方式进行,就像用户点击它一样。 SelectedNode仅在内部选择此选项。非常感谢你!

9 个答案:

答案 0 :(得分:34)

未突出显示的原因是树视图没有焦点。这是我的测试表单上的按钮点击事件:

TreeView1.SelectedNode = TreeView1.Nodes(2);
TreeView1.Focus();

正确突出显示节点。如果您删除Focus();调用,则在您单击树视图(树视图中的任何位置,不一定是您要选择的节点)之前,它不会突出显示。

答案 1 :(得分:2)

这适用于.net 3.5: 将treeview组件的 DrawMode 属性设置为: OwnerDrawAll 然后在 DrawNode 事件中编写以下内容:

 if (((e.State & TreeNodeStates.Selected) != 0) && (!MyTreeView.Focused))
     e.Node.ForeColor = Color.Blue;
else
     e.DrawDefault = true;

BeforeSelect 事件中有:

if (MyTreeView.SelectedNode != null)
    MyTreeView.SelectedNode.ForeColor = Color.Black;
e.Node.ForeColor = Color.Blue;

答案 2 :(得分:2)

TreeView1.SelectedNode.BackColor = SystemColors.HighlightText; // This will work

以上解决方案仅将重点放在其上,但不会更改其高亮视图。

答案 3 :(得分:1)

我不知道它是否对您有所帮助,但检查页面的Taborder并确保树视图控件的Tab键顺序为0

答案 4 :(得分:0)

底层Win32控件支持此功能(认为它是TVIS_DROPHILITED),但我看不到通过TreeView控件公开的相同功能。

正如theraneman所说,你可以使用TreeNode.ForeColorBackColor属性伪造它......

答案 5 :(得分:0)

我遇到了类似的问题,并希望在表单加载上选择(突出显示) TreeView节点。 也许有人也有同样的问题。

我首先尝试了Pondidum的解决方案。没有成功。 但后来我在另一个线程中找到了解决方案:只需将TabIndex的{​​{1}}设置为0即可。 在这种情况下您不需要设置焦点。只需选择应使用TreeView选择的节点并设置SelectedNode即可。就是这样。

答案 6 :(得分:0)

以下是我的工作:

void myProcedure()
{
  // Hookup a DrawMode Event Handler
  this.myTV.DrawNode += myTV_DrawNode;
  // Set DrawMode and HideSelection
  this.myTV.DrawMode = TreeViewDrawMode.OwnerDrawText;
  this.myTV.HideSelection = false;

  // Make sure the TreeView has Focus
  this.myTV.Focus();
  // Make sure the TreeView is Selected
  this.myTV.Select();
  // If the TreeView has a Node, I want to select the first Node to demonstrate.
  if (this.myTV.Nodes.Count > 0)
  {
    // Make sure the node is visible
    this.myTV.Nodes[0].EnsureVisible();
    // Make sure the Node is Selected
    this.myTV.SelectedNode = myTV.Nodes[0];
  }
  // Make sure the SelectedNode IS the Node that we programmatically want to select.
  textBox1.Text = this.myTV.SelectedNode.Text;
  // if we display sanityCheck1 string, it actually is the correct node.text
  // Make sure .NET runtime knows the Node is selected
  textBox1.Text += "  is Selected = " + this.myTV.SelectedNode.IsSelected.ToString();
}

跟进: laalto 回答了HowView HighLight TreeView.Node的问题。 DrawNode事件处理程序中的以下代码,来自 samball 的答案,根据其选择状态正确突出显示TreeView.Node。

private void myTV_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
  // first, let .NET draw the Node with its defaults
  e.DrawDefault = true;
  // Now update the highlighting or not
  if (e.State == TreeNodeStates.Selected)
  {
    e.Node.BackColor = SystemColors.Highlight;
    e.Node.ForeColor = SystemColors.HighlightText;
  }
  else
  {
    e.Node.BackColor = ((TreeView)sender).BackColor;
    e.Node.ForeColor = ((TreeView)sender).ForeColor;
  }
}

平台= Windows 10中的C#.NET 4.5,Visual Studio 2015

答案 7 :(得分:0)

    <html>
    <head>
        <title>Leaflet</title>
        <link rel="stylesheet" href="http://unpkg.com/leaflet@1.3.1/dist/leaflet.css" />
        <script src="http://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
    </head>
    <body>
        <div style="width:100%; height:100%" id="map"></div>
        <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
        <script src="BetterWMS.js"></script>
        <script type='text/javascript'>
            var map = new L.Map('map', {center: new L.LatLng(30.176166666666667,79.14880555555557), zoom: 8});
            var osm = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png');
            map.addLayer(osm);
            var Forest_Data = L.tileLayer.wms('http://localhost:8080/geoserver/cite/wms', {
        format: 'image/png',layers: 'cite:Forest_Data',transparent: 'true'
     }).addTo(map);

     </script>
    </body>
    </html>


and "BetterWMS.js" file is -

L.TileLayer.BetterWMS = L.TileLayer.WMS.extend({

  onAdd: function (mapid) {
    // Triggered when the layer is added to a map.
    //   Register a click listener, then do all the upstream WMS things
    L.TileLayer.WMS.prototype.onAdd.call(this, mapid);
    map.on('click', this.getFeatureInfo, this);
  },

  onRemove: function (mapid) {
    // Triggered when the layer is removed from a map.
    //   Unregister a click listener, then do all the upstream WMS things
    L.TileLayer.WMS.prototype.onRemove.call(this, mapid);
    map.off('click', this.getFeatureInfo, this);
  },

  getFeatureInfo: function (evt) {
    // Make an AJAX request to the server and hope for the best
    var url = this.getFeatureInfoUrl(evt.latlng),
        showResults = L.Util.bind(this.showGetFeatureInfo, this);
    $.ajax({
      url: url,
      success: function (data, status, xhr) {
        var err = typeof data === 'string' ? null : data;
        showResults(err, evt.latlng, data);
      },
      error: function (xhr, status, error) {
        showResults(error);  
      }
    });
  },

  getFeatureInfoUrl: function (latlng) {
    // Construct a GetFeatureInfo request URL given a point
    var point = this._map.latLngToContainerPoint(latlng, this._map.getZoom()),
        size = this._map.getSize(),

        params = {
          request: 'GetFeatureInfo',
          service: 'WMS',
          srs: 'EPSG:4326',
          styles: this.wmsParams.styles,
          transparent: this.wmsParams.transparent,
          version: this.wmsParams.version,      
          format: this.wmsParams.format,
          bbox: this._map.getBounds().toBBoxString(),
          height: size.y,
          width: size.x,
          layers: this.wmsParams.layers,
          query_layers: this.wmsParams.layers,
          info_format: 'text/html'
        };

    params[params.version === '1.3.0' ? 'i' : 'x'] = point.x;
    params[params.version === '1.3.0' ? 'j' : 'y'] = point.y;

    return this._url + L.Util.getParamString(params, this._url, true);
  },

  showGetFeatureInfo: function (err, latlng, content) {
    if (err) { console.log(err); return; } // do nothing if there's an error

    // Otherwise show the content in a popup, or something.
    L.popup({ maxWidth: 800})
      .setLatLng(latlng)
      .setContent(content)
      .openOn(this._map);
  }
});

L.tileLayer.betterWms = function (url, options) {
  return new L.TileLayer.BetterWMS(url, options);  
};

这对我有效(.net 4.7)

答案 8 :(得分:-1)

不确定,但是你不能改变那个节点的背景颜色吗?