Archive for the ‘Controls’ Category

Dropdown List event not firing

Just make sure to set the AutoPostBack = true.

, ,

No Comments


How to Print Datagrid in ASP.Net

To print the datagrid data alone, you need to encapsulate the datagrid using div element.

Example:

  1. <script language="javascript" type="text/javascript">
  2. function PrintDataGrid(elementname) {
  3. var oDiv = document.getElementById(elementname);
  4. var oprint_window = window.open('', '', 'letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
  5.  
  6. oprint_window.document.write(oDiv.innerHTML);
  7. oprint_window.document.close();
  8. oprint_window.focus();
  9. oprint_window.print();
  10. oprint_window.close();
  11. }
  12. </script>
  13.  
  14.  
  15. <div id="DatagridData">
  16. <asp:Datagrid>…</asp:Datagrid>
  17. </div>
  18.  
  19. <asp:Button ID="btnPrint" runat="server" OnClientClick="javascript:PrintDataGrid('DatagridData');" />

or

  1. <input id="Button1" type="button" value="button" onclick="javascript:PrintDataGrid('DatagridData')" />

,

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


How to add object to a combobox

To add the object as an item to a combobox is pretty easy, it is the same as you are adding a string/numeric value.

Example:

  1. Public class X
  2. Public Name as String
  3. Public Value as String
  4.  
  5. Public Sub New(StrName As String, StrValue As String)
  6. Name = StrName
  7. Value = StrValue
  8. End Sub
  9. End Class
  10.  
  11. To fill the combobox with the created object, let's say you have a combo box called cboX
  12. dim Obj as X
  13. For i as Integer = 0 to 10
  14.            cboX.Items.Add(New X("Name " & i.ToString(), "Value " & i.ToString())
  15. Next

Then you need to set the DisplayMember property of the Combo box so that the Text displayed in the
combobox is not “namespace.X”.

  1. cboX.DisplayMember = "Name"

,

No Comments


How to Adjust DropdownWidth of the ComboBox

use this script to adjust the size of the combobox

private void AdjustWidthComboBox_DropDown(object sender, System.EventArgs e)
{
ComboBox senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth =
(senderComboBox.Items.Count>senderComboBox.MaxDropDownItems)
?SystemInformation.VerticalScrollBarWidth:0;

int newWidth;
foreach (string s in ((ComboBox)sender).Items)
{
newWidth = (int) g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth )
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}

Go to Source

, , , ,

No Comments


Add Custom Object to Combo box

Use Items.add (object) and make sure your object class is overriding the ToString()

,

No Comments


Specified cast is not valid in WriteXml using Dataset

This exception appears when using WriteXml of datatable or dataset. This is also caused by when you create a datatable manually and you set the “defaultvalue” to one or more datacolumn. Remove the “defaultvalue” and it will be fine.

For more information on setting defaultvalue click here

, , ,

No Comments


A Generic error occured in GDI+

Source Control:
If this exception occurred in DataGrid Control, check if the displayed data is large. This might be the caused of the exception.

, ,

No Comments


How to get Last Directory of OpenDialog box in .net

You can use the InitialDirectory property of the dialog box to get the last directory browsed.

, , ,

No Comments



SetPageWidth