Click here to Skip to main content
15,902,938 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionRe: Type conversion in VB.NET Pin
Richard MacCutchan9-Mar-19 4:14
mveRichard MacCutchan9-Mar-19 4:14 
AnswerRe: Type conversion in VB.NET Pin
Revolution Radio9-Mar-19 5:22
Revolution Radio9-Mar-19 5:22 
GeneralRe: Type conversion in VB.NET Pin
Richard MacCutchan9-Mar-19 6:36
mveRichard MacCutchan9-Mar-19 6:36 
GeneralRe: Type conversion in VB.NET Pin
Revolution Radio11-Mar-19 0:43
Revolution Radio11-Mar-19 0:43 
QuestionQuestion for fetching the data from table Pin
Member 141728245-Mar-19 23:01
Member 141728245-Mar-19 23:01 
AnswerRe: Question for fetching the data from table Pin
CHill607-Mar-19 1:46
mveCHill607-Mar-19 1:46 
AnswerRe: Question for fetching the data from table Pin
Richard MacCutchan7-Mar-19 1:52
mveRichard MacCutchan7-Mar-19 1:52 
QuestionTrying to set caret at correct offset: CLOSED Pin
mo149228-Feb-19 7:58
mo149228-Feb-19 7:58 
This solved my problem:
Dim szF As SizeF = gr.MeasureString(Me.Text.Substring(0, iIndex), Me.Font, New SizeF(999, 999), StringFormat.GenericTypographic)


I have a control derived from UserControl in which I am trying to implement like a TextBox
and I am having trouble setting the caret in the correct text column position. Below is the
code to calculate the x offset and move the caret.

Simple test:
With the text "aBc", when I try to set the caret between 'a' and 'B' (iIndex=1), the caret is displayed
almost at the end of the 'B'.

MeasureDisplayStringWidth() is test code I found on the internet and
does not make any difference than using gr.MeasureString().

SetCaretPos() is the standared Windows API for setting the caret.

And for those who wonder why I don't just use a TextBox, this control has all kinds of custom
stuff including rotation.

I would appreciate any help.
Thank you

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim sb As SolidBrush = New SolidBrush(Color.FromArgb(255, 0, 0, 255))
' Display text
e.Graphics.DrawString(Me.Text, me.Font, sb, 0, 0)
End Sub

Private Sub MoveCaret(ByVal iIndex As Integer)
Dim gr As Graphics = Me.CreateGraphics()
' Both of these give the same result.
'Dim szF As SizeF = gr.MeasureString(Me.Text.Substring(0, iIndex), Me.Font)
Dim i As Integer = MeasureDisplayStringWidth(gr, Me.Text.Substring(0, iIndex), Me.Font)

gr.Dispose()
gr = Nothing

' Windows api call
MyUtilsLib.WindowsAPI.SetCaretPos(i, 1)
End Sub

Public Function MeasureDisplayStringWidth(ByVal gr As Graphics, ByVal text As String, ByVal fnt As Font) As Integer
If text.Length < 1 Then Return 0

Dim format As System.Drawing.StringFormat = New System.Drawing.StringFormat()
Dim rect As System.Drawing.RectangleF = New System.Drawing.RectangleF(0, 0, 1000, 1000)
Dim ranges As System.Drawing.CharacterRange() = {New System.Drawing.CharacterRange(0, text.Length)}
Dim regions As System.Drawing.Region() = {New System.Drawing.Region()}

format.SetMeasurableCharacterRanges(ranges)
regions = gr.MeasureCharacterRanges(text, fnt, rect, format)
rect = regions(0).GetBounds(gr)

Return CInt(rect.Right + 1.0F)
End Function

-- modified 28-Feb-19 15:15pm.
QuestionSystem.Data.OLEDB, my OLEDB connection stopped working in Win10 X64 Pin
jkirkerx26-Feb-19 7:47
professionaljkirkerx26-Feb-19 7:47 
AnswerTried some more things Pin
jkirkerx26-Feb-19 8:27
professionaljkirkerx26-Feb-19 8:27 
AnswerRe: System.Data.OLEDB, my OLEDB connection stopped working in Win10 X64 Pin
Maciej Los26-Feb-19 8:46
mveMaciej Los26-Feb-19 8:46 
QuestionMessage Closed Pin
11-Feb-19 1:03
manju 311-Feb-19 1:03 
AnswerRe: How can i make sure UDF Add-in to work on different machine Pin
Dave Kreskowiak11-Feb-19 3:07
mveDave Kreskowiak11-Feb-19 3:07 
GeneralRe: How can i make sure UDF Add-in to work on different machine Pin
manju 311-Feb-19 6:21
manju 311-Feb-19 6:21 
QuestionSplit cells with comma and space Pin
manju 34-Feb-19 21:34
manju 34-Feb-19 21:34 
QuestionRe: Split cells with comma and space Pin
Richard MacCutchan4-Feb-19 22:11
mveRichard MacCutchan4-Feb-19 22:11 
AnswerRe: Split cells with comma and space Pin
manju 34-Feb-19 23:47
manju 34-Feb-19 23:47 
GeneralRe: Split cells with comma and space Pin
Richard MacCutchan4-Feb-19 23:56
mveRichard MacCutchan4-Feb-19 23:56 
GeneralRe: Split cells with comma and space Pin
Richard Deeming5-Feb-19 1:17
mveRichard Deeming5-Feb-19 1:17 
AnswerRe: Split cells with comma and space Pin
Maciej Los5-Feb-19 1:41
mveMaciej Los5-Feb-19 1:41 
Questiondatagridview that was bound to dataset Pin
ClintSoft4-Feb-19 15:38
ClintSoft4-Feb-19 15:38 
QuestionLoad xlsx filr from folder and write to new workbook Pin
manju 33-Feb-19 20:25
manju 33-Feb-19 20:25 
QuestionRe: Load xlsx filr from folder and write to new workbook Pin
Richard MacCutchan3-Feb-19 22:23
mveRichard MacCutchan3-Feb-19 22:23 
AnswerRe: Load xlsx filr from folder and write to new workbook Pin
manju 34-Feb-19 21:39
manju 34-Feb-19 21:39 
GeneralRe: Load xlsx filr from folder and write to new workbook Pin
Richard MacCutchan4-Feb-19 22:00
mveRichard MacCutchan4-Feb-19 22:00 

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.