|
thanks for the reply....i actually was going about it wrong
what i'm doing now (to my disguntlement) is keeping an arraylist of my objects in the session collection, and databinding to it...it's working fine
i assumed that the ListBox.Items collection would hold objects (as long as they had a tostring() method), but i was wrong. you can, however databind to any object collection/list, as long as the tostring() method is provided, or a datavaluemember and datatextmember are specified
michael griffith
--------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
|
|
|
|
|
Can anyone explain how I can copy an Excel chart to the clipboard? It only works with text for me.
Tnx
|
|
|
|
|
Is anything equivalent in VB.Net with the WebBrowser of VB.6?
|
|
|
|
|
hi, all
The following code giving me an overflow error.
Please tell me what is wrong in code.
Private Sub Command1_Click()
Dim x As Long
Dim y As Integer
y = 10
x = 11025 * y
MsgBox x
End Sub
|
|
|
|
|
VB6 question right?
Dim x As Long
Dim y As Integer
y = 10
x = CLng(11025) * y
MsgBox x
Works fine.
But, so does this:
Dim x As Long
Dim y As Integer
y = 10
x = 11025 * CLng(y)
MsgBox x
By using 2 integers as operands, the compiler is presuming that the result will also be an integer.
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
hi,Simon thanks for your reply
but,take this code, it is working fine
with out using any type conversion
Please..
Dim x As Long
Dim y As Integer
y = 10
x = 55555 * y
MsgBox x
|
|
|
|
|
I don't have MSDN for VS6, but the upper limit for an integer is ~32,000, so in your example the "55,555" value is seen as a VB Long.
Basically, it's like this:
a. long = long * integer
b. integer = integer * integer
Your example corresponds to "a" and your initial post is like "b". The reason that "b" gave an overflow is that there's the potential for the product of 2 integers to exceed the max value for an integer (the compiler does it's best).
HTH
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
|
Ok, I have been messing around with dynamic loading of controls now all day and have a cool example that works, but I am getting some strange results on one area.
I am trying to add a way to get messages into the control that I am loading, but I digress. Here is an example of what I have:
1) I have a control that I use as a base (Called CtlBase.BaseControl) and it is defined as such:
Public MustInherit Class BaseControl
Inherits System.Windows.Forms.UserControl
Public Event MessageExit(ByVal OutputMessage As String)
Public MustOverride WriteOnly Property MessageEntry(ByVal InputMessage As String)
Protected Sub SendEvent(ByVal EventMessage As String)
RaiseEvent MessageExit(EventMessage)
End Sub
End Class
I have another project that inherits from this base control declared as such:
Public Class Ctl1
Inherits CtlBase.BaseControl
Private Sub cmdSendEvent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSendEvent.Click
SendEvent(txtSendMessage.Text)
End Sub
Public Overrides WriteOnly Property MessageEntry(ByVal InputMessage As String)
Set(ByVal Value)
txtRecvdmessage.Text = Value
End Set
End Property
End Class
Now, I have another project that is loading the Ctrl1 dynamicaly, tieing up the event that the control exsposes (that is working fine) and trying to assign data to the WriteOnly property that the control exsposes. This app looks like such:
Imports System.Reflection
Public Class Form1
Inherits System.Windows.Forms.Form
Dim c As Control
Dim m As CtlBase.BaseControl
Private Sub cmdLoad1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdLoad1.Click
Dim myObj As Object
Dim myAssem As System.Reflection.Assembly
myAssem = myAssem.LoadFrom("..\..\ControlLib\bin\ControlLib.dll")
myObj = myAssem.CreateInstance("ControlLib.Ctl1")
c = CType(myObj, CtlBase.BaseControl)
c.Dock = DockStyle.Fill
m = DirectCast(c, CtlBase.BaseControl)
AddHandler m.MessageExit, AddressOf Ctl_Click_Handler
pnlMainPanel.Controls.Add(c)
End Sub
Private Sub Ctl_Click_Handler(ByVal Message As String)
MessageBox.Show(Message & " : Was received.")
End Sub
Private Sub cmdCommand_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCommand.Click
m.MessageEntry(txtCommand.Text) = txtCommand.Text
End Sub
End Class
The very odd thing is the way that I have to do what is listed in BOLD
If I try this as:
m.MessageEntry(txtCommand.Text)
I get a -
"I:\RUS\SnapinTest\Application\Form1.vb(155): Property 'MessageEntry' is 'WriteOnly'."
If I try it as:
m.MessageEntry = txtCommand.Text
I get a -
"I:\RUS\SnapinTest\Application\Form1.vb(155): Argument not specified for parameter 'InputMessage' of 'Public MustOverride WriteOnly Property MessageEntry(InputMessage As String) As Object'."
Can ANYONE please tell me what the hell I am doing wrong?
I mean the 'm.MessageEntry(txtCommand.Text) = txtCommand.Text' code does what I want it to, but WHY? It looks very odd to me....
HELP!!!!!
|
|
|
|
|
Change the declaration of the MessageEntry property to:
Public MustOverride WriteOnly Property MessageEntry As String
Then, in Ctl :
Public Overrides WriteOnly Property MessageEntry() As String
Set(ByVal Value As String)
txtRecvdmessage.Text = Value
End Set
End Property
Then, you will be able to use:
m.MessageEntry = txtCommand.Text
Your original declaration was declaring a write-only String property which expected a single parameter of type String in addition to the value.
|
|
|
|
|
Cool...I'll give it a try when I get back to the office (attic )
Thanks...
|
|
|
|
|
Hi,
I'm trying to add a GUI effect for a form in VB.NET that will cause the window to fade to 50% transparency when the title bar is clicked on. It's easy to get the form to fade, but I'm having trouble finding a way to capture the event of clicking on the title bar. Since this is a nonclient area of the window, VB does not raise one of the built-in events for forms. If anyone knows a way for me to capture the messages for when the title bar is clicked, I would be most happy... the only way i think i can do this is by writing a control in C++ that will capture the window messages and let VB know when the title bar has been clicked... thanks in advance..
Jon
|
|
|
|
|
Try this in the form code:
Private Const WM_NCLBUTTONDOWN As Integer = &HA1
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_NCLBUTTONDOWN Then
Me.Opacity = 0.5
End If
MyBase.WndProc(m)
End Sub
|
|
|
|
|
Thanks a bunch, that worked just great.. one more question, is there a way to capture the left mouse button being released? I tried &HA2 as a code, but that is a double click (i think). Even Spy++ doesn't capture WM_NCLBUTTONUP. Thanks for the help!
|
|
|
|
|
&HA2 is the correct code, but the window never seems to get it! Spy++ seems to see it as a WM_LBUTTONUP with a negative y value, but that message never gets to the WndProc on the form.
|
|
|
|
|
Unfortunately, that's correct. I did some investigation this weekend and discovered that same problem. I think the only way to do this is to write a mouse hook to intercept the messages before Windows sends them to the form. I have a project that somebody did like this in C++ and i'm trying now to convert it to a DLL to use in my VB project.. i can post it here if I am successful, if you like.
Jon
|
|
|
|
|
Hello, I am trying to find out if anyone has a ComboBox Control for Visual Basic .NET that will handle Multi-Columns. Also, I have seen a Drop-Down TreeView Control as well on this site...has anyone Implemented this into .NET yet? I could really use both of these ASAP!
|
|
|
|
|
I've got multi-column data-bound ComboBox and ListBox controls, with support for column images and gridlines. They're very easy to do, so I don't think they're worth an article. Let me know if you want me to post the code here.
|
|
|
|
|
Hey, thanks for the help a while back! I was wondering if you could maybe send me the ".vb" file as an attachment to my email address. The formatting on this website when I copy and paste is horrendous! Greatly appreciated!
|
|
|
|
|
I'll split this code up over several messages.
First, define the Data column object:
Imports System.ComponentModel
Imports System.Drawing
<ToolboxItem(""), DesignTimeVisible(False)> _
Public Class DataColumn : Inherits Component
Protected Shared _StringFormatter As StringFormat
Shared Sub New()
_StringFormatter = New StringFormat()
With _StringFormatter
.Trimming = StringTrimming.EllipsisCharacter
.FormatFlags = StringFormatFlags.NoWrap
.LineAlignment = StringAlignment.Center
End With
End Sub
#Region "Member Vars"
Private _Image As Image
Private _DataMember As String
Private _Tag As Object
Private _Width As Integer
Private _Parent As DataColumnCollection
#End Region
#Region "Constructors"
Public Sub New()
_Image = Nothing
_Width = 75
_DataMember = String.Empty
_Tag = Nothing
End Sub
Public Sub New(ByVal Image As Image)
_Image = Image
_Width = 22
_DataMember = String.Empty
_Tag = Nothing
End Sub
Public Sub New(ByVal DataMember As String)
_Image = Nothing
_Width = 75
_DataMember = DataMember
_Tag = Nothing
End Sub
Public Sub New(ByVal Width As Integer)
_Image = Nothing
_Width = Width
_DataMember = String.Empty
_Tag = Nothing
End Sub
Public Sub New(ByVal DataMember As String, ByVal Width As Integer)
_Image = Nothing
_Width = Width
_DataMember = DataMember
_Tag = Nothing
End Sub
Public Sub New(ByVal Image As Image, _
ByVal DataMember As String, _
ByVal Width As Integer)
_Image = Image
_Width = Width
_DataMember = DataMember
_Tag = Nothing
End Sub
#End Region
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (_Image Is Nothing) Then
Try
_Image.Dispose()
Finally
_Image = Nothing
End Try
End If
End If
MyBase.Dispose(disposing)
End Sub
#Region "Properties"
<Editor("System.Windows.Forms.Design.DataMemberFieldEditor", _
"System.Drawing.Design.UITypeEditor"), _
Category("Behavior")> _
Public Overridable Property DataMember() As String
Get
Return _DataMember
End Get
Set(ByVal Value As String)
_DataMember = Value
End Set
End Property
<Category("Behavior")> _
Public Overridable Property Tag() As Object
Get
Return _Tag
End Get
Set(ByVal Value As Object)
_Tag = Value
End Set
End Property
<DefaultValue(75), Category("Appearance")> _
Public Overridable Property Width() As Integer
Get
If _Parent Is Nothing OrElse _Parent.Count <> 1 Then
Return _Width
Else
Return Math.Max(_Parent.ParentWidth, _Width)
End If
End Get
Set(ByVal Value As Integer)
_Width = Value
End Set
End Property
<Category("Appearance")> _
Public Property Image() As Image
Get
Return _Image
End Get
Set(ByVal Value As Image)
_Image = Value
End Set
End Property
Protected Friend Property Parent() As DataColumnCollection
Get
Return _Parent
End Get
Set(ByVal Value As DataColumnCollection)
_Parent = Value
End Set
End Property
#End Region
Protected Friend Overridable Sub OnDraw( _
ByVal e As System.Windows.Forms.DrawItemEventArgs, _
ByVal Bounds As RectangleF, _
ByVal DataSource As Object, _
ByVal DisplayMember As String)
If Not (_Image Is Nothing) Then
e.Graphics.DrawImage(_Image, _
Bounds.X + 3, _
Bounds.Y - 8 + Bounds.Height / 2, _
16, 16)
If Bounds.Width > 22 Then
Bounds.X += 22
Bounds.Width -= 22
Else
Return
End If
End If
Dim Text As String = pGetValue(DataSource, _
DisplayMember, _
e.Index, _
_DataMember)
If Not (Text Is Nothing OrElse Text.Length = 0) Then
Dim B As Brush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(Text, _
e.Font, _
B, _
Bounds, _
_StringFormatter)
B.Dispose()
End If
End Sub
#Region "Get Text"
Protected Function pGetValue(ByVal DataSource As Object, _
ByVal DisplayMember As String, _
ByVal Row As Integer, _
ByVal Col As String) As String
If Col Is Nothing OrElse Col.Length = 0 Then Col = DisplayMember
Try
If TypeOf DataSource Is System.Data.DataSet Then
Dim DS As System.Data.DataSet = _
DirectCast(DataSource, System.Data.DataSet)
Dim sTable As String = DisplayMember
If sTable.IndexOf(".") > -1 Then
sTable = sTable.Substring(0, sTable.IndexOf(".") - 1)
End If
Return DS.Tables(sTable).Rows(Row).Item(Col).ToString
ElseIf TypeOf DataSource Is System.Data.DataTable Then
Dim DS As System.Data.DataTable = _
DirectCast(DataSource, System.Data.DataTable)
Return DS.Rows(Row).Item(Col).ToString
ElseIf TypeOf DataSource Is System.Collections.IList Then
Dim DS As IList = DirectCast(DataSource, IList)
Return ObjectGetProperty(DS.Item(Row), Col)
ElseIf TypeOf DataSource Is System.Collections.IDictionary Then
Dim DS As IDictionary = DirectCast(DataSource, IDictionary)
Return ObjectGetProperty(DS.Item(Row), Col)
ElseIf TypeOf DataSource Is IEnumerable Then
Dim DS As IEnumerable = DirectCast(DataSource, IEnumerable)
Dim i As Integer = 0
Dim o As Object
For Each o In DS
i += 1
If i = Row Then Return ObjectGetProperty(o, Col)
Next
Return String.Empty
Else
Return ObjectGetProperty(DataSource, Col)
End If
Catch
Return String.Empty
End Try
End Function
Protected Shared Function ObjectGetProperty(ByVal o As Object, _
ByVal Name As String) As String
If o Is Nothing Then Return String.Empty
If Name Is Nothing OrElse Name.Length = 0 Then Return o.ToString
Try
Dim T As Type = o.GetType()
Dim F As Reflection.BindingFlags = _
Reflection.BindingFlags.GetField Or _
Reflection.BindingFlags.GetProperty Or _
Reflection.BindingFlags.Public Or _
Reflection.BindingFlags.Instance Or _
Reflection.BindingFlags.Static Or _
Reflection.BindingFlags.IgnoreCase
Dim oRes As Object = T.InvokeMember(Name, F, Nothing, o, New Object() {})
Return oRes.ToString
Catch
Return o.ToString
End Try
End Function
#End Region
End Class
|
|
|
|
|
Next, define a collection of DataColumn s:
Imports System.ComponentModel
Imports System.Collections
Imports System.Windows.Forms
Public Class DataColumnCollection : Inherits CollectionBase
Private _Parent As Control
#Region "Constructors"
Public Sub New()
'Do Nothing
End Sub
Public Sub New(ByVal Value() As DataColumn)
AddRange(Value)
End Sub
Public Sub New(ByVal Value As DataColumnCollection)
AddRange(Value)
End Sub
#End Region
#Region "Add"
Public Function Add(ByVal Value As DataColumn) As Integer
Value.Parent = Me
Return List.Add(Value)
End Function
Public Sub AddRange(ByVal Value() As DataColumn)
Dim oItem As DataColumn
For Each oItem In Value
oItem.Parent = Me
List.Add(oItem)
Next
End Sub
Public Sub AddRange(ByVal Value As DataColumnCollection)
Dim oItem As DataColumn
For Each oItem In Value
oItem.Parent = Me
List.Add(oItem)
Next
End Sub
#End Region
Public Function Contains(ByVal Value As DataColumn) As Boolean
Return List.Contains(Value)
End Function
Public Sub CopyTo(ByVal array() As DataColumn, ByVal index As Integer)
List.CopyTo(array, index)
End Sub
Public Overloads Function IndexOf(ByVal Value As DataColumn) As Integer
Return List.IndexOf(Value)
End Function
Public Sub Insert(ByVal index As Integer, ByVal Value As DataColumn)
List.Insert(index, Value)
End Sub
Default Public Property Item(ByVal index As Integer) As DataColumn
Get
Return DirectCast(List.Item(index), DataColumn)
End Get
Set(ByVal Value As DataColumn)
List.Item(index) = Value
End Set
End Property
Protected Overrides Sub OnInsert(ByVal index As Integer, _
ByVal Value As Object)
If TypeOf Value Is DataColumn Then
MyBase.OnInsert(index, Value)
DirectCast(Value, DataColumn).Parent = Me
Else
Throw New ArgumentException()
End If
End Sub
Protected Overrides Sub OnSet(ByVal index As Integer, _
ByVal OldValue As Object, _
ByVal NewValue As Object)
If TypeOf NewValue Is DataColumn Then
MyBase.OnSet(index, OldValue, NewValue)
DirectCast(NewValue, DataColumn).Parent = Me
Else
Throw New ArgumentException()
End If
End Sub
Protected Friend Property Parent() As Control
Get
Return _Parent
End Get
Set(ByVal Value As Control)
_Parent = Value
End Set
End Property
Public ReadOnly Property ParentWidth() As Integer
Get
If _Parent Is Nothing Then
Return 0
Else
Return _Parent.ClientSize.Width
End If
End Get
End Property
End Class
|
|
|
|
|
Next, a base for the ComboBox / ListBox:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class ComboBoxEx : Inherits ComboBox
'Or - Public Class ListBoxEx : Inherits ListBox
Protected HighlightBrush As Brush
Protected HighlightPen As Pen
#Region "Constructors"
Public Sub New()
DrawMode = DrawMode.OwnerDrawFixed
ItemHeight += 1
HighlightBrush = New SolidBrush(SystemColors.Highlight)
HighlightPen = New Pen(SystemColors.HighlightText)
End Sub
#End Region
#Region "Overrides"
Protected Overrides Sub OnDrawItem(ByVal e As DrawItemEventArgs)
If Not (e.Index = -1) Then
DrawListBoxItem(e)
End If
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
HighlightBrush.Dispose()
HighlightPen.Dispose()
End If
MyBase.Dispose(disposing)
End Sub
<Browsable(False)> _
Public Shadows Property DrawMode() As DrawMode
Get
Return MyBase.DrawMode
End Get
Set(ByVal Value As DrawMode)
MyBase.DrawMode = Value
End Set
End Property
#End Region
#Region "Implementation"
Protected Sub DrawListBoxItem(ByVal e As DrawItemEventArgs)
'
If Enabled AndAlso _
((e.State And DrawItemState.Selected) = DrawItemState.Selected) Then
DrawItemHighlight(e)
Else
DrawItemClearHighlight(e)
End If
DrawItemText(e)
End Sub
Protected Overridable Sub DrawItemText(ByVal e As DrawItemEventArgs)
Dim currentObject As Object = Items(e.Index)
Dim item As String = GetItemText(currentObject)
If Not (item Is Nothing) Then
Dim B As Brush
If Enabled Then
B = New SolidBrush(e.ForeColor)
Else
B = SystemBrushes.ControlDark
End If
e.Graphics.DrawString(item, _
e.Font, _
B, _
New PointF(e.Bounds.Left + 2, e.Bounds.Top))
If Enabled Then B.Dispose()
End If
End Sub
Protected Overridable Sub DrawItemHighlight(ByVal e As DrawItemEventArgs)
Dim g As Graphics = e.Graphics
Dim b As Rectangle = e.Bounds
g.FillRectangle(HighlightBrush, b)
g.DrawRectangle(HighlightPen, b.Left, b.Top, b.Width - 1, b.Height - 1)
End Sub
Protected Overridable Sub DrawItemClearHighlight(ByVal e As DrawItemEventArgs)
e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds)
End Sub
#End Region
End Class
|
|
|
|
|
Finally, define the List/ComboBox:
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
<DesignerCategory("Component"), _
DefaultProperty("DataSource"), _
TypeConverter("System.Windows.Forms.Design.DataSourceConverter"), _
ToolboxItem(True)> _
Public Class DataComboBoxEx : Inherits ComboBoxEx
'Or - Public Class DataListBoxEx : Inherits ListBoxEx
Private _Columns As DataColumnCollection
Private _GridLineColor As Color
Private _ShowHorizontalGridLines As Boolean
Private _ShowVerticalGridLines As Boolean
Public Sub New()
_ShowVerticalGridLines = True
_ShowHorizontalGridLines = False
_Columns = New DataColumnCollection()
_Columns.Parent = Me
_GridLineColor = SystemColors.ControlDark
ItemHeight = Math.Max(ItemHeight, 22)
End Sub
Protected Overrides Sub DrawItemText( _
ByVal e As System.Windows.Forms.DrawItemEventArgs)
Dim rItem As Rectangle = e.Bounds
Dim g As Graphics = e.Graphics
Dim gPen As New Pen(_GridLineColor)
Dim DS As Object
If DataSource Is Nothing Then
DS = Items
Else
DS = DataSource
End If
Dim rItemF As New RectangleF(rItem.X, rItem.Y, rItem.Width, rItem.Height)
Dim oItem As DataColumn
For Each oItem In _Columns
rItemF.Width = oItem.Width
oItem.OnDraw(e, rItemF, DS, DisplayMember)
If _ShowVerticalGridLines Then
g.DrawLine(gPen, rItemF.Right, rItemF.Y, rItemF.Right, rItemF.Bottom)
End If
rItemF.X += rItemF.Width
Next
If _ShowHorizontalGridLines Then
g.DrawLine(gPen, rItem.X, rItem.Bottom - 1, rItem.Right, rItem.Bottom - 1)
End If
gPen.Dispose()
End Sub
<Category("Behavior"), _
DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public Property Columns() As DataColumnCollection
Get
Return _Columns
End Get
Set(ByVal Value As DataColumnCollection)
_Columns = Value
_Columns.Parent = Me
End Set
End Property
<Category("Appearance")> _
Public Property GridLineColor() As Color
Get
Return _GridLineColor
End Get
Set(ByVal Value As Color)
_GridLineColor = Value
End Set
End Property
<Category("Appearance"), DefaultValue(False)> _
Public Property ShowHorizontalGridLines() As Boolean
Get
Return _ShowHorizontalGridLines
End Get
Set(ByVal Value As Boolean)
_ShowHorizontalGridLines = Value
End Set
End Property
<Category("Appearance"), DefaultValue(True)> _
Public Property ShowVerticalGridLines() As Boolean
Get
Return _ShowVerticalGridLines
End Get
Set(ByVal Value As Boolean)
_ShowVerticalGridLines = Value
End Set
End Property
#Region "Hide properies"
<Browsable(False)> _
Public Shadows Property DropDownStyle() As ComboBoxStyle
Get
Return ComboBoxStyle.DropDown
End Get
Set(ByVal Value As ComboBoxStyle)
MyBase.DropDownStyle = ComboBoxStyle.DropDown
End Set
End Property
<Browsable(False)> _
Public Shadows Property Sorted() As Boolean
Get
Return MyBase.Sorted
End Get
Set(ByVal Value As Boolean)
MyBase.Sorted = Value
End Set
End Property
#End Region
End Class
|
|
|
|
|
Ok, this is good. It will probably take some time for me to evaluate everything here. Will this support "unbound" data as well? Thanks a lot!
|
|
|
|
|
It should do - if you don't specify a data source, it will use the Items property, which implements IList , so it should pick up the items from that.
|
|
|
|
|