Force UpperCase in DataGrid

  • Thread starter Thread starter OHM
  • Start date Start date
O

OHM

Hi Guys,

I wouldnt normally ask for help on this but Im really strapped for time.
Does anyone know how I can force characters in a DataGrid cell to uppercase
as they are typed.

Cheers - OHM
 
Hi,

Make a new datagridtextboxcolumn. In the edit event set the
charactercasing for the textbox. Here is a sample.


Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String = Me.GetColumnValueAtRow([source],
rowNum).ToString()
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class

Ken
 
Hi OHM,

Just to give you the idea, it is not acting completly well (but that has to
do with the standard behaviour of the textboxchange event), I think when you
see this, I am sure that you can do the rest yourself. I used the
"propercase" because that does me always remind on you.

The begin is just to build the table

I hope it helps?

Cor
\\\

Private WithEvents dtbCol1 As DataGridTextBox
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable("Cor")
Dim dc As New DataColumn("OHM")
dt.Columns.Add(dc)
For i As Integer = 0 To 11
dt.Rows.Add(dt.NewRow)
dt.Rows(i)(0) = ""
Next
'
Dim ts As New DataGridTableStyle
ts.MappingName = dt.TableName
Dim column As New DataGridTextBoxColumn
ts.GridColumnStyles.Add(column)
DataGrid1.TableStyles.Add(ts)
column = CType(ts.GridColumnStyles(0), DataGridTextBoxColumn)
dtbCol1 = DirectCast(column.TextBox, DataGridTextBox)
column.MappingName = dt.Columns(0).ColumnName
column.HeaderText = "OHM"
DataGrid1.DataSource = dt
End Sub
Private Sub dtbCol1_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles dtbCol1.TextChanged
DirectCast(sender, TextBox).Text = StrConv(DirectCast(sender, _
TextBox).Text, vbProperCase)
End Sub
///
 
Many thanks,

I would have gone down this route eventually, but I thought there might be a
simple solution.

Best regards - OHM
 
Hi,

maybe this helps, place a textbox on a form and copy paste this code.
Run the project and type something in the textbox to see the result.

HTH
gr Peter

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
TextBox1.Text = Strings.UCase(TextBox1.Text)
TextBox1.SelectionLength = 0
TextBox1.SelectionStart = TextBox1.Text.Length
End Sub
 
I'd like to apologize for my post, which was just weak SH*T, I don't
know what I was thinking, missing out on the
textbox.CharacterCasing = CharacterCasing.Upper
which I've used before myself, but I have to admit it was a long day
at work yesterday :-)

Gr Peter

Ken Tucker said:
Hi,

Make a new datagridtextboxcolumn. In the edit event set the
charactercasing for the textbox. Here is a sample.


Public Class HeaderAndDataAlignColumn
Inherits DataGridTextBoxColumn

Private mTxtAlign As HorizontalAlignment = HorizontalAlignment.Left
Private mDrawTxt As New StringFormat

Protected Overloads Overrides Sub Edit(ByVal source As
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText
As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], instantText,
cellIsVisible)
MyBase.TextBox.TextAlign = mTxtAlign
MyBase.TextBox.CharacterCasing = CharacterCasing.Upper
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal
source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer,
ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As
System.Drawing.Brush, ByVal alignToRight As Boolean)
'clear the cell
g.FillRectangle(backBrush, bounds)

'draw the value
Dim s As String = Me.GetColumnValueAtRow([source],
rowNum).ToString()
Dim r As Rectangle = bounds
r.Inflate(0, -1)
g.DrawString(s, MyBase.TextBox.Font, foreBrush,
RectangleF.op_Implicit(r), _
mDrawTxt)
End Sub

Public Property DataAlignment() As HorizontalAlignment
Get
Return mTxtAlign
End Get
Set(ByVal Value As HorizontalAlignment)
mTxtAlign = Value
If mTxtAlign = HorizontalAlignment.Center Then
mDrawTxt.Alignment = StringAlignment.Center
ElseIf mTxtAlign = HorizontalAlignment.Right Then
mDrawTxt.Alignment = StringAlignment.Far
Else
mDrawTxt.Alignment = StringAlignment.Near
End If
End Set
End Property

End Class

Ken
--------------------
OHM said:
Hi Guys,

I wouldnt normally ask for help on this but Im really strapped for time.
Does anyone know how I can force characters in a DataGrid cell to
uppercase
as they are typed.

Cheers - OHM
 
Back
Top