form.showdialog problem

i can get the OK message return when i press the OK Button in form frmChooseItems,

also get the CANCEL message return when i press ESC

If the focus on the control datagridview on frmChooseItems, when i press the enter,

i cannot get the message "OK" return.

I want to know how to :

when i press the "Enter" on the datagridview on frmChooseItems, i can get the result sames as i press the OK Button.

frmPI CODE

Dim frmChooseItems As New frmChooseItems

If frmChooseItems.ShowDialog = Windows.Forms.DialogResult.OK Then
MsgBox("OK")
Else
MsgBox("CANCEL")
End If
frmChooseItems.Dispose()

frmChooseItems CODE

Private Sub frmChooseItems_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
dsChoose.dtItems.Clear()
adChooseItems.Fill(DsChoose)

Me.dgvChooseItem.DataSource = DsChoose.Tables("dtItems")

End Sub

Private Sub dgvChooseItem_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgvChooseItem.KeyPress

Select Case e.KeyChar
Case Chr(13)
Msgbox ("Enter")

End Select

End Sub



Answer this question

form.showdialog problem

  • D Hoffman

    I dont know why i also cannot get the result what i want.

    When i press the ENTER on my DGV, it cannot pass the result to the other form, and close itself.

    But when i press the btnOK by mouse, i can get the result what i want.

    I have add the msgbox inside the keypress function, i am sure the program can capture the enter,

    but have do anything, do you know why

    Here is my code 2 form code

    Public Class frmChooseItems

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvChooseItem.CellContentClick

    End Sub

    Private Sub frmChooseItems_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Enter

    End Sub

    Private Sub frmChooseItems_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
    If cnAlfol.State <> ConnectionState.Open Then
    cnAlfol.ConnectionString = cnStr
    cnAlfol.Open()
    End If

    DsChoose.dtItems.Clear()
    adChooseItems.Fill(DsChoose)

    Me.dgvChooseItem.DataSource = DsChoose.Tables("dtItems")

    Catch ex As Exception
    MsgBox(ex.Message)
    Finally
    cnAlfol.Close()
    End Try


    End Sub

    Private Sub dgvChooseItem_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles dgvChooseItem.KeyPress
    If e.KeyChar = Convert.ToChar(13) Then

    'any of the next 2 lines works and raise button1.click event

    'Button1.PerformClick()

    MsgBox("ENTER")

    btnOK_Click(Nothing, Nothing)
    End If
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

    End Sub
    End Class

    The other form

    Dim frmChooseItems As New frmChooseItems

    If frmChooseItems.ShowDialog = Windows.Forms.DialogResult.OK Then
    With Me.dgvPUINVD.Rows(y)
    .Cells("dgvtxtItemID").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "ItemID")
    .Cells("dgvtxtBarcode").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "Barcode")
    .Cells("dgvtxtShortName").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "ShortName")
    .Cells("dgvtxtLongName").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "LongName")
    .Cells("dgvtxtUnitPrice").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "UnitPrice")
    .Cells("dgvtxtCategoryName").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "CategoryName")
    .Cells("dgvtxtCategoryDesc").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "CategoryDesc")
    .Cells("dgvtxtBrandName").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "BrandName")
    .Cells("dgvtxtBrandDesc").Value = getDGVCellValueStr(frmChooseItems.dgvChooseItem, "BrandDesc")
    .Cells("dgvtxtQty").Value = 1
    .Cells("dgvtxtDiscount").Value = 0
    End With
    End If
    frmChooseItems.Dispose()


  • jlechem

     

    But i add

            If e.KeyCode = Keys.Enter Then
                Call btnOK_Click(sender, e)
            End If

    in 'Do Something

     

    when i press the enter key on the form, i also cannot get the same result as i press the OK Button

    (close myself and return the value to the parent form)

    WHY

     

     


  • Ralph11234

    I have set it already,

    therefore when i press the btnOK, i can get what i want,

    but i cannot get what i want through this calling method

    If e.KeyCode = Keys.Enter Then
    Call btnOK_Click(sender, e)
    End If

    WHY

    I think is the if statement cannot capture the enter key, it is because i add the msgbox inside the if statement,

    i cannot get the msgbox popup when i press "ENTER" on the DGV.

    Can you help me


  • JohnEck

    NO body can help me


  • SS12

    hi,

    first of all your program doesn't know anything except what you wrote so if you didn't wrote it in a way can be executed then your program will do nothing

    second your datagridview in your second form will not be able to see the other datagridview in the other form you have to pass data between forms

    third you asked about keypress and its different when you deal with the datagridview

    fourth nothing better than a good book or a decent tutorial , the absolute beginner vedio series has a good tutorial about this , in the "RSS feed" there is a good example about how to pass a dataset between forms useing properties, and you can bound both of your datagridview to the same dataset or the same datatable , i believe this is in "Channel form " part in the tutorial

    http://msdn.microsoft.com/vstudio/express/vb/learning/default.aspx

    hope this helps



  • jjrdk

    hi,

    sorry its my mistake it suppose to be performClick


    Public Class Form1
    Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If e.KeyChar = Convert.ToChar(13) Then

    'any of the next 2 lines works and raise button1.click event

    'Button1.PerformClick()

    Button1_Click(Nothing, Nothing)
    End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show(
    "this buton was clicked")
    End Sub
    End
    Class


    hope this helps



  • whitey cox

    hi, in your form that you deal with it as dialog , select the form go to properties and set AcceptButtons = okButton

    and see if this gonna work or not

    hope this helps



  • Julien74

    Private Sub DataGridView1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyUp
    If e.KeyCode = Keys.Return Then
    'Do something
    End If
    End
    Sub
    Hope this helps you :)


  • RSNovice

    When i type btnOK.

    i cannot found the method click

    can you give me the example


  • bob_monkhouse_book_of_gags

    hi,

    because you call the method doesn't have the same signature call it like this

    Call btnOK_Click(nothing, nothing)

    or call button class to click

    btnOk.Click

    hope this helps



  • form.showdialog problem