Simple recursion to get all directories inside the specifed directory and then bind it to a TreeView

//Main method inside a Win Form
private static void Main()
{
TreeView tv=new TreeView();
DirectoryInfo dirInfo = new DirectoryInfo(“d:\\anyDir”);
TreeNode tn=new TreeNode(dirInfo.Name);
ProduceListing(dirInfo, ” “,tn);
tv.Nodes.Add(tn);
tv.Location=new Point(5,5);
tv.Size=new Size(this.Width,this.Height);
tv.Visible=true;
this.Controls.Add(tv);
}
private void ProduceListing(DirectoryInfo dirInfo,string Spacer,TreeNode TN)
{
foreach(DirectoryInfo subDir in dirInfo.GetDirectories())
{
TreeNode tn=new TreeNode(subDir.Name);
TN.Nodes.Add(tn);
Console.WriteLine(Spacer + Spacer + “{0}”, subDir.Name);
if( subDir.GetDirectories().Length > 0 )
{
ProduceListing(subDir, Spacer + ” “,tn);
}
}
}

-Bugs!

Recently there were many posts asking about "creating a immovable form in .NET". I guess there are three approaches:

1. Make the property FormBorder=None and repaint the minimize, maximize and close buttons with custom bitmaps and set the event handlers respectively.

2. Keep the FormBorder as it is and try to save the initial position of the form in a Point. Then restore the location to the initial point when ever the Form's LocationChanged event is hit.

System.Drawing.Point initialLocation; 
private void Form1_Load(object sender, System.EventArgs e)
{
initialLocation=this.Location;
}

private void Form1_LocationChanged(object sender,System.EventArgs e) 
{
Me.Location = initLocation;
}

This causes flickering of the form though the purpose is served.

3. Next the last and complex method is overridding the WinProc to disable the [Move] menuitem from the system menu.


[DllImport("user32.dll")]
private static extern Int32 EnableMenuItem ( System.IntPtr hMenu , Int32 uIDEnableItem, Int32 uEnable);
private const Int32 HTCAPTION = 0x00000002;
private const Int32 MF_BYCOMMAND =0x00000000;
private const Int32 MF_ENABLED =0x00000000;
private const Int32 MF_GRAYED =0x00000001;
private const Int32 MF_DISABLED =0x00000002;
private const Int32 SC_MOVE = 0xF010;
private const Int32 WM_NCLBUTTONDOWN = 0xA1;
private const Int32 WM_SYSCOMMAND = 0x112;
private const Int32 WM_INITMENUPOPUP = 0x117;

protected override void WndProc(ref System.Windows.Forms.Message m )
{
if( m.Msg == WM_INITMENUPOPUP )
{
//handles popup of system menu

if ((m.LParam.ToInt32() / 65536) != 0 ) // 'divide by 65536 to get hiword

{
Int32 AbleFlags = MF_ENABLED;
if (!Moveable)
{
AbleFlags = MF_DISABLED | MF_GRAYED; // disable the move

}
EnableMenuItem(m.WParam, SC_MOVE, MF_BYCOMMAND | AbleFlags);
}
}if(!Moveable)
{
if(m.Msg==WM_NCLBUTTONDOWN) //cancels the drag this is IMP

{if(m.WParam.ToInt32()==HTCAPTION) return;
}
if (m.Msg==WM_SYSCOMMAND) // Cancels any clicks on move menu

{
if ((m.WParam.ToInt32() & 0xFFF0) == SC_MOVE) return;
}
}
base.WndProc(ref m);
}

This is how you can disable the form being moved.
Enjoy

-Bugs!