How do I pass Form1.TextBox1.Text to Form2.TextBox2.Text using Visual Basic (correctly)? - vba

This is a continuation of a question asked two years ago in this thread:
VB6 equivalent of string.IsNullOrEmpty
(I think.) A programmer recommended I use the String.IsNullorEmpty method, which I used this thread for to convert to Visual Basic, but I still couldn't get it to work.
The specifics of my question are here, including all current code:
http://www.daniweb.com/software-development/visual-basic-4-5-6/threads/473930/passing-data-between-forms-in-vba
Here is the gist of it, copied directly from the second link:
So I'm trying to make a link between TextBox1.Text on Form1 to TextBox2.Text on Form 2. What I currently have is a line of code underneath my TextBox2_Change code reading:
TextBox2 = Form1.TextBox1.Text
This ALMOST does what I want it to do. The only problem is that it is requiring me to input any character in to the TextBox2 when Form2 pops up before it displays.
I'm trying to get that problem solved and then I'm eventually going to try to get it to chop off part of the file name until just the project file name displays.......but that's a whole different game I'll be playing. One step at a time.
Does anyone have any suggestions?

Explanation
You should write the code under the Form2_Load event.
If you write the code under Textbox2_TextChanged event, the code will be executed only when you type or delete something in Textbox2 (That is the same as Text being changed).
Code and Example
Private Sub Form2_Load () Handles Mybase.Load
Textbox2.Text = Form1.Textbox1.Text
End Sub
Hope it works perfectly!

The behaviour you observe is normal : your TextBox2 is only updated (with the value from TextBox1) ... when you update it manually (_Change).

Hey friend it very simple. you need not to add any kind of other functions just use dot(.) operator to access all components of Form1.
e.g.:
Form1.TextBox1.AppendText("hello")
or you can read value from Form2 and insert it into Form1.
e.g. :
Dim txt As String=TextBox1.Text
Form1.TextBox1.AppendText(txt)

Related

In VB.NET use a textbox as a log for which if statement it is beeing proccesed inside a sub

Hi i have a Sub that has multiple if statements in it.
Each if statement has a large loop that searches for specific files and text inside files.
I tried various ways to use a text box in order to get the information which if is currently proccessing at the time and i see that for some reason the ui is not refreshed until the sub finishes and so i see everytime in the textbox the last proceesed if message.
What do you think is the best way to handle it?
I hope that this has nothing to do with threads because threads are something that i am not familiar with !
I think using Application.DoEvents() is an easy choice. But I don't know if that would be the desired behavior.
If the use of Application.DoEvents() fails, another thread should handle it.
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.doevents?view=netframework-3.5
I use this:
Public Sub logWithCrLf(tx As TextBox, s As String)
tx.AppendText(s & vbCrLf)
tx.Select(tx.TextLength - 1, 0)
tx.ScrollToCaret()
tx.Refresh()
End Sub
I see that it scrolls a bit smoother using tx.AppendText(s) than tx.Text &= s, which scrolls up to 0 then down to caret again.
(I write this as an answer to contribute with the tx.AppendText() recommendation)

Put A textbox value in a Label from another form

I have Two Forms
In The First Form I Have A Textbox Called TB1 An A String Called S1
In The Second Form I Have A Label Called L1
I Declared them publicly in a module
Module M1
Public L1 As New Label
Public TB1 As new Textbox
Public S1 As new String
End Module
/../
'in the First form
S1=TextBox1.text
'in the second form
L1.text=S1
But I Get this error "System.NullReferenceException" on the first Label L1
Any ideas why i'm getting this error
Try something like this:
On Form1 in the event calling Form2, Add:
Form2.L1.Text = TextBox1.text
Form2.Show()
You can't put them in a module like that; nothing will see them.
In addition, there's no way to see how this code is flowing.
To do a simple exercise:
Draw first form - form1 with Textbox1 on it.
Make the first form startup form (it will be by default in new winforms project)
Draw 2nd form - form2 with Label1 on it.
Put a button on the first form
Double click the button
In the handler put this code:
Sub Button1_Click (etc...
dim Form2Instance as new Form2
Form2Instance.Label1.Caption = Textbox1.text
Form2Instance.ShowDialog
End Sub
You can put a textbox on form2 and read the results in form1 the same way.
You can tell if the user OK'd (not canceled) form2 like this:
if Form2Instance.ShowDialog() = DialogResults.OK then
' only do this code if they HIT OK
I suggest maybe hitting youtube or pluralsite and watching a 'winforms for beginners' video, if such a thing exists. There are a lot of traps and pitfalls that you will run into hacking away on your own, that will be explained to before hand if you learn the basics. This is especially true if you've only done web programming or are just learning programming.

Simplify one-type syntaxis in vb.net

I have the next piece of code:
Private Sub TextBox_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged,
TextBox3.TextChanged, TextBox4.TextChanged, TextBox5.TextChanged, TextBox6.TextChanged, TextBox7.TextChanged,
TextBox8.TextChanged, TextBox9.TextChanged, TextBox10.TextChanged, TextBox13.TextChanged, TextBox14.TextChanged,
TextBox15.TextChanged, TextBox18.TextChanged, TextBox19.TextChanged, TextBox20.TextChanged
Double.TryParse(TextBox1.Text, Component.Methane.Mole)
Double.TryParse(TextBox2.Text, Component.Ethane.Mole)
Double.TryParse(TextBox3.Text, Component.Propane.Mole)
Double.TryParse(TextBox4.Text, Component.iButane.Mole)
Double.TryParse(TextBox5.Text, Component.nButane.Mole)
Double.TryParse(TextBox6.Text, Component.neoPentane.Mole)
Double.TryParse(TextBox7.Text, Component.nHexane.Mole)
Double.TryParse(TextBox8.Text, Component.nHeptane.Mole)
Double.TryParse(TextBox9.Text, Component.N2.Mole)
Double.TryParse(TextBox10.Text, Component.CO2.Mole)
Double.TryParse(TextBox13.Text, Component.iPentane.Mole)
Double.TryParse(TextBox14.Text, Component.nPentane.Mole)
Double.TryParse(TextBox15.Text, Compress.Avol)
Double.TryParse(TextBox18.Text, Compress.AF)
Double.TryParse(TextBox19.Text, Compress.AP)
Double.TryParse(TextBox20.Text, Compress.AT)
End Sub
Is there any possibility to make this procedure more compact, beatiful and smarter ? Use some kind of control, loop or something else? It would be greate to make it shorter and avoid typing. I am quite new in programming, any help is very appreciated!
Thanks in advance!
If your range of textboxes should only contain numbers, create a new Control that inherits from Textbox and alter the Textbox to only accept valid numbers. See an example here:
https://www.tigraine.at/2008/10/28/decimaltextbox-for-windows-forms/
The other thing I would point you is to look into Databinding. Databinding is something where a WinForm or WPF Control will "push" the value contained within the Control back to a property on an object. Windows has made a nice and quick write up on it here:
https://msdn.microsoft.com/en-us/library/2b4be09b.aspx
Finally, for "beautiful code" give things meaningful names. Currently your textbox have the default name and this can result in some very confusing problems later on in life. A quick and easy naming convention I follow is , for example, instead of "TextBox15" you could have TextboxCompressAvol (Or, txbxCompressAvol).

Open the same form twice or more?

I'm currently working on a game that uses a "simulated operating system". But I'm stuck now. I want the players to be able to open two or more of the same windows, but I just can't get more than one to open. If I want to open another one with the Form.Show() command it just doesn't open again.
I would apprechiate any help!
Mika
In order to be able to be offered better help, you should provide the example of the code you are already using (even if it's not working, that's the point).
I'll try to guess what your code is. Since you are mentioning Form.Show(), my guess is that your code looks a bit like this:
Private formVariable = New FormClass()
Private Sub ButtonClick()
formVariable.Show()
End Sub
If this is the case, what happens is that you created formVariable once, and you are calling the same instance over and over again with formVariable.Show().
To solve that, you should create a new instance every time you click the button, like this:
Private Sub ButtonClick()
Dim formVariable = New FormClass()
formVariable.Show()
End Sub

Visual Studio 2013: Cannot update textbox with event handler

I am building an application using one of our vendors interfaces, which is required to keep a status message box updated.
I have some events which I have handled for testing using a Message box, but now I come to pass these messages to the display box I get nothing.
Public Shared Sub PageAirHandler(ChannelNum As Integer, Index As Integer, ChannelType As CLARITYCOMLib.ChannelType, PageName As String) Handles Status1.AirStatusChanged
MessageBox.Show(PageName)
ControlPanel.AirStatusBox.Text = PageName
End Sub
The messagebox dutifully displays the PageName string, but the textbox does nothing... even if I replace the PageName String variable with "test"
ControlPanel.AirStatusBox.Text = "test"
I get no activity, no errors, nada.
I have googled around, but every example I can find seems to show the same code.
I have recreated the textbox, tried buttons, labels and other objects with the same result.
Setting up a button click handler to update any of these works as expected.
Apologies if this is a noob blunder, but it's driving me nuts!
Using dlg As New Form2
dlg.ShowDialog()
End Using
Things like:
Form2.Show()
Form2.Show(Me)
Form2.ShowDialog()
Form2.ShowDialog(Me)
SHOUL BE AVOIDED but are possible, because Vb.net creates an implicit instance.
But the problem is, this instance gets killed if
the call has be done.
There you can see that a class instance of a project does not interact correctly
in some cases.
As Hans Passant said, your call to set the textbox text
targets the wrong instance.
Invoking to user controls is also possible with:
Me.invoke(sub()
TextBox1.Text = "Blabla"
End sub)