2008-06-08
Make your programs run on Windows startup
Sample Code.
'Write to register
Dim regKey As Microsoft.Win32.RegistryKeyregKey
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", True)
regKey.SetValue("YourApplicationName", "YourInstallPath")
regKey.Close()
If you want to remove from register use this code.
regKey.DeleteValue("YourApplicationName", False)
2008-06-03
Insert picture into Report
1. Add picture column in ReportDataset.xsd and set data type to System.Byte() then compile project.
2. Open CustomerReport.rpt , right click Database Fields and select Verify Database menu. Insert Picture column into report.
3. At CreateDataSet function insert this code for add new picture data column.
dt.Columns.Add("Picture", Type.GetType("System.Byte[]"))
4. Create new function.
Private Function CreatePictureArray(ByVal bImagePath As String) As Byte()
Dim imgLogo As System.Drawing.Image = System.Drawing.Image.FromFile(bImagePath)
Dim msLogo As New IO.MemoryStream() imgLogo.Save(msLogo,
System.Drawing.Imaging.ImageFormat.Bmp)
Return msLogo.ToArray()
End Function
5. Convert image to byte array and bind to datarow.
' Create new row
Dim dr As DataRow = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Tom"
dr("LastName") = "Brown"
dr("Address") = "Tom Address"
dr("Picture") = CreatePictureArray("c:\person.jpg")
6. Show Report
2008-05-10
Create Report
1. Create new visual basic project.
2. Add button and CrystalReportViewer to form1.
3. Add new DataSet item.
4. Add DataTable in dataset design page and add data table columns.
- Title, FirstName, LastName, Address
6. On Field Explorer right click at Database Fields and select Database Expert.
7. Select Project Data -> ADO.NET DataSets and add customer table to ReportDataset.8. Design Report (Add report header name txtHeader and report footer name txtFooter).
9. Sample code to show report.
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
' Get data to dataset
Dim CustomerData As DataSet = CreateDataSet()
' Create report instance and set data to report data source
Dim rpt As New CustomerReport
rpt.SetDataSource(CustomerData.Tables(0))
' Access crystal report object
' Sample to access textbox object
Dim txtHeader As CrystalDecisions.CrystalReports.Engine.TextObject = rpt.Section2.ReportObjects("txtHeader")
txtHeader.Text = "This is Report Header"
Dim txtFooter As CrystalDecisions.CrystalReports.Engine.TextObject = rpt.Section2.ReportObjects("txtFooter")
txtFooter.Text = "This is Report Footer"
' Show Report
Me.CrystalReportViewer1.ReportSource = rpt
End Sub
Private Function CreateDataSet() As DataSet
Dim ds As New DataSet("ReportDataset")
' Create Table
Dim dt As New DataTable("Customer")
dt.Columns.Add("Title", Type.GetType("System.String"))
dt.Columns.Add("FirstName", Type.GetType("System.String"))
dt.Columns.Add("LastName", Type.GetType("System.String"))
dt.Columns.Add("Address", Type.GetType("System.String"))
' Add DataTable to DataSet
ds.Tables.Add(dt)
' Create new row
Dim dr As DataRow = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Tom"
dr("LastName") = "Brown"
dr("Address") = "Tom Address"
ds.Tables(0).Rows.Add(dr)
dr = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "John"
dr("LastName") = "Grary"
dr("Address") = "John Address"
ds.Tables(0).Rows.Add(dr)
dr = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Alex"
dr("LastName") = "Smith"
dr("Address") = "Alex Address"
ds.Tables(0).Rows.Add(dr)
Return ds
End Function
10. Show Report.
2008-05-05
BackgroundWorker
BackgroundWorker Class
Executes an operation on a separate thread.
Sample Code
1.Create event DoWork.
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Sample how to disable button send before send email method after that enabled button.
DisableSendButton()
' to do SendEmail method
EnabledSendButton()
End Sub
2. Show how to invoke method when use BackgroundWorker control.
Private Sub DisableSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(DisableSendButton))
Else
btnSend.Enabled = False
btnCancelSend.Visible = True
End If
End Sub
Private Sub EnabledSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(EnabledSendButton))
Else
btnSend.Enabled = True
btnCancelSend.Visible = False
End If
End Sub
3.Start BackgroundWorker
BackgroundWorker1.RunWorkerAsync()
2008-03-01
CheckedListBox
Add Object to CheckedListBox
Dim pItem As CarItem
pItem = New CarItem()
pItem.CarId = 1
pItem.CarName="CarName"
' Add this object to CheckListBox
' And CheckListBox show CarName because I override toString function
Me.CheckListBox1.Items.Add(pItem, True)
' Class CarItem
Private Class CarItem
Public CarId As Integer
Public CarName As String
Public Sub New()
End Sub
' Override tostring
Public Overrides Function ToString() As String
Return CarName
End Function
End Class
Get status items in CheckedListBox
For i As Integer = 0 To Me.CheckedListBox.Items.Count - 1
If Me.CheckedListBox.GetItemChecked(i) Then
'TODO Something
End If
Next i
Get only Checked Items
For i As Integer = 0 To Me.CheckedListBox.CheckedItems.Count - 1
'TODO Something
Next
Set all item checked
For i As Integer = 0 To Me.CheckedListBox.Items.Count-1
Me.CheckedListBox.SetItemChecked(i, true)
Next i
2008-02-23
Windows Controls
A control is an object that can be drawn on to the Form to enable or enhance user interaction with the application. Examples of these controls, TextBoxes, Buttons, Labels, Radio Buttons, etc. All these Windows Controls are based on the Control class, the base class for all controls. Visual Basic allows us to work with controls in two ways: at design time and at runtime. Working with controls at design time means, controls are visible to us and we can work with them by dragging and dropping them from the Toolbox and setting their properties in the properties window. Working at runtime means, controls are not visible while designing, are created and assigned properties in code and are visible only when the application is executed. There are many new controls added in Visual Basic .NET and we will be working with some of the most popular controls in this section. You can select the controls from the menu towards the left-hand side of this page.
Notable properties of most of these Windows Controls which are based on the Control class itself are summarized in the table below. You can always find the properties of the control with which you are working by pressing F4 on the keyboard or by selecting View->Properties Window from the main menu.
The Control Class
The Control class is in the System.Windows.Forms namespace. It is a base class for the Windows Controls. The class hierarchy is shown below.
Object
MarshalByRefObject
Component
Control
ButtonBase, Etc, Etc
Button, Etc, Etc
Main class is the Object class from which MarshalByRefObject class is derived and the Component class is derived from the MarshalByRefObject class and so on.
The properties of the Control object are summarized below. Properties are alphabetical as seen in the properties window.
Control Tab Order
To move focus from one control to other quickly using the keyboard we can use the Tab key. We can set the order in which the focus is transferred by setting the tab order. The tab order is the order in which controls on the form receive the focus and is specified by the TabIndex property. To change the order in which a control receives focus we need to set the TabIndex property to different value for each control on the form. Lower values receive the focus first and proceed numerically through higher values. If there is a tie between TabIndex values, the focus first goes to the control that is closest to the front of the form.
We can also set the tab order graphically with Visual Studio by selecting Tab Index from the View menu. Boxes containing current tab order appear in each control when you select Tab Index from View menu. Click each control to set the correct tab order in which you want the controls to receive focus.
2008-01-15
Check Database Memory
- In TaskPad, Enterprise Manager reported that the database had 211549.75MB of allocated space, 110294.44MB of used space, and 101255.31MB of free space.
- For the same database, the sp_Spaceused stored procedure told me I had 212113.50MB of allocated space and 19541.14MB of unallocated space.
- The sp_helpDB stored procedure reported that I had an MDF file size of 211549.75MB.
Can I use T-SQL to return the same information that I see when I use the TaskPad?
To generate the information on the TaskPad, Enterprise Manager uses a combination of the following four queries.
EXEC sp_spaceused
SELECT fileid, name, filename, size, growth, status, maxsize
FROM dbo.sysfiles WHERE (status & 0x40) <>0
DBCC sqlperf(logspace)
DBCC showfilestats
Enterprise Manager uses SQL-DMO to retrieve the information. . . .
Content by Microsoft's SQL Server Development Team Ask Microsoft