locked
Problem............Not sure about its nature. RRS feed

  • Question

  •  

    I am trying to read line by line from text file and paste it in html forms, with seperated tabs. And it should also work for office documents, rich text and notepad. Here is the code of it. But i can't able to do it without the messageboxes inserted in and out of while loop of below code. Can you guys tell me what is the exact problem here as i tried with auto closed forms but it didn't worked.

     

    I also tried it with tab character 'Chr(9)' but it donot work in text fields of html form for others its fine.

     

     

    If (objDir.Extension = ".txt") Then

    Dim LineRead As String

    If System.IO.File.Exists(path) = True Then

    Dim objReader As New System.IO.StreamReader(path)

    LineRead = objReader.ReadLine()

    Clipboard.SetDataObject(LineRead, False)

    SetActiveWindow(8975651603260375040)

    keybd_event(VK_CONTROL, 0, 0, 0)

    keybd_event(VK_V, 0, 0, 0)

    keybd_event(VK_V, 0, KEYEVENTF_KEYUP, 0)

    keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)

    ' Me.Invalidate()

    TempForm.ShowDialog()

    '

    MessageBox.Show("bef loop")

    keybd_event(VK_TAB, 0, 0, 0)

    keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0)

     

    While (Not objReader.EndOfStream())

    LineRead = objReader.ReadLine()

    Clipboard.SetDataObject(LineRead, False)

    SetActiveWindow(8975651603260375040)

    keybd_event(VK_CONTROL, 0, 0, 0)

    keybd_event(VK_V, 0, 0, 0)

    keybd_event(VK_V, 0, KEYEVENTF_KEYUP, 0)

    keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)

    MessageBox.Show("in loop")

    keybd_event(VK_TAB, 0, 0, 0)

    keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0)

    End While

    objReader.Dispose()

    objReader.Close()

    End If

    Thursday, May 31, 2007 11:50 AM

Answers

  • Could you be more specific about what you're trying to do with the keyboard events?  This is probably not working because you need Windows messages to be pumped, and that won't happen if you're in a CPU loop.  One possibility for fixing it is to make calls to System.Windows.Forms.Application.DoEvents(), but make sure you disable any UI (menus, buttons etc) that you don't want the user to be able to click during your loop so that your code doesn't get re-entered.  Another possibility (better, but more complex, you need to understand some relevant rules) is to consider using threads or a BackgroundWorker component.
    Monday, June 4, 2007 10:44 PM
    Moderator

All replies

  • Here's some things which may simplify your life a lot

     

    Reading from a CSV (delimited) format file
    http://msdn2.microsoft.com/en-us/library/cakac7e6(VS.80).aspx

     

    The only difference between the example comma separated and your tab delimited file should be setting the SetDelimiters property to a tab character.

     


    However you problem may well be that you have a messagebox in there and the subsequent keybd_event are to dismiss the messagebox.   Remove the messagebox then your probably going to have to remove these lines of code used to dismiss this dialog otherwise you sending extra unnecessary keyboard events.

     

     

    Thursday, May 31, 2007 5:24 PM
  • I think i couldn't express my problem in my post .....Lets take it like this ...... i want to paste an array of string one by one in a while loop via generating keyboard events. But right now i can't able to do it in a loop but its working fine for a single string only without loop. They problem can be 50% resumed if i put messageboxes between these keyboard events. But i want to do it without messageboxes. And also please tell me if it is not possible.
    Saturday, June 2, 2007 7:00 AM
  • Could you be more specific about what you're trying to do with the keyboard events?  This is probably not working because you need Windows messages to be pumped, and that won't happen if you're in a CPU loop.  One possibility for fixing it is to make calls to System.Windows.Forms.Application.DoEvents(), but make sure you disable any UI (menus, buttons etc) that you don't want the user to be able to click during your loop so that your code doesn't get re-entered.  Another possibility (better, but more complex, you need to understand some relevant rules) is to consider using threads or a BackgroundWorker component.
    Monday, June 4, 2007 10:44 PM
    Moderator
  • sure, Right now i am trying to fill an html form via this project and to do this i am generating keyboard events of control + v and  tab. i.e

     

    SetActiveWindow(8975651603260375040)

    Clipboard.SetDataObject(ListBox1.SelectedItem.ToString())

    keybd_event(VK_CONTROL, 0, 0, 0)

    keybd_event(VK_V, 0, 0, 0)

    keybd_event(VK_V, 0, KEYEVENTF_KEYUP, 0)

    keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)

    keybd_event(VK_TAB, 0, 0, 0)

    keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0)

     

    This is working fine. But when i insert this bunch of code into a loop to fill html form fields with different data ... i got an unexpected behavior. Code in loop is given below...

     

    For i = 0 To Total - 2

    Clipboard.SetDataObject(ListBox1.Items.Item(i))

    SetActiveWindow(8975651603260375040)

    keybd_event(VK_CONTROL, 0, 0, 0)

    keybd_event(VK_V, 0, 0, 0)

    keybd_event(VK_V, 0, KEYEVENTF_KEYUP, 0)

    keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)

    keybd_event(VK_TAB, 0, 0, 0)

    keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0)

    Clipboard.Clear()

    Next

     

     

    i have already inserted "System.Threading.Thread.Sleep(2)" between these but it only solves my problem for only once but its right and wrong behavior is unexpected. Unable to understand really wts going on....

    Wednesday, June 6, 2007 10:06 AM
  • Sleep will cause the program to completely stop during the wait, that won't do any good.  Your program has to be able to process the keyboard events, and you're not giving it a chance to do that.  Try my suggestions of DoEvents or background threading in previous post.
    Wednesday, June 6, 2007 5:51 PM
    Moderator
  • "DoEvents" worked for me but its little bit unstable ..... few empty fields and sometimes duplicate values..... but most of the time it worked fine .... can you tell me how to stabalize it?

     

    Thursday, June 7, 2007 8:04 AM