110,545
社区成员
发帖
与我相关
我的任务
分享
static class TreeViewNodeIterator
{
private static IEnumerable<TreeNode> GetChild(TreeNode tnparent)
{
foreach (TreeNode tn in tnparent.Nodes)
{
if (tn.Nodes.Count > 0)
{
foreach (TreeNode tnchild in GetChild(tn))
{
yield return tnchild;
}
}
yield return tn;
}
}
public static IEnumerable<TreeNode> GetAllNodes(this TreeView treeview)
{
if (treeview == null) throw new NullReferenceException();
foreach (TreeNode tn in treeview.Nodes)
{
if (tn.Nodes.Count > 0)
{
foreach (TreeNode tnchild in GetChild(tn))
{
yield return tnchild;
}
}
yield return tn;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (TreeNode tn in treeView1.GetAllNodes())
Debug.WriteLine(tn.Text);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Test.foo(3, 5);
}
}
static class Test
{
private static void _foo(params int[] args)
{
for (int i = 0; i < args.GetLength(0); i++)
{
Console.WriteLine(args[i]);
}
}
public static void foo(int x, int y)
{
_foo(x, y);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MyWindow win = new MyWindow();
win.Close();
}
}
class MyWindow : Window
{
protected sealed override void OnBeforeClose(out bool Cancel)
{
Console.WriteLine("Are you sure to close this window? (yes/no)");
string s = Console.ReadLine();
if (s.ToLower() != "yes")
Cancel = true;
else
Cancel = false;
}
protected sealed override void OnClosing()
{
Console.WriteLine("Please wait while the window is closing...");
}
}
class Window
{
protected virtual void OnBeforeClose(out bool Cancel) { Cancel = false; }
protected virtual void OnClosing() { }
protected virtual void OnClosed() { }
public void Close()
{
bool Cancel = false;
OnBeforeClose(out Cancel);
if (Cancel != true)
{
OnClosing();
OnClosed();
Console.WriteLine("Close() Finished!");
}
else
{
Console.WriteLine("Close() Canceled!");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static private Action<string> DebugWriter = s => Debug.WriteLine(s);
static private Action<string> ConsoleWriter = s => Console.WriteLine(s);
static void Main(string[] args)
{
Log log1 = new Log(DebugWriter);
log1.WriteLog("I am writing to outpot window!");
Log log2 = new Log(ConsoleWriter);
log2.WriteLog("I am writing to console!");
}
}
class Log
{
private Action<string> _writer = s => Console.WriteLine(s);
public Log(Action<string> writer)
{
if (writer != null) _writer = writer;
}
public void WriteLog(string s)
{
_writer(s);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Log log1 = Factory.GetLog(Factory.LogType.Debug);
log1.WriteLog("I am writing to outpot window!");
Log log2 = Factory.GetLog(Factory.LogType.Console);
log2.WriteLog("I am writing to console!");
}
}
static class Factory
{
public enum LogType
{
Debug,
Console
};
static private Action<string> DebugWriter = s => Debug.WriteLine(s);
static private Action<string> ConsoleWriter = s => Console.WriteLine(s);
public static Log GetLog(LogType Type)
{
if (Type == LogType.Console) return new Log(ConsoleWriter);
if (Type == LogType.Debug) return new Log(DebugWriter);
return new Log(null);
}
}
class Log
{
private Action<string> _writer = s => Console.WriteLine(s);
public Log(Action<string> writer)
{
if (writer != null) _writer = writer;
}
public void WriteLog(string s)
{
_writer(s);
}
}
}
private static IEnumerable<TreeNode> GetChild(TreeNode tnparent)
{
foreach (TreeNode tn in tnparent.ChildNodes)
{
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode tnchild in GetChild(tn))
{
yield return tnchild;
}
}
yield return tn;
}
}