Solved

How to display Select Computer stantard dialog

Posted on 2004-09-14
8
742 Views
Last Modified: 2012-08-14
I'd like to display standard Select Computer dialog, like the one in Computer Management Console -> Right click -> Select "Connect to Another Computer" -> Browse
How can it be done from C#?
Any other standard network browse dialog also will be OK.

Any help will be appreciated,
Jigit
0
Comment
Question by:Jigit
8 Comments
 
LVL 19

Accepted Solution

by:
Desp earned 500 total points
ID: 12054147
There is no such dialog but you can make your own... eg i am filling all the computers in a listbox:

DirectoryEntry root  = new DirectoryEntry("WinNT:");        
               
foreach (DirectoryEntry dDom in root.Children)                  
      foreach (DirectoryEntry dPC in dDom.Children)                  
            if (dPC.SchemaClassName == "Computer" )
                  listBox1.Items.Add (dDom.Name + "/" + dPC.Name);            

0
 
LVL 5

Expert Comment

by:tomasX2
ID: 12054469
     FolderBrowserDialog di = new FolderBrowserDialog();
      di.ShowDialog();
0
 
LVL 4

Author Comment

by:Jigit
ID: 12054983
tomasX2, thank you. Your suggestion allows to browse for folders, while I need only computers.

Desp, your suggestion is closer to what I need. The only problem I encounter with it is performance. The whole enterprise network is being scanned for all the object.

Is there possibility to limit it only to computers?
Or display only domain controllers and expand their networks on mouse click? (Maybe filling them in a tree view)
Does it support WinNT Networks and Active Directory? What has "LDAP" to do with it?

Thanks,
Jigit
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:Desp
ID: 12055413
you can put a line
Application.Doevents();
to improve the performance (compare the performance by commenting the line and run the code again)

for active directory , LDAP thingy check this:
http://www.c-sharpcorner.com/Code/2002/Aug/ListAllComps.asp
0
 
LVL 4

Author Comment

by:Jigit
ID: 12055749
I'm trying to build my own ComputerBrowserDialog using your suggestions. I'm filling Domain Controllers into TreeView on form load. I'd like to expand a network when user double clicks on it. How can I do it? My code is published below, but it does not react on clicking on a node. Any ideas?

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.DirectoryServices;

namespace MMCTest
{
      /// <summary>
      /// Summary description for BrowseServerForm.
      /// </summary>
      public class BrowseServerForm : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button okButton;
            private System.Windows.Forms.Button cancelButton;
            private System.Windows.Forms.ListBox listBox1;
            public string ServerName = null;
            private System.Windows.Forms.TreeView computersTreeView;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public BrowseServerForm()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if(components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.okButton = new System.Windows.Forms.Button();
                  this.cancelButton = new System.Windows.Forms.Button();
                  this.listBox1 = new System.Windows.Forms.ListBox();
                  this.computersTreeView = new System.Windows.Forms.TreeView();
                  this.SuspendLayout();
                  //
                  // okButton
                  //
                  this.okButton.Location = new System.Drawing.Point(392, 352);
                  this.okButton.Name = "okButton";
                  this.okButton.TabIndex = 1;
                  this.okButton.Text = "OK";
                  this.okButton.Click += new System.EventHandler(this.okButton_Click);
                  //
                  // cancelButton
                  //
                  this.cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
                  this.cancelButton.Location = new System.Drawing.Point(160, 360);
                  this.cancelButton.Name = "cancelButton";
                  this.cancelButton.TabIndex = 2;
                  this.cancelButton.Text = "Cancel";
                  this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
                  //
                  // listBox1
                  //
                  this.listBox1.Location = new System.Drawing.Point(32, 40);
                  this.listBox1.Name = "listBox1";
                  this.listBox1.Size = new System.Drawing.Size(344, 225);
                  this.listBox1.TabIndex = 3;
                  //
                  // computersTreeView
                  //
                  this.computersTreeView.ImageIndex = -1;
                  this.computersTreeView.Location = new System.Drawing.Point(392, 48);
                  this.computersTreeView.Name = "computersTreeView";
                  this.computersTreeView.SelectedImageIndex = -1;
                  this.computersTreeView.Size = new System.Drawing.Size(360, 272);
                  this.computersTreeView.TabIndex = 4;
                  //
                  // BrowseServerForm
                  //
                  this.AcceptButton = this.okButton;
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.CancelButton = this.cancelButton;
                  this.ClientSize = new System.Drawing.Size(786, 418);
                  this.Controls.Add(this.computersTreeView);
                  this.Controls.Add(this.listBox1);
                  this.Controls.Add(this.cancelButton);
                  this.Controls.Add(this.okButton);
                  this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
                  this.Name = "BrowseServerForm";
                  this.Text = "Browse Server";
                  this.Load += new System.EventHandler(this.BrowseServerForm_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            private void BrowseServerForm_Load(object sender, System.EventArgs e)
            {

                  computersTreeView.BeforeExpand += new TreeViewCancelEventHandler(computersTreeView_BeforeExpand);
                  
                  DirectoryEntry root  = new DirectoryEntry("WinNT:");    
                  foreach (DirectoryEntry dDom in root.Children)
                  {
                        TreeNode node = new TreeNode();
                        node.Text = dDom.Name;
                        computersTreeView.Nodes.Add ( node );    
                  }
            }

            private void okButton_Click(object sender, System.EventArgs e)
            {
                  this.DialogResult = DialogResult.OK;
                  if ( listBox1.SelectedItem != null )
                  {
                        ServerName = listBox1.SelectedItem.ToString();
                  }
            }

            private void cancelButton_Click(object sender, System.EventArgs e)
            {
                  this.DialogResult = DialogResult.Cancel;
            }

            private void computersTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
            {
                  TreeNode node = sender as TreeNode;
                  if ( node == null ) return;

                  DirectoryEntry root  = new DirectoryEntry("WinNT:");        
                  foreach (DirectoryEntry dDom in root.Children)
                  {
                        if ( dDom.Name != node.Text ) continue;

                        foreach (DirectoryEntry dPC in dDom.Children)              
                              if (dPC.SchemaClassName == "Computer" )
                                    node.Nodes.Add ( dPC.Name);      
                  }
            }
      }
}
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12057875
Change:

  computersTreeView.DoubleClick += new TreeViewCancelEventHandler(computersTreeView_BeforeExpand);

to:

  computersTreeView.DoubleClick += new EventHandler(computersTreeView_DoubleClick);


Change:

  private void computersTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)

to:

  private void computersTreeView_DoubleClick(object sender, EventArgs e)


And finally, change:

  TreeNode node = sender as TreeNode;

to:

  TreeNode node = ((TreeView)sender).SelectedNode;


You may also find this of interest: http://www.codeproject.com/csharp/csenumnetworkresources.asp
0
 
LVL 4

Author Comment

by:Jigit
ID: 12057966
eternal_21, thanks for your comment. Let me argue with you: node expansion may be done not only by double-clicking, but also by presssing "+" on the keyboard or maybe some other event. I've already implemented it using BeforeExpand. So all the points go to Desp, who was the first to suggest this solution.

Thanks to all of you , guys!!!
0
 
LVL 19

Expert Comment

by:Desp
ID: 12061496
Thanks
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question