Posts Tagged ‘vb.net’

How to convert .net Form to Image
  1. Public Class Form1
  2.  
  3.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  4.         CreateBitmap(Me)
  5.     End Sub
  6.  
  7.     Private Sub CreateBitmap(ByVal con As Control)
  8.         'Dim g As Graphics = Me.CreateGraphics()
  9.  
  10.         Dim b As Bitmap = New Bitmap(Me.Width, Me.Height)
  11.         Me.DrawToBitmap(b, New Rectangle(0, 0, Me.Width, Me.Height))
  12.  
  13.         b.Save("test.jpg", Imaging.ImageFormat.Jpeg)
  14.         b.Dispose()
  15.         'g.Dispose()
  16.     End Sub
  17. End Class

, ,

No Comments


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

VB.NET


  1. Imports System.Runtime.CompilerServices
  2.  
  3. Module StringExtensions
  4.  
  5.     <Extension()>
  6.     Public Sub Print(ByVal aString As String)
  7.         Console.WriteLine(aString)
  8.     End Sub
  9.  
  10.     <Extension()>
  11.     Public Sub PrintAndPunctuate(ByVal aString As String,
  12.                                  ByVal punc As String)
  13.         Console.WriteLine(aString & punc)
  14.     End Sub
  15.  
  16. End Module

To use that function

  1.     Sub Main()
  2.  
  3.         Dim example As String = "Example string"
  4.         example.Print()
  5.  
  6.         example = "Hello"
  7.         example.PrintAndPunctuate(".")
  8.         example.PrintAndPunctuate("!!!!")
  9.  
  10.     End Sub

C#.NET
If you notice the parameter there is a this keyword, that is how it done.

  1. namespace ExtensionMethods
  2. {
  3.     public static class StringExtensions
  4.     {
  5.         public static int Print(<strong>this </strong>String str)
  6.         {
  7.             Console.WriteLine(String );
  8.         }
  9.     }  
  10. }
  11.  
  12. string s = "Hello Extension Method";
  13. s.Print();

, , ,

No Comments


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())

}

, , ,

No Comments


The install location for prerequisites has not been set to

Follow this steps to solve this problem:
Update the Package Data

  1. 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
  2. Edit the Product.xml file in Notepad.
  3. Paste the following into the < PackageFiles > element:
  4. 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
  5. 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
  6. Save the product.xml file

Download and Extract the Core Installation Files

  1. Navigate to the following URL: http://go.microsoft.com/fwlink?LinkID=118080
  2. Download the dotNetFx35.exe file to your local disk.
  3. Open a Command Prompt window and change to the directory to which you downloaded dotNetFx35.exe.
  4. 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.

VS 2008 SP1 Readme

, , , ,

No Comments


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

, , ,

No Comments


How to get Month Name in .net


dim dt as Datetime

dt = Datetime.Today

Messabox.show(dt.ToString(“MMM”))

, ,

No Comments


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”)

,

No Comments


How to search Node in Treeview Control
  1.  Private Function search(ByVal Nod As TreeNode, ByVal StrFind As String) As TreeNode
  2.         If Nod Is Nothing Then Nod = Me.Nodes(0)
  3.         Dim tmpNod As TreeNode
  4.  
  5.         If String.Compare(Nod.Text, StrFind, True) = 0 Then
  6.             Return Nod
  7.         Else
  8.             For Each mNod As TreeNode In Nod.Nodes
  9.                 tmpNod = search(mNod, StrFind)
  10.                 If Not tmpNod Is Nothing Then
  11.                     Return tmpNod
  12.                 End If
  13.             Next
  14.         End If
  15.         Return Nothing
  16.     End Function

Usage:

  1. dim nod as TreeNode
  2.  
  3. nod = search(me.MyTreeViewControl.Nodes(0), "My search")

, ,

No Comments


How to remove parent node checkbox of the Treeview

Add this script to your common module
VB.NET

Imports System.Runtime.InteropServices
  1.  
  2. Public Const TVIF_STATE As Integer = &amp;H8
  3. Public Const TVIS_STATEIMAGEMASK As Integer = &amp;HF000
  4. Public Const TV_FIRST As Integer = &amp;H1100
  5. Public Const TVM_SETITEM As Integer = TV_FIRST + 63
  6.  
  7.  Public Structure TVITEM
  8.     Public mask As Integer
  9.     Public hItem As IntPtr
  10.     Public state As Integer
  11.     Public stateMask As Integer
  12.      Public lpszText As String
  13.     Public cchTextMax As Integer
  14.     Public iImage As Integer
  15.     Public iSelectedImage As Integer
  16.     Public cChildren As Integer
  17.     Public lParam As IntPtr
  18. End Structure
  19.  
  20.     Public Sub RemoveCheckbox(ByVal nod As SiteTreeNode)
  21.         Dim tvi As TVITEM
  22.         tvi.hItem = nod.Handle
  23.         tvi.mask = TVIF_STATE
  24.         tvi.stateMask = TVIS_STATEIMAGEMASK
  25.         tvi.state = nod.Index &lt;&lt; 12
  26.         SendMessage(nod.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, tvi)
  27.     End Sub

Then call the subroutine
Public Sub TreeNode_SetStateImageIndex(ByVal node As TreeNode, ByVal index
As Integer)
RemoveCheckbox(node)
End Sub

C#

  1.  
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace Treeview___CheckBoxes
  5. {
  6.  public partial class Form1 : Form
  7.  {
  8.   public Form1()
  9.   {
  10.  InitializeComponent();
  11.   }
  12.  private void Form1_Load(object sender, EventArgs e)
  13.  {
  14.  // Iterate over the root nodes, removing their checkboxes
  15.  for (int n = 0; n<treeView1.Nodes.Count; n++)
  16.  {
  17.   TreeNode node = treeView1.Nodes[n];
  18.   TVITEM tvItem = new TVITEM();
  19.   tvItem.hItem = node.Handle;
  20.   tvItem.mask = TVIF_STATE;
  21.   tvItem.stateMask = TVIS_STATEIMAGEMASK;
  22.   tvItem.state = 0;
  23.   IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
  24.   Marshal.StructureToPtr(tvItem, lparam, false);
  25.   SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
  26.  }
  27.  }
  28.  public const int TVIF_STATE = 0×8;
  29.  public const int TVIS_STATEIMAGEMASK = 0xF000;
  30.  public const int TV_FIRST = 0×1100;
  31.  public const int TVM_SETITEM = TV_FIRST + 63;
  32.  public struct TVITEM
  33.  {
  34.   public int mask;
  35.   public IntPtr hItem;
  36.   public int state;
  37.   public int stateMask;
  38.   [MarshalAs(UnmanagedType.LPTStr)]
  39.   public String lpszText;
  40.   public int cchTextMax;
  41.   public int iImage;
  42.   public int iSelectedImage;
  43.   public int cChildren;
  44.   public IntPtr lParam;
  45.  }
  46.   [DllImport(“user32.dll)]  static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  47.  
  48.  }
  49. }

Sources: c#  | vb.net

, , ,

No Comments


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()])

, ,

No Comments



SetPageWidth