1

I have a Form with a TextBox in it. Every time the text changes i use the TextChanged Event to create a PDF-file and load it to an AxAcroPDF-Object in the same Form. This works fine, but then the TextBox loses focus and for some reason the textBox.Focus() after loading the file doesn't work.

Has anyone ideas how I can arrange that you can go on typing while refreshing the PDF?

EDIT:

i had another idea, i made a separate thread where i update the PDF and in the TextChanged-event i only set a flag. But now im getting a strange error

Unable to cast COM object of type 'System.__ComObject' to interface type 'AcroPDFLib.IAcroAXDocShim'.
SaschaW
  • 58
  • 1
  • 9
  • I think you should set .SelectedControl in the form to the TextBox instance at design time. You cannot set the focussed control in the form loader (if I remember correctly). Sorry I am not sure about the property name, could be .FocusedControl or something. – TheBlastOne Oct 02 '12 at 08:30
  • Is textbox control hosted inside groupbox, panel or any other container ? – Danilo Vulović Oct 02 '12 at 08:32
  • How you load the file? Look for some kind of event triggered when the file finished loading and only then set the focus. Otherwise, try use timer for this. – Shadow The GPT Wizard Oct 02 '12 at 08:32
  • @TheBlastOne: couldnt find the property you meant, theres no focus in form.controls or sth. like that – SaschaW Oct 02 '12 at 08:51
  • @Danilo Vulović : No, simple testform, only a button, a textbox and the pdf-viewer – SaschaW Oct 02 '12 at 08:52
  • @ShadowWizard : the com-object has a method named LoadFile(), i already looked for a suitable event but i couldnt find one, and a timer isnt the best solution when you want to typa and you have to wait until the textbox has focus again – SaschaW Oct 02 '12 at 08:52
  • In the form´s Shown event, call the control´s Focus method. Report back what happens. (Don´t do it in the FormLoad oder FormLoaded or what have you event) – TheBlastOne Oct 02 '12 at 09:16
  • Is the PDF com component the culprit? Does it work if you remove it temporarily? – TheBlastOne Oct 02 '12 at 09:17
  • @TheBlastOne The result is that after the startup the textbox has focus, but after typing the first letter it still loses focus and doesnt get it back – SaschaW Oct 02 '12 at 09:19
  • @TheBlastOne i tried Controls.Remove(axAcroPDF1); axAcroPDF1.LoadFile(filename); Controls.Add(axAcroPDF1); but same result – SaschaW Oct 02 '12 at 09:22
  • No, remove the PDF component at design time, comment all references, and report what happens. – TheBlastOne Oct 02 '12 at 09:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/17441/discussion-between-theblastone-and-saschaw) – TheBlastOne Oct 02 '12 at 09:24
  • ehm, that wont make any sense because i want to show a preview of the generated pdf. i think you are right, its the com-object and when i tried it with another thread the textbox kept focus so it definitely has to be the pdf-component. but without it the application is useless ;) – SaschaW Oct 02 '12 at 09:26

3 Answers3

4

Try this one:

textBox.Select();
textBox.Focus();
Danilo Vulović
  • 2,983
  • 20
  • 31
1

Im so ashamed of myself, i found a really, really dirty hack, but it works... I did the following:

When i write a text in the MessageBox i rewrite my PDF in the TextChange-Event. In the same method i store the Control that has focus (when calling the LoadFile on the PDF-Object this Control still loses focus). And now the dirty work comes: I implemented a Thread that constantly sets focus to the Control stored in the variable. In the Leave-Event of the TextBox i reset the variable so other controls wont be blocked.

Its a really dirty hack i know, but now i can instantly "edit" a pdf with my own form, which is a nice eyecandy ;)

Thanks for all the help!

SaschaW
  • 58
  • 1
  • 9
0

I could not get .Focus() and .Select() to work so I used Jquery and it works perfectly.

$(document).ready(function () {
    setTimeout(function () {
        $(".contentWrapper input")[0].focus();
    }, 100);
});
Josiah
  • 63
  • 5