Đặt làm trang chủ     Ghi nhớ (bookmark)     RSS     Đăng ký     Đăng nhập ?>

PN2design ’s Blog

Internet is my life - Thủ thuật IT

Lưu trữ của danh mục ‘Thủ thuật lập trình’

BindingNavigator và nút Delete, cách confirm

Thứ hai, 20 Tháng mười, 2008

Cách đưa ra câu hỏi xác nhận việc xóa một dòng dữ liệu khi bạn click nút xóa trên BindingNavigator tự phát sinh khi bạn kéo DataSource và form design (phát sinh mã tự động)

  1.     Private Shared Sub Row_Deleting(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)
  2.         If MessageBox.Show("Bạn có chắc mình muốn XÓA?", "Xác nhận việc xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
  3.             e.Row.RejectChanges()
  4.         End If
  5.     End Sub
  1. AddHandler Me.PN2ProductManagerDataSet.Tables("tblPhieuBaoGiaHangHoa").RowDeleted, AddressOf Row_Deleting

Inserting New Records into a Dataset

Thứ sáu, 17 Tháng mười, 2008

In order to add new records into a dataset, a new data row must be created and added to the DataRow collection of a data table. The following procedure details how to insert additional rows into a DataTable object in a dataset. For this example, it is assumed the ExistingTable is in a dataset and has two columns named FirstName and LastName.

To add new records to a typed or untyped dataset

Call a data table’s NewRow method to create a new, blank record. This new record inherits its column structure from the data table’s DataColumnCollection.
' Visual Basic
Dim anyRow as DataRow = ExistingTable.NewRow

// C#
DataRow anyRow = ExistingTable.NewRow();

Update the new row as if it were an existing record.
' Visual Basic
anyRow(0) = "Money"
anyRow(1) = "Phan"
' or
anyRow("FirstName") = "Money"
anyRow("LastName") = "Phan"

// C#
anyRow[0] = “Money”;
anyRow[1] = “Phan”;
// or
anyRow["FirstName"] = “Money”;
anyRow["LastName"] = “Phan”;

Inserting New Records with Typed Datasets
' Visual Basic
ExistingTable.Rows.Add(anyRow)

// C#
ExistingTable.Rows.Add(anyRow);

Typed datasets expose the column names as properties of the DataRow object.

To add new records using typed datasets

* The following example illustrates the same three steps above, except this time the code is modified for use with a typed dataset:

Add the new record to the table by calling the Add method of the DataRowCollection object.

' Visual Basic
Dim anyRow as DataRow = DatasetName.ExistingTable.NewRow
anyRow.FirstName = "Jay"
anyRow.LastName = "Stevens"
ExistingTable.Rows.Add(anyRow)

// C#
DataRow anyRow = DatasetName.ExistingTable.NewRow();
anyRow.FirstName = "Jay";
anyRow.LastName = "Stevens";
ExistingTable.Rows.Add(anyRow);

String.Format Method (String, Object)

Thứ sáu, 17 Tháng mười, 2008

Replaces the format item in a specified String with the text equivalent of the value of a specified Object instance.
The following code example demonstrates the standard formatting specifiers for numbers, dates, and enumerations.
VB.NET

  1. ' This code example demonstrates the String.Format() method.
  2. ' Formatting for this example uses the "en-US" culture.
  3. Class Sample
  4.    Public Enum Color
  5.       Yellow = 1
  6.       Blue = 2
  7.       Green = 3
  8.    End Enum 'Color
  9.  
  10.    Private Shared thisDate As DateTime = DateTime.Now
  11.  
  12.    Public Shared Sub Main()
  13.  
  14.       ' Store the output of the String.Format method in a string.
  15.       Dim s As String = ""
  16.  
  17.       Console.Clear()
  18.  
  19.       ' Format a negative integer or floating-point number in various ways.
  20.       Console.WriteLine("Standard Numeric Format Specifiers")
  21.       s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _
  22.                         "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _
  23.                         "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _
  24.                         "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _
  25.                         "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
  26.                         "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
  27.                         "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _
  28.                         "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _
  29.                         "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _
  30.                         "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
  31.                         - 123, - 123.45F)
  32.       Console.WriteLine(s)
  33.  
  34.       ' Format the current date in various ways.
  35.       Console.WriteLine("Standard DateTime Format Specifiers")
  36.       s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _
  37.                         "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _
  38.                         "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _
  39.                         "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _
  40.                         "(f) Full date/short time: . . {0:f}" & vbCrLf & _
  41.                         "(F) Full date/long time:. . . {0:F}" & vbCrLf & _
  42.                         "(g) General date/short time:. {0:g}" & vbCrLf & _
  43.                         "(G) General date/long time: . {0:G}" & vbCrLf & _
  44.                         "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
  45.                         "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _
  46.                         "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _
  47.                         "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _
  48.                         "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _
  49.                         "(U) Universal full date/time: {0:U}" & vbCrLf & _
  50.                         "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _
  51.                         thisDate)
  52.       Console.WriteLine(s)
  53.  
  54.       ' Format a Color enumeration value in various ways.
  55.       Console.WriteLine("Standard Enumeration Format Specifiers")
  56.       s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _
  57.                         "    (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _
  58.                         "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _
  59.                         "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _
  60.                         "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _
  61.                         Color.Green)
  62.       Console.WriteLine(s)
  63.    End Sub 'Main
  64. End Class 'Sample
  65. '
  66. 'This code example produces the following results:
  67. '
  68. 'Standard Numeric Format Specifiers
  69. '(C) Currency: . . . . . . . . ($123.00)
  70. '(D) Decimal:. . . . . . . . . -123
  71. '(E) Scientific: . . . . . . . -1.234500E+002
  72. '(F) Fixed point:. . . . . . . -123.45
  73. '(G) General:. . . . . . . . . -123
  74. '    (default):. . . . . . . . -123 (default = 'G')
  75. '(N) Number: . . . . . . . . . -123.00
  76. '(P) Percent:. . . . . . . . . -12,345.00 %
  77. '(R) Round-trip: . . . . . . . -123.45
  78. '(X) Hexadecimal:. . . . . . . FFFFFF85
  79. '
  80. 'Standard DateTime Format Specifiers
  81. '(d) Short date: . . . . . . . 6/26/2004
  82. '(D) Long date:. . . . . . . . Saturday, June 26, 2004
  83. '(t) Short time: . . . . . . . 8:11 PM
  84. '(T) Long time:. . . . . . . . 8:11:04 PM
  85. '(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
  86. '(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
  87. '(g) General date/short time:. 6/26/2004 8:11 PM
  88. '(G) General date/long time: . 6/26/2004 8:11:04 PM
  89. '    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
  90. '(M) Month:. . . . . . . . . . June 26
  91. '(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
  92. '(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
  93. '(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
  94. '(U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
  95. '(Y) Year: . . . . . . . . . . June, 2004
  96. '
  97. 'Standard Enumeration Format Specifiers
  98. '(G) General:. . . . . . . . . Green
  99. '    (default):. . . . . . . . Green (default = 'G')
  100. '(F) Flags:. . . . . . . . . . Green (flags or integer)
  101. '(D) Decimal number: . . . . . 3
  102. '(X) Hexadecimal:. . . . . . . 00000003
  103. '

C#.NET

  1. // This code example demonstrates the String.Format() method.
  2. // Formatting for this example uses the "en-US" culture.
  3.  
  4. using System;
  5. class Sample
  6. {
  7.     enum Color {Yellow = 1, Blue, Green};
  8.     static DateTime thisDate = DateTime.Now;
  9.  
  10.     public static void Main()
  11.     {
  12. // Store the output of the String.Format method in a string.
  13.     string s = "";
  14.  
  15.     Console.Clear();
  16.  
  17. // Format a negative integer or floating-point number in various ways.
  18.     Console.WriteLine("Standard Numeric Format Specifiers");
  19.     s = String.Format(
  20.         "(C) Currency: . . . . . . . . {0:C}\n" +
  21.         "(D) Decimal:. . . . . . . . . {0:D}\n" +
  22.         "(E) Scientific: . . . . . . . {1:E}\n" +
  23.         "(F) Fixed point:. . . . . . . {1:F}\n" +
  24.         "(G) General:. . . . . . . . . {0:G}\n" +
  25.         "    (default):. . . . . . . . {0} (default = 'G')\n" +
  26.         "(N) Number: . . . . . . . . . {0:N}\n" +
  27.         "(P) Percent:. . . . . . . . . {1:P}\n" +
  28.         "(R) Round-trip: . . . . . . . {1:R}\n" +
  29.         "(X) Hexadecimal:. . . . . . . {0:X}\n",
  30.         -123, -123.45f);
  31.     Console.WriteLine(s);
  32.  
  33. // Format the current date in various ways.
  34.     Console.WriteLine("Standard DateTime Format Specifiers");
  35.     s = String.Format(
  36.         "(d) Short date: . . . . . . . {0:d}\n" +
  37.         "(D) Long date:. . . . . . . . {0:D}\n" +
  38.         "(t) Short time: . . . . . . . {0:t}\n" +
  39.         "(T) Long time:. . . . . . . . {0:T}\n" +
  40.         "(f) Full date/short time: . . {0:f}\n" +
  41.         "(F) Full date/long time:. . . {0:F}\n" +
  42.         "(g) General date/short time:. {0:g}\n" +
  43.         "(G) General date/long time: . {0:G}\n" +
  44.         "    (default):. . . . . . . . {0} (default = 'G')\n" +
  45.         "(M) Month:. . . . . . . . . . {0:M}\n" +
  46.         "(R) RFC1123:. . . . . . . . . {0:R}\n" +
  47.         "(s) Sortable: . . . . . . . . {0:s}\n" +
  48.         "(u) Universal sortable: . . . {0:u} (invariant)\n" +
  49.         "(U) Universal full date/time: {0:U}\n" +
  50.         "(Y) Year: . . . . . . . . . . {0:Y}\n",
  51.         thisDate);
  52.     Console.WriteLine(s);
  53.  
  54. // Format a Color enumeration value in various ways.
  55.     Console.WriteLine("Standard Enumeration Format Specifiers");
  56.     s = String.Format(
  57.         "(G) General:. . . . . . . . . {0:G}\n" +
  58.         "    (default):. . . . . . . . {0} (default = 'G')\n" +
  59.         "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
  60.         "(D) Decimal number: . . . . . {0:D}\n" +
  61.         "(X) Hexadecimal:. . . . . . . {0:X}\n",
  62.         Color.Green);      
  63.     Console.WriteLine(s);
  64.     }
  65. }
  66. /*
  67. This code example produces the following results:
  68.  
  69. Standard Numeric Format Specifiers
  70. (C) Currency: . . . . . . . . ($123.00)
  71. (D) Decimal:. . . . . . . . . -123
  72. (E) Scientific: . . . . . . . -1.234500E+002
  73. (F) Fixed point:. . . . . . . -123.45
  74. (G) General:. . . . . . . . . -123
  75.     (default):. . . . . . . . -123 (default = 'G')
  76. (N) Number: . . . . . . . . . -123.00
  77. (P) Percent:. . . . . . . . . -12,345.00 %
  78. (R) Round-trip: . . . . . . . -123.45
  79. (X) Hexadecimal:. . . . . . . FFFFFF85
  80.  
  81. Standard DateTime Format Specifiers
  82. (d) Short date: . . . . . . . 6/26/2004
  83. (D) Long date:. . . . . . . . Saturday, June 26, 2004
  84. (t) Short time: . . . . . . . 8:11 PM
  85. (T) Long time:. . . . . . . . 8:11:04 PM
  86. (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
  87. (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
  88. (g) General date/short time:. 6/26/2004 8:11 PM
  89. (G) General date/long time: . 6/26/2004 8:11:04 PM
  90.     (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
  91. (M) Month:. . . . . . . . . . June 26
  92. (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
  93. (s) Sortable: . . . . . . . . 2004-06-26T20:11:04
  94. (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
  95. (U) Universal full date/time: Sunday, June 27, 2004 3:11:04 AM
  96. (Y) Year: . . . . . . . . . . June, 2004
  97.  
  98. Standard Enumeration Format Specifiers
  99. (G) General:. . . . . . . . . Green
  100.     (default):. . . . . . . . Green (default = 'G')
  101. (F) Flags:. . . . . . . . . . Green (flags or integer)
  102. (D) Decimal number: . . . . . 3
  103. (X) Hexadecimal:. . . . . . . 00000003
  104.  
  105. */

Adding Customized Tags in web.config

Thứ năm, 16 Tháng mười, 2008

The web.config file in ASP.NET is the central location for your web applications configuration. It contains settings such as authentication, handler settings, compilation settings, globalization settings, tracing and error settings, etc… But what happens when this is not enough? Or you want to add you own settings into the web.config file. This tutorial will explain how it’s done. To create your own custom configuration handler, it will require two parts: writing some code, and editing your web.config file.

The code
Here we have a small C# file with code to create a new handler that will be used in the web.config file.

  1. using System;
  2. using System.Collections;
  3. using System.Xml;
  4. using System.Configuration;
  5. using System.Web.Configuration;
  6.  
  7. namespace Devhood {
  8.    
  9.     internal class PageStyleHandler:IConfigurationSectionHandler {
  10.         public virtual object Create(Object parent, Object context, XmlNode node) {
  11.             PageStyle config = new PageStyle((PageStyle)parent);
  12.             config.LoadValuesFromConfigurationXml(node);
  13.             return config;
  14.         }
  15.     }
  16.    
  17.     public class PageStyle {
  18.         string _backColour;
  19.        
  20.         internal PageStyle(PageStyle parent) {
  21.             if (parent != null)
  22.                 _backColour = parent._backColour;
  23.         }
  24.        
  25.         internal void LoadValuesFromConfigurationXml(XmlNode node) {
  26.             XmlAttributeCollection attribCol = node.Attributes;
  27.             _backColour = attribCol["backColour"].Value;
  28.         }
  29.        
  30.         public string BackColour {
  31.             get {
  32.                 return _backColour;
  33.             }
  34.         }
  35.     }
  36. }

There are two classes here, the PageStyleHandler class which implements the IConfigurationSectionHandler, and the PageStyle class which is used to store and retrieve the configuration data.
The PageStyleHandler contains the Create method. It is used to create and instance of the PageStyle class to pass the data from the web.config file.
The PageStyle class will accept an XML node which comes from the web.config file, it reads the attribute from the XML node and it will save the data for future retrieval by the BackColour property.

The web.config file
To add your custom handler to the web.config file for this application, it requires simply editing the web.config file so that it will accept your new handler. Your new web.config file will look like this:

  1. <configuration>
  2.     <configSections>
  3.         <sectionGroup name="devhood">
  4.             <section name="pageStyle" type="Devhood.PageStyleHandler, PageStyle" />
  5.         </sectionGroup>
  6.     </configSections>
  7.        
  8.     <devhood>
  9.         <pageStyle backColour="navy" />
  10.     </devhood>
  11. </configuration>

Note that this example will only apply to the web application that this file resides in. If you would like this new handler to apply to all web applications on this server, the tag can be moved to the machine.config.

An example usage in an ASPX page
Here is an example of our new custom handler in action:

  1.   <%@ Import Namespace="Devhood" %>
  2.  
  3. <html>
  4.     <head>
  5.         <title>ASP.NET Configuration</title>
  6.         <script language="C#" runat="server">
  7.             void Page_Load(Object sender, EventArgs e) {
  8.                 PageStyle _pageStyle;
  9.                 _pageStyle = (PageStyle) Context.GetConfig("devhood/pageStyle");
  10.                 bodyTag.Attributes["bgcolor"] = _pageStyle.BackColour;
  11.             }
  12.         </script>
  13.     </head>
  14.     <body id="bodyTag" runat="server">
  15.         <table bgcolor="white" align="center" width="400"><tr><td>
  16.         <p align="center">
  17.             <font size=+2>This background is Navy!</font>
  18.         </p>
  19.         <p align="center">
  20.             <font size=+2>Created for <a href="http://www.devhood.com">www.devhood.com</a></font>
  21.         </p>
  22.         </td></tr></table>
  23.     </body>
  24. </html>

The custom configuration handler in ASP.NET is a useful addition for creating really flexible web pages. Usage for custom configuration handlers can be for: allowing the web applications style to be defined in one web.config file, saving information that is commonly used (ie. DSN), and whatever else you can think of.

Copyright © 2001 Andrew Ma. at devhood.com

Free VB.NET 2005 Docking component

Thứ tư, 15 Tháng mười, 2008

All kinds of Third party controls exists to make a tabbed MDI possible. But (Vietnamese as I am) was looking for a good looking, free alternative. I came across DockPanel suite. Dockpanel suite is much more than a tabbed MDI only .
it’s possible to dock panels exactly like in VS.NET 2005. (with the nice looking, user friendly navigation)

VB.NET::Thao tác với Registry

Thứ hai, 13 Tháng mười, 2008

VB.NET cũng có hàm SaveSetting, GetSetting như VB6 giúp bạn thao tác với Resgistry, nhưng 2 hàm này chỉ thao tác tại một Location nhất định (location dành riêng cho ứng dụng). Nếu bạn muốn thao tác tai các location khác thì dùng 2 hàm này.

Một cái sai nhảm đến tức điên

Thứ hai, 06 Tháng mười, 2008

Hôm nay mình đụng phải một đoạn code UPDATE dữ liệu vào Database MS Access. Khi chạy, nó không hề báo lỗi gì cả nhưng dữ liệu không được Update. Đau đầu, tốn nhiều thời gian cho nó một cách thật bực mình.