Click here to Skip to main content
15,914,165 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralControl array annoyances... Pin
Ray Cassick5-Aug-02 9:55
Ray Cassick5-Aug-02 9:55 
Generalhelp needed...!!! Pin
drmzunlimited5-Aug-02 7:04
drmzunlimited5-Aug-02 7:04 
GeneralStoring input to a cell in excel Pin
skydivenut4-Aug-02 16:31
skydivenut4-Aug-02 16:31 
Generalmulti text file processing Pin
ftjjk4-Aug-02 15:51
ftjjk4-Aug-02 15:51 
GeneralError when trying Update on SQLDataAdapter Pin
Knappen4-Aug-02 5:07
Knappen4-Aug-02 5:07 
Generalupdate database in vb.net Pin
devil6663-Aug-02 11:41
devil6663-Aug-02 11:41 
GeneralRe: update database in vb.net Pin
Nick Parker7-Aug-02 2:59
protectorNick Parker7-Aug-02 2:59 
GeneralHelp!! VB.NET Custom DataGridColumn Problem ... Pin
afphinfan12-Aug-02 5:42
afphinfan12-Aug-02 5:42 
I am having a problem with the datagrid. I needed a columnstyle to display a email link for each contact that is displayed in the detail portion of a master-detail form. I found an example of a combobox and modified it for to a linklabel (oddly enough it works ... this surprised me because I am completely lost with inherit, override, overload, etc. I think I'm down to two problems: I can't figure out how to handle the clicked link (this is kind of important!) and I would like to be able to change the background color of the label to match the alternating row color.

Thanks in advance for any help provided!!
Mark

Here is the code for the datagridlinklabel: (sorry for the length)

Option Strict On
Option Explicit On
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data
Public Class DataGridLinkLabel
Inherits LinkLabel
Public Sub New()
MyBase.New()
End Sub
Public isInEditOrNavigateMode As Boolean = True
End Class
Public Class DataGridLinkLabelColumnStyle
Inherits DataGridColumnStyle
'
' UI constants
'
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private dgLinkLabel As DataGridLinkLabel
'
' Used to track editing state
'
Private OldVal As String = String.Empty
Private InEdit As Boolean = False
'
' Create a new column - DisplayMember passed by string
'
Public Sub New(ByVal DisplayMember As String)

dgLinkLabel = New DataGridLinkLabel()
With dgLinkLabel
.Visible = False
.Text = DisplayMember
End With
End Sub
Public Sub New()
dgLinkLabel = New DataGridLinkLabel()
With dgLinkLabel
.Visible = False
End With
End Sub
'------------------------------------------------------
' Methods overridden from DataGridColumnStyle
'------------------------------------------------------
'
' Abort Changes
'
Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
Debug.WriteLine("Abort()")
RollBack()
HideLinkLabel()
EndEdit()
End Sub
Protected Overloads Overrides Function Commit(ByVal DataSource As CurrencyManager, _
ByVal RowNum As Integer) As Boolean
HideLinkLabel()
If Not InEdit Then
Return True
End If
Try
Dim Value As Object = dgLinkLabel.Text
If NullText.Equals(Value) Then
Value = Convert.DBNull
End If
SetColumnValueAtRow(DataSource, RowNum, Value)
Catch e As Exception
RollBack()
Return False
End Try
EndEdit()
Return True
End Function
'
' Remove focus
'
Protected Overloads Overrides Sub ConcedeFocus()
dgLinkLabel.Visible = False
End Sub
'
' Edit Grid
'
Protected Overloads Overrides Sub Edit(ByVal Source As CurrencyManager, _
ByVal Rownum As Integer, _
ByVal Bounds As Rectangle, _
ByVal [ReadOnly] As Boolean, _
ByVal InstantText As String, _
ByVal CellIsVisible As Boolean)
dgLinkLabel.Text = String.Empty
Dim OriginalBounds As Rectangle = Bounds
OldVal = dgLinkLabel.Text
If CellIsVisible Then
Bounds.Offset(xMargin, yMargin)
Bounds.Width -= xMargin * 2
Bounds.Height -= yMargin
dgLinkLabel.Bounds = Bounds
dgLinkLabel.Visible = True
Else
dgLinkLabel.Bounds = OriginalBounds
dgLinkLabel.Visible = False
End If
dgLinkLabel.Text = GetText(GetColumnValueAtRow(Source, Rownum))
If Not InstantText Is Nothing Then
dgLinkLabel.Text = InstantText
End If
dgLinkLabel.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
dgLinkLabel.Focus()
If InstantText Is Nothing Then
dgLinkLabel.Select()
Else
Dim [End] As Integer = dgLinkLabel.Text.Length
dgLinkLabel.Select()
End If
If dgLinkLabel.Visible Then
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeight() As Integer
'
' Set the minimum height to the height of the linklabel
'
Return dgLinkLabel.PreferredHeight + yMargin
End Function
Protected Overloads Overrides Function GetPreferredHeight(ByVal g As Graphics, _
ByVal Value As Object) As Integer
Debug.WriteLine("GetPreferredHeight()")
Dim NewLineIndex As Integer = 0
Dim NewLines As Integer = 0
Dim ValueString As String = Me.GetText(Value)
Do
While NewLineIndex <> -1
NewLineIndex = ValueString.IndexOf("r\n", NewLineIndex + 1)
NewLines += 1
End While
Loop
Return FontHeight * NewLines + yMargin
End Function
Protected Overloads Overrides Function GetPreferredSize(ByVal g As Graphics, _
ByVal Value As Object) As Size
Dim Extents As Size = Size.Ceiling(g.MeasureString(GetText(Value), _
Me.DataGridTableStyle.DataGrid.Font))
Extents.Width += xMargin * 2 + DataGridTableGridLineWidth
Extents.Height += yMargin
Return Extents
End Function
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer)
Paint(g, Bounds, Source, RowNum, False)
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, _
ByVal AlignToRight As Boolean)
Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, _
ByVal BackBrush As Brush, _
ByVal ForeBrush As Brush, _
ByVal AlignToRight As Boolean)
Dim Text As String = GetText(GetColumnValueAtRow(Source, RowNum))
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Value As DataGrid)
MyBase.SetDataGridInColumn(Value)
If Not (dgLinkLabel.Parent Is Value) Then
If Not (dgLinkLabel.Parent Is Nothing) Then
dgLinkLabel.Parent.Controls.Remove(dgLinkLabel)
End If
End If
If Not (Value Is Nothing) Then Value.Controls.Add(dgLinkLabel)
End Sub
Protected Overloads Overrides Sub UpdateUI(ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, ByVal InstantText As String)
dgLinkLabel.Text = GetText(GetColumnValueAtRow(Source, RowNum))
If Not (InstantText Is Nothing) Then
dgLinkLabel.Text = InstantText
End If
End Sub

'----------------------------------------------------------------------
' Helper Methods
'----------------------------------------------------------------------
Private ReadOnly Property DataGridTableGridLineWidth() As Integer
Get
If Me.DataGridTableStyle.GridLineStyle = DataGridLineStyle.Solid Then
Return 1
Else
Return 0
End If
End Get
End Property
Private Sub EndEdit()
InEdit = False
Invalidate()
End Sub
Private Function GetText(ByVal Value As Object) As String
If Value Is System.DBNull.Value Then Return NullText

If Not Value Is Nothing Then
Return Value.ToString
Else
Return String.Empty
End If
End Function
Private Sub HideLinkLabel()
If dgLinkLabel.Focused Then
Me.DataGridTableStyle.DataGrid.Focus()
End If
dgLinkLabel.Visible = False
End Sub
Private Sub RollBack()
dgLinkLabel.Text = OldVal
End Sub
Private Sub PaintText(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Text As String, _
ByVal AlignToRight As Boolean)
Dim BackBrush As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub
Private Sub PaintText(ByVal g As Graphics, _
ByVal TextBounds As Rectangle, _
ByVal Text As String, _
ByVal BackBrush As Brush, _
ByVal ForeBrush As Brush, _
ByVal AlignToRight As Boolean)
Dim Rect As Rectangle = TextBounds
Dim RectF As RectangleF = RectF.op_Implicit(Rect) ' Convert to RectangleF
Dim Format As StringFormat = New StringFormat()
If AlignToRight Then
Format.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If
Select Case Me.Alignment
Case Is = HorizontalAlignment.Left
Format.Alignment = StringAlignment.Near
Case Is = HorizontalAlignment.Right
Format.Alignment = StringAlignment.Far
Case Is = HorizontalAlignment.Center
Format.Alignment = StringAlignment.Center
End Select
Format.FormatFlags = Format.FormatFlags Or StringFormatFlags.NoWrap
g.FillRectangle(Brush:=BackBrush, Rect:=Rect)
Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Text, Me.DataGridTableStyle.DataGrid.Font, ForeBrush, RectF, Format)
Format.Dispose()
End Sub
End Class
GeneralAcess Form Controls Pin
Bruno Lemos2-Aug-02 0:48
Bruno Lemos2-Aug-02 0:48 
GeneralWeb Services Implementation Guide Pin
Stanford Powers31-Jul-02 18:33
sussStanford Powers31-Jul-02 18:33 
GeneralRe: Web Services Implementation Guide Pin
Jason McBurney1-Aug-02 11:25
Jason McBurney1-Aug-02 11:25 
GeneralMDI QueryUnloads Pin
Jason McBurney31-Jul-02 5:07
Jason McBurney31-Jul-02 5:07 
GeneralRe: MDI QueryUnloads Pin
Tom Welch31-Jul-02 5:14
Tom Welch31-Jul-02 5:14 
GeneralRe: MDI QueryUnloads Pin
Colin Leitner31-Jul-02 5:17
Colin Leitner31-Jul-02 5:17 
GeneralRe: MDI QueryUnloads Pin
Roger Wright31-Jul-02 5:26
professionalRoger Wright31-Jul-02 5:26 
GeneralRe: MDI QueryUnloads Pin
Nick Parker31-Jul-02 5:30
protectorNick Parker31-Jul-02 5:30 
GeneralRe: MDI QueryUnloads Pin
Jason McBurney31-Jul-02 11:22
Jason McBurney31-Jul-02 11:22 
Generalpath help needed !! Pin
drmzunlimited31-Jul-02 2:43
drmzunlimited31-Jul-02 2:43 
GeneralRe: path help needed !! Pin
chris foote1-Aug-02 10:32
chris foote1-Aug-02 10:32 
Generalpath help needed !! Pin
drmzunlimited31-Jul-02 2:42
drmzunlimited31-Jul-02 2:42 
GeneralMSComm with DTREnable Pin
Anonymous30-Jul-02 23:09
Anonymous30-Jul-02 23:09 
GeneralTry Catch Finally and variable declaration Pin
Zyxil30-Jul-02 10:26
Zyxil30-Jul-02 10:26 
GeneralRe: GotDotNet won the race Pin
Zyxil30-Jul-02 10:31
Zyxil30-Jul-02 10:31 
GeneralDebug a VC++ DLL from VBA Pin
Ryan B.30-Jul-02 10:13
Ryan B.30-Jul-02 10:13 
GeneralRe: Debug a VC++ DLL from VBA Pin
Vi230-Jul-02 19:57
Vi230-Jul-02 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.