Mouse Move Handle on a ListBox - visual-studio-2010

I have a ListBox on which I want to handle the mousemove event; And for that reason I'm using the following code
Private Sub AreaLB_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles AreaLB.MouseMove
Dim ListMousePosition As Point = AreaLB.PointToClient(Me.MousePosition)
Dim itemIndex As Integer = AreaLB.IndexFromPoint(ListMousePosition)
Dim AreaToolTip As ToolTip = ToolTip1
Dim myLB As ListBox = AreaLB
AreaToolTip.Active = True
Dim g As Graphics = AreaLB.CreateGraphics()
If itemIndex > -1 Then
Dim s As String = myLB.Items(itemIndex)
If g.MeasureString(s, myLB.Font).Width > myLB.ClientRectangle.Width Then
AreaToolTip.SetToolTip(myLB, s)
Else
AreaToolTip.SetToolTip(myLB, "")
End If
g.Dispose()
End If
End Sub
My problem is... When I'm not moving the mouse this procedure runs always when the
g.MeasureString(s, myLB.Font).Width > myLB.ClientRectangle.Width
Why that happens and how can I avoid it.

What you can do is only set the ToolTip if it isn't already the value you want it to be:
Private Sub AreaLB_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles AreaLB.MouseMove
ToolTip1.Active = True
Dim itemIndex As Integer = AreaLB.IndexFromPoint(e.X, e.Y)
If itemIndex > -1 Then
Using g As Graphics = AreaLB.CreateGraphics()
Dim s As String = AreaLB.Items(itemIndex)
If g.MeasureString(s, AreaLB.Font).Width > AreaLB.ClientRectangle.Width Then
If ToolTip1.GetToolTip(AreaLB) <> s Then
ToolTip1.Show(s, AreaLB)
End If
Else
ToolTip1.Show("", AreaLB)
End If
End Using
End If
End Sub

Related

Drag and drop rows between two datatable using msflexgrid

I have two forms with msflexgrid to display data with datasource from datatable.
I want to drag and drop rows between two form each other. I saw this topic and edited but it doesn't work.
Drag data from DG and other controls to another DG in vb.net
This error:
Please help me!
This is my form1
form1
Code form 1:
Imports C1.Win.C1FlexGrid
Public Class frm1
Private mdt As New DataTable("Test")
Private downHitInfo As C1.Win.C1FlexGrid.HitTestInfo = Nothing
Private Sub frm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mdt.Columns.Add("EmplCode")
mdt.Columns.Add("EmplName")
mdt.Rows.Add("012345", "A")
mdt.Rows.Add("012346", "B")
mdt.Rows.Add("012347", "C")
flg1.DataSource = mdt
With flg1
.DragMode = DragModeEnum.Manual
.DropMode = DropModeEnum.Manual
End With
End Sub
Private Sub flg1_MouseDown(sender As Object, e As MouseEventArgs) Handles flg1.MouseDown
Dim view As C1FlexGrid = CType(sender, C1FlexGrid)
Dim hitInfo As C1.Win.C1FlexGrid.HitTestInfo = view.HitTest(e.X, e.Y)
If Not Control.ModifierKeys = Keys.None Then
Exit Sub
End If
If e.Button = MouseButtons.Left Then
downHitInfo = hitInfo
End If
End Sub
Private Sub flg1_MouseMove(sender As Object, e As MouseEventArgs) Handles flg1.MouseMove
Dim view As C1FlexGrid = CType(sender, C1FlexGrid)
If e.Button = MouseButtons.Left And Not downHitInfo Is Nothing Then
Dim dragSize As Size = SystemInformation.DragSize
Dim DragRect As Rectangle = New Rectangle(New Point(Convert.ToInt32(downHitInfo.X - dragSize.Width / 2), _
Convert.ToInt32(downHitInfo.Y - dragSize.Height / 2)), dragSize)
If Not DragRect.Contains(New Point(e.X, e.Y)) Then
'Extract the DataRow
Dim gridRowView As C1.Win.C1FlexGrid.Row = DirectCast(view.Rows(downHitInfo.Row), C1.Win.C1FlexGrid.Row)
'Dim rowView As DataRowView = DirectCast(gridRowView.DataBoundItem, DataRowView)
Dim rowView As DataRowView = DirectCast(gridRowView.DataMap, DataRowView)
'Raise the DragDrop with the extracted DataRow
view.DoDragDrop(rowView.Row, DragDropEffects.Move)
downHitInfo = Nothing
End If
End If
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Dim lfrm As New frm2()
lfrm.Show()
End Sub
End Class
This is form 2:
Form 2
Code of form 2:
Imports C1.Win.C1FlexGrid
Public Class frm2
Private Sub frm2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With flg2
.DragMode = DragModeEnum.Manual
.DropMode = DropModeEnum.Manual
End With
End Sub
Private Sub flg2_DragOver(sender As Object, e As DragEventArgs) Handles flg2.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub flg2_DragDrop(sender As Object, e As DragEventArgs) Handles flg2.DragDrop
Dim draggedRow As DataRow = CType(e.Data.GetData(GetType(DataRow)), DataRow)
End Sub
End Class

How to display selected features in ArcGIS Identify Dialog using Visual Studio 2010/ArcObjects?

I'm brand new to ArcObjects SDKs and am struggling. I have a Python Add-in performing a query to select records (works great)and now trying to call the identify dialog via an .NET addin button that displays the identify dialog box to show attributes of the selected records. Below is the code I have at this point. I currently have the identify dialog displaying, but no records appearing. I know I need to input the selected records somewhere....but not sure where. Any thoughts would be appreciated. (I'm using Visual Studio/Microsoft Visual Basic 2010 and ArcGIS 10.2.1)
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.Carto
Public Class Identify_Button
Inherits ESRI.ArcGIS.Desktop.AddIns.Button
Dim pMxDoc As IMxDocument
Dim activeView As IMap
Public Sub DoIdentify(ByVal activeView As ESRI.ArcGIS.Carto.IActiveView, ByVal x As System.Int32, ByVal y As System.Int32)
pMxDoc = My.ArcMap.Application.Document
activeView = pMxDoc.FocusMap
If activeView Is Nothing Then
Return
End If
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass
identifyDialog.Map = map
'Clear the dialog on each mouse click
identifyDialog.ClearLayers()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay ' Implicit Cast
identifyDialog.Display = display
Dim identifyDialogProps As ESRI.ArcGIS.CartoUI.IIdentifyDialogProps = CType(identifyDialog, ESRI.ArcGIS.CartoUI.IIdentifyDialogProps) ' Explicit Cast
Dim enumLayer As ESRI.ArcGIS.Carto.IEnumLayer = identifyDialogProps.Layers
enumLayer.Reset()
Dim layer As ESRI.ArcGIS.Carto.ILayer = enumLayer.Next
Do While Not (layer Is Nothing)
identifyDialog.AddLayerIdentifyPoint(layer, x, y)
layer = enumLayer.Next()
Loop
identifyDialog.Show()
End Sub
Public Sub New()
End Sub
Protected Overrides Sub OnClick()
DoIdentify(activeView, 300, 100)
End Sub
Protected Overrides Sub OnUpdate()
Enabled = My.ArcMap.Application IsNot Nothing
End Sub
End Class
Give the code below a try. This is executed from the OnClick event from an ArcMap command button that Inherits from BaseCommand. It displays the selected features in the map in the identifyDialog just as you were needing it to except I used AddLayerIdentifyOID() instead of AddLayerIdentifyPoint() although both should work.
Public Overrides Sub OnClick()
'VBTest.OnClick implementation
Try
Dim mxDoc As IMxDocument = m_application.Document
Dim map As ESRI.ArcGIS.Carto.IMap = mxDoc.FocusMap
Dim layer As ESRI.ArcGIS.Carto.ILayer
Dim flayer As ESRI.ArcGIS.Carto.IFeatureLayer
Dim featSelection As ESRI.ArcGIS.Carto.MapSelection = map.FeatureSelection
Dim feat As ESRI.ArcGIS.Geodatabase.IFeature = featSelection.Next()
Dim count As Int16 = 0
While feat IsNot Nothing
count += 1 ' flag for clearing layers
flayer = New ESRI.ArcGIS.Carto.FeatureLayer()
flayer.FeatureClass = feat.Class
layer = flayer
DoIdentify(layer, feat.OID, (count = 1))
feat = featSelection.Next()
End While
Catch ex As Exception
' handle error
MsgBox("Error: " + ex.Message)
End Try
End Sub
Private Sub DoIdentify(ByVal layer As ESRI.ArcGIS.Carto.ILayer, ByVal OID As Int32, Optional ByVal clearLayers As Boolean = True)
If layer Is Nothing Or OID <= 0 Then
Return
End If
Dim pMxDoc As IMxDocument = m_application.Document
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = pMxDoc.ActiveView
Dim map As ESRI.ArcGIS.Carto.IMap = activeView.FocusMap
Dim identifyDialog As ESRI.ArcGIS.CartoUI.IIdentifyDialog = New ESRI.ArcGIS.CartoUI.IdentifyDialogClass()
Dim screenDisplay As ESRI.ArcGIS.Display.IScreenDisplay = activeView.ScreenDisplay
Dim display As ESRI.ArcGIS.Display.IDisplay = screenDisplay
identifyDialog.Map = map ' REQUIRED
identifyDialog.Display = display ' REQUIRED
' Clear the dialog
If clearLayers Then
identifyDialog.ClearLayers()
End If
' Add our selected feature to the dialog
identifyDialog.AddLayerIdentifyOID(layer, OID)
' Show the dialog
identifyDialog.Show()
End Sub

How to change the location of a picture box in VB 2010 using the arrow or wasd keys?

I only have access to the internet at school, so the filters get in the way of any real research. I'm currently coding an rpg for a school project but it's difficult to get the avatar to move on a map. Here's my pathetic code so far:
Public Class Map1
Private Sub USER_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
User.Top = User.Top - 1
End Sub
Private Sub USER_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
User.Top = User.Bottom + 1
'User.Location.X = 200
End Sub
End Class
I have the following problems with it:
User.location.x = 200 had syntax errors when I deleted the x and when I didn't.
The player also had to continually press the keys to move.
both I do not know how to correct.
Any help at all is greatly appreciated as it's for my final grade.
You can put it in a timer_tick to loop over it, that is what I do.
And the correct version of User.Location.X = 200 is:
User.location = new point(200, User.location.y)
nvm found the solution.
Here it is for anyone else how might need the code in the future.
Private Sub Map_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
User.Location = New Point(User.Location.X, User.Location.Y - 5)
ElseIf e.KeyCode = Keys.Down Then
User.Location = New Point(User.Location.X, User.Location.Y + 5)
ElseIf e.KeyCode = Keys.Left Then
User.Location = New Point(User.Location.X - 5, User.Location.Y)
ElseIf e.KeyCode = Keys.Right Then
User.Location = New Point(User.Location.X + 5, User.Location.Y)
End If
Module
Public Sub MovePictureBox(ByRef PictureBox As PictureBox)
Tiempo.Tag = PictureBox
AddHandler PictureBox.Parent.KeyDown, AddressOf Parent_KeyDown
AddHandler PictureBox.Parent.KeyUp, AddressOf Parent_KeyUp
AddHandler CType(PictureBox.Parent, Form).Load, AddressOf Parent_Load
End Sub
Private Up, Down, Left, Right As Boolean
WithEvents Tiempo As New Timer() With {.Interval = 1}
Private Sub Tiempo_Tick() Handles Tiempo.Tick
If Up Then Tiempo.Tag.Top -= 2
If Down Then Tiempo.Tag.Top += 2
If Left Then Tiempo.Tag.Left -= 2
If Right Then Tiempo.Tag.Left += 2
End Sub
Private Sub Parent_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Up Then Up = True
If e.KeyCode = Keys.Down Then Down = True
If e.KeyCode = Keys.Left Then Left = True
If e.KeyCode = Keys.Right Then Right = True
Tiempo.Enabled = True
End Sub
Private Sub Parent_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Tiempo.Enabled = False
Up = False : Down = False : Right = False : Left = False
End Sub
Private Sub Parent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
sender.KeyPreview = True
End Sub
End Module

Auto complete text box in excel VBA

I am creating a excel sheet that would autocomplete a text based on the text present in a particular column. After trying to make one myself unsuccessfully, I was looking online for sample codes that I could modify and incorporate in my program. (and not plagiarize)
I downloaded Workbook1.xls from http://www.ozgrid.com/forum/showthread.php?t=144438
The code is
Option Explicit
Dim ufEventsDisabled As Boolean
Dim autoCompleteEnabled As Boolean
Dim oRange As Range
Private Sub TextBox1_Change()
If ufEventsDisabled Then Exit Sub
If autoCompleteEnabled Then Call myAutoComplete(TextBox1)
End Sub
Sub myAutoComplete(aTextBox As MSForms.TextBox)
Dim RestOfCompletion As String
On Error GoTo Halt
With aTextBox
If .SelStart + .SelLength = Len(.Text) Then
RestOfCompletion = Mid(oRange.Cells(1, 1).AutoComplete(.Text), Len(.Text) + 1)
ufEventsDisabled = True
.Text = .Text & RestOfCompletion
.SelStart = Len(.Text) - Len(RestOfCompletion)
.SelLength = Len(RestOfCompletion)
End If
End With
Halt:
ufEventsDisabled = False
On Error GoTo 0
End Sub
Private Sub TextBox1_AfterUpdate()
Dim strCompleted As String
With TextBox1
strCompleted = oRange.AutoComplete(.Text)
If LCase(strCompleted) = LCase(.Text) Then
ufEventsDisabled = True
.Text = strCompleted
ufEventsDisabled = False
End If
End With
End Sub
Private Sub TextBox1_Enter()
Set oRange = ThisWorkbook.Sheets("Sheet1").Range("f4")
End Sub
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
autoCompleteEnabled = KeyCode <> vbKeyBack
autoCompleteEnabled = ((vbKey0 <= KeyCode) And (KeyCode <= vbKeyZ))
End Sub
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub UserForm_Click()
End Sub
If you'd notice the line RestOfCompletion = Mid(oRange.Cells(1, 1).AutoComplete(.Text), Len(.Text) + 1), I was wondering what AutoComplete is doing here. Its not a in built function and is not defined anywhere. Still the code runs fine. I am very curious.
Thanks
The .AutoComplete is a function of the Range object - it is based on passing the text to a range that exists elsewhere on the sheet.
You can see the documentation on this function here:
http://msdn.microsoft.com/en-us/library/bb209667(v=office.12).aspx
The myAutoComplete function handles the finding of the autocomplete data against the range if it exists, and the other pieces in the code are for highlighting the correct piece of text.

Creating a strip of selectable letters for a form

I wish to display a list of letters from a through z on a form.
Each letter needs to be clickable with that value being passed as a click argument.
Aside from creating 26 letters and using the click event of each letter does anyone know of a quick way to do this?
I know how to load dynamic controls etc and how to do it that way. Just wondering if anyone knew of a clever way to do this?
Cheers
You can use a FlowLayoutPanel and a loop like this:
private void button1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight;
flowLayoutPanel1.AutoSize = true;
flowLayoutPanel1.WrapContents = false; //or true, whichever you like
flowLayoutPanel1.Controls.Clear();
for (char c = 'A'; c <= 'Z'; c++)
{
Label letter = new Label();
letter.Text = c.ToString();
letter.AutoSize = true;
letter.Click += new EventHandler(letter_Click);
flowLayoutPanel1.Controls.Add(letter);
}
}
private void letter_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked on " + ((Label)sender).Text);
}
This is the "dynamic way" I would do it in. I know you asked for other clever ways to do it in but I think this is the most accepted way to do it.
This will produce those buttons and add a click-handler that takes the button as sender. It will also see to that the buttons location wraps if outside of the forms width.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ButtonSize As New Size(20, 20)
Dim ButtonLocation As New Point(10, 20)
For p As Integer = Asc("A") To Asc("Z")
Dim newButton As New Button
If ButtonLocation.X + ButtonSize.Width > Me.Width Then
ButtonLocation.X = 10
ButtonLocation.Y += ButtonSize.Height
End If
newButton.Size = ButtonSize
newButton.Location = ButtonLocation
newButton.Text = Chr(p)
ButtonLocation.X += newButton.Width + 5
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)
Next
End Sub
Sub ButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, Button).Text)
End Sub
End Class
alt text http://img235.imageshack.us/img235/2267/testoa6.jpg
Draw a string on a control, then match mouse clicks to the character positions on the form. It's actually easier than it sounds (this is adapted from standard documentation on MeasureCharacterRanges, which simplifies the entrire task). The example is drawn on a form, it would be simple enough to make this into a user control.
In this example, clicking on a letter will cause a messagebox to appear, telling you which letter you just clicked.
Hope it helps.
P.S. Please forgive "magic numbers" e.g. "knowning" there'll be 25 items in the array, this is just a sample after all :)
Public Class Form1
Const LETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private letterRects(25) As System.Drawing.RectangleF
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim index As Integer = -1
Dim mouseP As Point = Me.PointToClient(MousePosition)
For i As Integer = 0 To 25
If letterRects(i).Contains(mouseP.X, mouseP.Y) Then
index = i
Exit For
End If
Next
If index >= 0 Then
MessageBox.Show("Letter = " + LETTERS(index).ToString())
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' Set up string.
Dim stringFont As New Font("Times New Roman", 16.0F)
' Set character ranges
Dim characterRanges(26) As CharacterRange
For i As Integer = 0 To 25
characterRanges(i) = New CharacterRange(i, 1)
Next
' Create rectangle for layout, measurements below are not exact, these are "magic numbers"
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim width As Single = 400.0F
Dim height As Single = 40.0F
Dim layoutRect As New RectangleF(x, y, width, height)
' Set string format.
Dim stringFormat As New StringFormat
stringFormat.FormatFlags = StringFormatFlags.FitBlackBox
stringFormat.SetMeasurableCharacterRanges(characterRanges)
' Draw string to screen.
e.Graphics.DrawString(letters, stringFont, Brushes.Black, _
x, y, stringFormat)
Dim stringRegions() As [Region]
' Measure two ranges in string.
stringRegions = e.Graphics.MeasureCharacterRanges(letters, _
stringFont, layoutRect, stringFormat)
For i As Integer = 0 To 25
letterRects(i) = stringRegions(i).GetBounds(e.Graphics)
Next
End Sub
End Class

Resources