none
Add button inside DataGridViewCell when editing RRS feed

  • Question

  • Hi,

    when the user clicks on a cell to edit it, I need to show a calculator button or icon, so the user can make some operations, I have no idea where to start, but I know the logic:

    - Catch CellBeginEdit cell's event
    - Show the icon or button
    - If user clic the icon, then a new form with a calculator is shown
    - The result value is sent to the cell
    - Everyone's happy

    Any idea where to start?

    Regards !!!


    G.Waters


    Wednesday, January 30, 2019 7:06 PM

All replies

  • Hello,

    The following is how to place a button in a TextBox (I understand this is not what you want, stick with me).

    Public Class Form1
        <System.Runtime.InteropServices.DllImport("user32.dll")>
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
        End Function
        Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
    
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim btn As New Button
            AddHandler btn.Click, AddressOf MyClick
    
            btn.Size = New Size(20, TextBox1.ClientSize.Height + 2)
            btn.Location = New Point(TextBox1.ClientSize.Width - btn.Width - 1, -1)
            'btn.FlatStyle = FlatStyle.Flat
            btn.Cursor = Cursors.Default
            btn.Image = Image.FromFile("Calc.png")
            'btn.FlatAppearance.BorderSize = 0
            TextBox1.Controls.Add(btn)
            SendMessage(TextBox1.Handle, &HD3, CType(2, IntPtr), CType((btn.Width << 16), IntPtr))
        End Sub
    
        Private Sub MyClick(sender As Object, e As EventArgs)
            MessageBox.Show("Do something")
        End Sub
    End Class
    

    In the DataGridView EditorControlShowing cast the incoming object to TextBox which equates  to TextBox1, from there do what you need. I'm hunkered down in web developer else I make a code sample.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 30, 2019 9:49 PM
    Moderator
  • Okay I had a chance to try this out (my last reply) but simply picked a random column, in this case a string but this will work on numerics also.

    Code placed in EditingControlShowing. One thing I didn't count on was needing (and I should had) to clear the TextBox of former controls meaning if the control collection was not cleared each time performing these actions a new button would be added.

    Dim textbox1 = TryCast(e.Control, TextBox)
    textbox1.Controls.Clear()
    Dim btn As New Button
    AddHandler btn.Click, AddressOf MyClick
    
    btn.Size = New Size(20, textbox1.ClientSize.Height + 2)
    btn.Location = New Point(textbox1.ClientSize.Width - btn.Width - 1, -1)
    btn.FlatStyle = FlatStyle.Flat
    btn.Cursor = Cursors.Default
    btn.Image = Image.FromFile("Calc.png")
    btn.FlatAppearance.BorderSize = 0
    textbox1.Controls.Add(btn)
    SendMessage(textbox1.Handle, &HD3, CType(2, IntPtr), CType((btn.Width << 16), IntPtr))


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 30, 2019 11:46 PM
    Moderator