Archive for the ‘Controls’ Category
How to remove / disable focus of the control
Public Class NotSelectableButton
Inherits Button
Public Sub New()
MyBase.New()
' following line will make this button Not Focusable
SetStyle(ControlStyles.Selectable, False)
End Sub
End Class
How create a Container User Control
Posted by: admin in .NET, User Control on December 28th, 2010
-
Imports System.ComponentModel
-
Imports System.ComponentModel.Design
-
-
<Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _
-
Public Class UserControl1
-
-
End Class
Set SelectedIndex of a DataGridViewComboBoxCell
Posted by: admin in .NET, Controls, Datagridview on September 25th, 2010
-
-
'Fill
-
Dim mcbo As DataGridViewComboBoxColumn = dgvProperty.Columns("colPositionReport")
-
Dim obj As New Dictionary(Of Integer, String)
-
Dim mbinder As New BindingSource
-
-
obj.Add(1, "X Dimension")
-
obj.Add(2, "Y Dimension")
-
obj.Add(3, "Inactive")
-
obj.Add(4, "Fact")
-
mbinder.DataSource = obj
-
mcbo.DataSource = mbinder
-
-
mcbo.ValueMember = "Key"
-
mcbo.DisplayMember = "Value"
-
-
'Selecting
-
-
Dim dr As DataGridViewRow
-
-
With myGridView
-
dr = .Rows(0)
-
mcbo = dgvProperty.Columns("comboboxcolumn")
-
dr.Cells("colPositionReport").Value = 1
-
End With
How to Print Datagrid in ASP.Net
To print only the content of the datagrid control, you need to encapsulate it with div element.
Example:
-
<script type="text/javascript" language="javascript">// <![CDATA[
-
function PrintDataGrid(elementname) {
-
var oDiv = document.getElementById(elementname);
-
var oprint_window = window.open('', '', 'letf=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
-
-
oprint_window.document.write(oDiv.innerHTML);
-
oprint_window.document.close();
-
oprint_window.focus();
-
oprint_window.print();
-
oprint_window.close();
-
}
-
// ]]></script>
or
-
<input id="Button1" onclick="javascript:PrintDataGrid('DatagridData')" type="button" value="button" />
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);
-
-
}
-
}
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:
-
Public class X
-
Public Name as String
-
Public Value as String
-
-
Public Sub New(StrName As String, StrValue As String)
-
Name = StrName
-
Value = StrValue
-
End Sub
-
End Class
-
-
To fill the combobox with the created object, let's say you have a combo box called cboX
-
dim Obj as X
-
For i as Integer = 0 to 10
-
cboX.Items.Add(New X("Name " & i.ToString(), "Value " & i.ToString())
-
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”.
-
cboX.DisplayMember = "Name"
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;
}
Add Custom Object to Combo box
Use Items.add (object) and make sure your object class is overriding the ToString()
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