Posts Tagged ‘vb.net’
How to convert .net Form to Image
-
Public Class Form1
-
-
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
-
CreateBitmap(Me)
-
End Sub
-
-
Private Sub CreateBitmap(ByVal con As Control)
-
'Dim g As Graphics = Me.CreateGraphics()
-
-
Dim b As Bitmap = New Bitmap(Me.Width, Me.Height)
-
Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
-
-
b.Save("test.jpg", Imaging.ImageFormat.Jpeg)
-
b.Dispose()
-
'g.Dispose()
-
End Sub
-
End Class
Extension method in .net
Extension methods enable developers to add a method to an existing class without deriving or affecting the existing class. This extension methods only support a sub routine or a function.
Example if we want to extend the functionality of the String type
-
Imports System.Runtime.CompilerServices
-
-
Module StringExtensions
-
-
<Extension()>
-
Public Sub Print(ByVal aString As String)
-
Console.WriteLine(aString)
-
End Sub
-
-
<Extension()>
-
Public Sub PrintAndPunctuate(ByVal aString As String,
-
ByVal punc As String)
-
Console.WriteLine(aString & punc)
-
End Sub
-
-
End Module
To use that function
-
Sub Main()
-
-
Dim example As String = "Example string"
-
example.Print()
-
-
example = "Hello"
-
example.PrintAndPunctuate(".")
-
example.PrintAndPunctuate("!!!!")
-
-
End Sub
C#.NET
If you notice the parameter there is a this keyword, that is how it done.
-
namespace ExtensionMethods
-
{
-
public static class StringExtensions
-
{
-
public static int Print(<strong>this </strong>String str)
-
{
-
Console.WriteLine(String );
-
}
-
}
-
}
-
-
string s = "Hello Extension Method";
-
s.Print();
How to calculate Time span in .Net
Here’s how to code the time span;
Function ExecuteLargeRec() {
DateTime d1 = DateTime.Now
DateTime d2
TimeSpan ts
‘ Your query here / processing
d2 = DateTime.Now
ts = d1.Subtract(d2)
MessageBox.Show(ts.ToString())
}
The install location for prerequisites has not been set to
Posted by: admin in .NET, Deployment on June 9th, 2010
Follow this steps to solve this problem:
Update the Package Data
- Open the [Program Files]\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder or %ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems
- Edit the Product.xml file in Notepad.
- Paste the following into the < PackageFiles > element:
- Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-x86-en-US.exe” and change the PublicKey value to:
3082010A0282010100A2DB0A8DCFC2C1499BCDAA3A34AD23596BDB6CBE2122B79
4C8EAAEBFC6D526C232118BBCDA5D2CFB36561E152BAE8F0DDD14A36E284C7F163
F41AC8D40B146880DD98194AD9706D05744765CEAF1FC0EE27F74A333CB74E5EFE
361A17E03B745FFD53E12D5B0CA5E0DD07BF2B7130DFC606A2885758CB7ADBC85E
817B490BEF516B6625DED11DF3AEE215B8BAF8073C345E3958977609BE7AD77C13
78D33142F13DB62C9AE1AA94F9867ADD420393071E08D6746E2C61CF40D5074412
FE805246A216B49B092C4B239C742A56D5C184AAB8FD78E833E780A47D8A4B2842
3C3E2F27B66B14A74BD26414B9C6114604E30C882F3D00B707CEE554D77D208557
6810203010001 - Find the element for < PackageFile Name=”dotNetFX30\XPSEPSC-amd64-en-US.exe” and change the PublicKey value to the same as in step 4 above
- Save the product.xml file
Download and Extract the Core Installation Files
- Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
- Download the dotNetFx35.exe file to your local disk.
- Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
- At the command prompt, type:
dotNetFx35.exe /x:.
This will extract the Framework files to a folder named “WCU” in the current directory.
Copy the contents of the WCU\dotNetFramework folder and paste them in the %Program Files%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 folder (%ProgramFiles(x86)%\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\DotNetFx35SP1 on x64 operating systems).
Note: Do not copy the WCU\dotNetFramework folder itself. There should be 5 folders under the WCU folder, and each of these should now appear in the DotNetFx35SP1 folder. The folder structure should resemble the following: DotNetFx35SP1 (folder)
-
- dotNetFX20 (folder
- dotNetFX30 (folder)
- dotNetFX35 (folder)
- dotNetMSP (folder)
- TOOLS folder)
- en (or some other localized folder)
- dotNetFx35setup.exe (file)
You may now delete the files and folders you downloaded and extracted in steps 2 and 4.
You can read more on the attached readme file.
Cannot write to the registry key in .Net
Public Function SetValue(ByVal Value As String, ByVal k As String) As String
Dim mReg As RegistryKey = Registry.LocalMachine.OpenSubKey(“Software”, RegistryKeyPermissionCheck.ReadWriteSubTree, Security.AccessControl.RegistryRights.FullControl)
Try
mReg.OpenSubKey(My.Application.Info.CompanyName & “\” & My.Application.Info.Description, True).SetValue(k, Value)
Return True
Catch ex As Exception
Throw ex
End Try
End Function
How to get Month Name in .net
Posted by: admin in .NET, Date Formatting on May 25th, 2010
dim dt as Datetime
dt = Datetime.Today
Messabox.show(dt.ToString(“MMM”))
Enum: How to get the underlying value given the name
Use Parse method of the enum to get the value:
Enum x
a = 1
b = 2
End Enum
dim e as x
e = [enum].Parse(GetType(x), “a”)
How to search Node in Treeview Control
-
Private Function search(ByVal Nod As TreeNode, ByVal StrFind As String) As TreeNode
-
If Nod Is Nothing Then Nod = Me.Nodes(0)
-
Dim tmpNod As TreeNode
-
-
If String.Compare(Nod.Text, StrFind, True) = 0 Then
-
Return Nod
-
Else
-
For Each mNod As TreeNode In Nod.Nodes
-
tmpNod = search(mNod, StrFind)
-
If Not tmpNod Is Nothing Then
-
Return tmpNod
-
End If
-
Next
-
End If
-
Return Nothing
-
End Function
Usage:
-
dim nod as TreeNode
-
-
nod = search(me.MyTreeViewControl.Nodes(0), "My search")
How to remove parent node checkbox of the Treeview
Add this script to your common module
VB.NET
-
-
Public Const TVIF_STATE As Integer = &H8
-
Public Const TVIS_STATEIMAGEMASK As Integer = &HF000
-
Public Const TV_FIRST As Integer = &H1100
-
Public Const TVM_SETITEM As Integer = TV_FIRST + 63
-
-
Public Structure TVITEM
-
Public mask As Integer
-
Public hItem As IntPtr
-
Public state As Integer
-
Public stateMask As Integer
-
Public lpszText As String
-
Public cchTextMax As Integer
-
Public iImage As Integer
-
Public iSelectedImage As Integer
-
Public cChildren As Integer
-
Public lParam As IntPtr
-
End Structure
-
-
Public Sub RemoveCheckbox(ByVal nod As SiteTreeNode)
-
Dim tvi As TVITEM
-
tvi.hItem = nod.Handle
-
tvi.mask = TVIF_STATE
-
tvi.stateMask = TVIS_STATEIMAGEMASK
-
tvi.state = nod.Index << 12
-
SendMessage(nod.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, tvi)
-
End Sub
Then call the subroutine
Public Sub TreeNode_SetStateImageIndex(ByVal node As TreeNode, ByVal index
As Integer)
RemoveCheckbox(node)
End Sub
C#
-
-
using System.Runtime.InteropServices;
-
-
namespace Treeview___CheckBoxes
-
{
-
public partial class Form1 : Form
-
{
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void Form1_Load(object sender, EventArgs e)
-
{
-
// Iterate over the root nodes, removing their checkboxes
-
for (int n = 0; n<treeView1.Nodes.Count; n++)
-
{
-
TreeNode node = treeView1.Nodes[n];
-
TVITEM tvItem = new TVITEM();
-
tvItem.hItem = node.Handle;
-
tvItem.mask = TVIF_STATE;
-
tvItem.stateMask = TVIS_STATEIMAGEMASK;
-
tvItem.state = 0;
-
IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
-
Marshal.StructureToPtr(tvItem, lparam, false);
-
SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
-
}
-
}
-
public const int TVIF_STATE = 0×8;
-
public const int TVIS_STATEIMAGEMASK = 0xF000;
-
public const int TV_FIRST = 0×1100;
-
public const int TVM_SETITEM = TV_FIRST + 63;
-
public struct TVITEM
-
{
-
public int mask;
-
public IntPtr hItem;
-
public int state;
-
public int stateMask;
-
[MarshalAs(UnmanagedType.LPTStr)]
-
public String lpszText;
-
public int cchTextMax;
-
public int iImage;
-
public int iSelectedImage;
-
public int cChildren;
-
public IntPtr lParam;
-
}
-
[DllImport(“user32.dll”)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
-
-
}
-
}
Sum with empty element on XPath using .Net
When using sum function on XPath using .net you will get errors “Nan” if the node is empty because it use for numbers only. So, you need to select the nodes/elements that are not empty.
sum(//Quantity[node()])