 |
|
 |
|
|
|
|
Filtering TextBox Input |
|
Andy's Blog
|
By Andy Beaulieu on
8/20/2004 8:56 AM
|
|
|
|
Sometimes it's handy to create an inherited textbox that only accepts certain keys. For example, a textbox for inputing a monetary value. To do so, create a new user control, then change it's inherits clause to System.Windows.Forms.TextBox. You can then implement the KeyPress event to filter out any ANSI keyvalues. You can also implement the KeyDown event to handle other control keys such as the arrow keys.
Note that setting Handled = True will prevent the textbox from processing the key.
Private Sub ucTextBoxFilter_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
' allow only numerics
If Not IsNumeric(e.KeyChar) Then
e.Handled = True
End If
End Sub
|
 |
|
Comments (0)
|
|
|
|
A Simple Threading Sample for WindowsForms |
|
Andy's Blog
|
By Andy Beaulieu on
2/1/2004 8:28 PM
|
|
|
|
You can use a separate thread to, for example, fill a long ListBox without making the user wait. You must create a delegate and use the Invoke method on the form (or a control) so that the separate thread can safely update the UI. Here is an example that fills a ListBox with all customers from a separate thread. Note that the data comes from a simple Middle-Tier component, which is beyond the scope of this post.
Imports System.Threading
Public Class frmThreadUI
Inherits System.Windows.Forms.Form
Private Sub LoadCustomers()
' this function will execute on a separate thread.
Dim dsCustomers As CustomerSet = Customer.GetAll()
' call our threadsafe DisplayCustomers from our separate thread
DisplayCustomers_ThreadSafe(dsCustomers)
End Sub
Private Sub DisplayCustomers_ThreadSafe(ByVal dsCustomers As CustomerSet)
' this is a threadsafe method called from the separate worker thread
Dim oDel As New DisplayCustomersDelegate(AddressOf Me.DisplayCustomers)
Dim args() As DataSet = {dsCustomers}
Me.Invoke(oDel, args)
End Sub
' define a delegate (signature) for the callback.
Delegate Sub DisplayCustomersDelegate(ByVal dsCustomers As CustomerSet)
Private Sub DisplayCustomers(ByVal dsCustomers As CustomerSet)
' display the customer list in the ListBox
lstCustomers.DataSource = dsCustomers.Tables(0)
lstCustomers.DisplayMember = "CompanyName"
lstCustomers.ValueMember = "CustomerId"
End Sub
Private Sub frmThreadUI_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
' give a "loading" message.
lstCustomers.Items.Add("Loading... please wait.")
' kick off a separate thread to load the Customers into a ListBox
Dim oTS As New ThreadStart(AddressOf LoadCustomers)
Dim oThread As Thread = New Thread(oTS)
oThread.Start()
End Sub
End Class
|
 |
|
Comments (0)
|
|
|
|
Populating a Crystal Report from DataSets |
|
Andy's Blog
|
By Andy Beaulieu on
1/14/2004 9:00 AM
|
|
|
|
In an n-tier application using Crystal Reports, it is necessary to populate reports from offline DataSets instead of using a direct DB connection. This class allows feeding a Crystal Report with 1 or more DataSets (in the case of subreports, you can have the DataSets in the same order as they appear in the main Crystal Report).
Imports CrystalDecisions.CrystalReports.Engine Imports CrystalDecisions.Shared
Public Class CCrystalUtilities
Public Shared Function GetReportDocument(ByRef oRpt As ReportDocument, ByRef dsDataSets() As DataSet) As ReportDocument
' this function accepts a Path to a report and an array of DataSets to use ' to fill the report (in the case of nested reports, we need to use multiple ' datasets per report)
Dim iDataSetNum As Integer
' set the main report's DataSource oRpt.SetDataSource(dsDataSets(iDataSetNum)) iDataSetNum += 1
Dim mySection As Section Dim mySections As Sections Dim myReportObject As ReportObject Dim myReportObjects As ReportObjects Dim mySubReportObject As SubreportObject Dim mySubRepDoc As New ReportDocument()
'Declare all of the sections of the main report mySections = oRpt.ReportDefinition.Sections
'Loop through the sections in the main report to find the 'subreport(objects) 'then open the subreport and set the datasource to the subreport()
For Each mySection In mySections myReportObjects = mySection.ReportObjects
For Each myReportObject In myReportObjects If myReportObject.Kind = ReportObjectKind.SubreportObject Then 'Subreport Found - convert the report to a sub report type mySubReportObject = CType(myReportObject, SubreportObject)
'Set the specific instance of this subreport mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
mySubRepDoc.SetDataSource(dsDataSets(iDataSetNum)) iDataSetNum += 1 End If Next Next
' return the complete report Return oRpt
End Function
End Class
|
 |
|
Comments (5)
|
|
|
|
|
 |
|
 |
|
|