2011/04/09

VB.net Convert to JSON

VB.net
  1. Public Class JSONConvert
  2.     Public Shared Function DataTable2NameValueCollection(ByVal dt As DataTable) As NameValueCollection
  3.         Dim columnlist As New List(Of String)()
  4.         For Each column As DataColumn In dt.Columns
  5.             columnlist.Add(column.ColumnName)
  6.         Next
  7.  
  8.         Dim rowlist As New List(Of String)()
  9.         For Each row As DataRow In dt.Rows
  10.             Dim itemlist As New List(Of String)()
  11.             For Each column As DataColumn In dt.Columns
  12.                 If column.DataType.Equals(GetType(Int32)) Or column.DataType.Equals(GetType(Double)) Then
  13.                     itemlist.Add(row(column.ColumnName))
  14.                 Else
  15.                     itemlist.Add(String.Format("""{0}""", row(column.ColumnName)))
  16.                 End If
  17.             Next
  18.             rowlist.Add(String.Format("[{0}]", String.Join(", ", itemlist.ToArray())))
  19.         Next
  20.  
  21.         Dim nv As New NameValueCollection()
  22.         nv.Add("columns", Array2JSONArray(columnlist.ToArray()))
  23.         nv.Add("rows", Array2JSONArray(rowlist.ToArray()))
  24.         Return nv
  25.     End Function
  26.  
  27.     Public Shared Function DataTable2JSONObject(ByVal dt As DataTable) As String
  28.         Dim nv As NameValueCollection = DataTable2NameValueCollection(dt)
  29.         Return NameValueCollection2JSONObjectArray(nv)
  30.     End Function
  31.  
  32.     Public Shared Function NameValueCollection2JSONObjectArray(ByVal nv As System.Collections.Specialized.NameValueCollection) As String
  33.         Dim list As New List(Of String)()
  34.         For Each name As String In nv.AllKeys
  35.             If Regex.IsMatch(nv(name), "^(.*(\[|\]|{|})+.*|\d+)$") Then
  36.                 list.Add(String.Format("""{0}"": {1}", name, nv(name)))
  37.             Else
  38.                 list.Add(String.Format("""{0}"": ""{1}""", name, nv(name)))
  39.             End If
  40.         Next
  41.         Return String.Format("{{{0}}}", String.Join(", ", list.ToArray()))
  42.     End Function
  43.  
  44.     Public Shared Function Array2JSONArray(ByVal arr As Array) As String
  45.         If Regex.IsMatch(String.Join(", ", CType(arr, String())), "^(.*(\[|\]|{|})+.*|\d+)$") Then
  46.             Return ObjectArray2JSONArray(arr)
  47.         Else
  48.             Return StringArray2JSONArray(arr)
  49.         End If
  50.     End Function
  51.  
  52.     Public Shared Function StringArray2JSONArray(ByVal arr() As String) As String
  53.         If arr.Length = 0 Then
  54.             Return ("[]")
  55.         Else
  56.             Return String.Format("[""{0}""]", String.Join(""", """, arr))
  57.         End If
  58.     End Function
  59.  
  60.     Public Shared Function ObjectArray2JSONArray(ByVal arr As Array) As String
  61.         Return String.Format("[{0}]", String.Join(", ", CType(arr, String())))
  62.     End Function
  63. End Class

2011/04/08

prototype.js extend Ajax.Queue

javascript
  1. var GUID = function () {
  2.     function S4() {
  3.         return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  4.     }
  5.     function guid() {
  6.         return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  7.     }
  8.     return guid();
  9. };


javascript
  1. Ajax.Queue = Class.create({
  2.     initialize: function () {
  3.         this.id = GUID();
  4.         this.active = false;
  5.         this.completed = true;
  6.         this.queue = [];
  7.         this.stopped = false;
  8.         Ajax.Queues.push(this);
  9.     },
  10.     add: function (url, options) {
  11.         this.queue.push({ "url": url, "options": options });
  12.         return this.queue.length - 1;
  13.     },
  14.     enqueue: function (url, options) {
  15.         var index = this.add(url, options);
  16.         if (!this.active) this.start();
  17.         return index;
  18.     },
  19.     clear: function () {
  20.         this.queue = [];
  21.     },
  22.     start: function () {
  23.         if (this.queue.length == 0) {
  24.             this.active = false;
  25.             this.completed = true;
  26.         }
  27.         else if (this.stopped) {
  28.             this.stopped = false;
  29.             this.clear();
  30.             this.active = false;
  31.             this.completed = true;
  32.         }
  33.         else {
  34.             this.active = true;
  35.             var params = this.queue.shift();
  36.             params.options = params.options || {};
  37.             var handlers = {
  38.                 "Success": function (transport) {
  39.                     if (typeof console == "object") console.log("success");
  40.                     this.start();
  41.                 },
  42.                 "Failure": function (transport) {
  43.                     if (typeof console == "object") console.log("failure");
  44.                     this.active = false;
  45.                 },
  46.                 "Complete": function (transport) {
  47.                     if (typeof console == "object") console.log("complete");
  48.                     this.active = false;
  49.                 },
  50.                 "Exception": function (transport, e) {
  51.                     if (typeof console == "object") console.log("exception");
  52.                     this.active = false;
  53.                     this.exception = e;
  54.                 }
  55.             };
  56.             var that = this;
  57.             Object.keys(handlers).each(function (event) {
  58.                 var eventName = "on" + event;
  59.                 var originalHandler = params.options[eventName];
  60.                 var handler = handlers[event];
  61.                 params.options[eventName] = (function () {
  62.                     if (Object.isFunction(originalHandler)) {
  63.                         originalHandler.apply(this, arguments);
  64.                     }
  65.                     handler.apply(this, arguments);
  66.                 }).bind(that);
  67.             });
  68.             this.completed = false;
  69.             new Ajax.Request(params.url, params.options);
  70.         }
  71.     },
  72.     stop: function () {
  73.         this.stopped = true;
  74.     }
  75. });
  76. Ajax.Queues = [];
  77. Ajax.Q = new Ajax.Queue();


usage
  1. Ajax.Q.enqueue("url", {
  2.     onSuccess: function (transport) {
  3.         try {
  4.             alert(transport.responseText);
  5.         } catch (e) { if(typeof(console) == "object") console.log(e) }
  6.     },
  7.     onFailure: function (ajax) {
  8.         if(typeof(console) == "object") console.log(e);
  9.     }
  10. });

2010/12/30

VB.net Process ExecutablePath(Process MainModule Filename Failed In Windows 7)

VB.net
  1. Dim query As New System.Management.ManagementObjectSearcher()
  2. query.Query = New System.Management.ObjectQuery("SELECT * FROM Win32_Process WHERE Name LIKE 'chrome%'")
  3. Dim queryCollection As System.Management.ManagementObjectCollection = query.Get()
  4. For Each mo As System.Management.ManagementObject In queryCollection
  5.     Dim ID As Integer = mo("ProcessId")
  6.     Dim Name As String = mo("Name")
  7.     Dim Path As String = mo("ExecutablePath")
  8.     System.Diagnostics.Debug.WriteLine(String.Format("{0},{1},{2}", ID, Name, Path))
  9. Next

2010/04/21

去掉所有Form上TextBox的空白

VB.net
  1. Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.     Dim tbs() As TextBox = FindAllTextBox(Me)
  3.     For Each tb As TextBox In tbs
  4.         AddHandler tb.Leave, AddressOf TrimTextBox
  5.     Next
  6. End Sub
  7.  
  8. Private Sub TrimTextBox(ByVal sender As Object, ByVal e As EventArgs)
  9.     Dim tb As TextBox = sender
  10.     tb.Text = tb.Text.Trim()
  11. End Sub
  12.  
  13. Private Function FindAllTextBox(ByVal ctrl As Control) As TextBox()
  14.     Dim list As New List(Of TextBox)
  15.     For Each c As Control In ctrl.Controls
  16.         If c.GetType().Name = "TextBox" Then
  17.             list.Add(c)
  18.         Else
  19.             list.AddRange(FindAllTextBox(c))
  20.         End If
  21.     Next
  22.     Return list.ToArray()
  23. End Function