How to refresh a textbox on a form without refreshing the entire form?
I am new into programming and having a issue with refreshing a text box with data from another form. I am working on a program to help keep track of specimens while the main system is offline.<br><br>My intention is to allow the user to press F9 while in a text box on form1 to open a search form, select a client on form2, then update the textbox on form1 with the text selected, then close the search form. What is currently happening is everything works all the way up to updating the textbox on form1 with the new data. If I use form1.show() then all of my previous entries are lost, but the text I am after is there. Is vb .net not able to refresh a single textbox without refreshing the entire form? I have been working with this for a month, been all over the web, and no luck on my own so I thought I better pose a question. <br><br>Here is my code.<br><br>Form1<br><br>Form1.jpg<br>Form2<br><br>Form2.jpg
Let's say that you have a main form with a Label on it and a dialogue with a TextBox on it. You want to open the dialogue and display the text from the Label in the TextBox to let the user edit it, then display the new text back in the Label afterwards. In your dialogue form you would define a property to expose the Text of the TextBox:
Public Property TextBoxText() As String
Set(value As String)
TextBox1.Text = value
To display the dialogue and pass the text in and out you would do something like this:
Using dialogue As New Form2
'Pass the current text in.
dialogue.TextBoxText = Label1.Text
If dialogue.ShowDialog() = Windows.Forms.DialogResult.OK Then
'Get the new text out.
Label1.Text = dialogue.TextBoxText
Ok, I see what I am doing wrong. I need to force an update of the text box control "tbTest" not the form. Instead of displaying a new form or dialog, I used the method, Focus() on form2.
So here is the rollup: While in form1, use the F9 key while the cursor is in the text box to open a search form. Then select the item from the Data Grid View. Once the save button is clicked on form2 to save the entry, the click event of the button calls the Focus() method of the original text box on form1. Once the text box "GotFocus", then clear the textbox, assign the return variable, and move to the next control.
No that is not a valid approach. For a start, you should basically never call Focus or handle the GotFocus event. The documentation for both specifically says so. If you did need to do that sort of thing you would call Select and handle Enter, but you don't need to do either. There's no point setting focus explicitly to the TextBox because it will receive focus automatically when the dialogue closes. There's also no reason to handle any event other than KeyDown for the TextBox. You handle KeyDown, detect F9, call ShowDialog and then, EXACTLY as I have already demonstrated, get the value from the dialogue and display it in the TextBox.