How to pass values from a textbox to another textbox using variables? - vb.net

I am creating a chat application and i want the application to collect some information from the user like, Name, Nationality etc. I am using variables for that.
I thought it would be easy, but it was the absolute opposite. When i try to display username on another form, It just doesn't
display any text on the label.
I tried doing it by storing the values in Application Settings.
My.settings.Username = BunifuMetroTextBox1.text
My.settings.Nationality = BunifuMetroTextBox2.text
Private Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.text = My.settings.Username
Label2.text = My.settings.Username
End Sub
This above written block of code absolutely displays no text on the label. After this, I tried doing it with Variables.
Below is the code -
Public class MainMenu
Public Property UserName As String
Public Property Nationality As String
Private Sub BunifuButton1_Click(sender As Object, e As EventArgs)
Username = BunifuMetroTextBox1.text
Nationality = BunifMetroTextBox2.text
End Sub
Private Sub Home_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.text = Username
Label2.text = Nationality
End Sub
End class
Here are some images -
Code for the button which passes the value is highlighted
Code for the labels which receive the text is also highlighted
The above code too doesn't display any text on the label too. Any Solutions?

To use Settings In the Solution Explorer double click My Project or from the Project Menu choose ProjectName Properties (at the bottom of the menu). Now choose the Settings tab. Add your settings as shown. Be sure to scope to User so you can edit the setting.
Then in you set your settings thusly...
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.UserName = TextBox1.Text
My.Settings.Nationality = TextBox2.Text
Form2.Show()
Me.Close()
End Sub
End Class
Form2 will open and the labels are filled like this...
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = My.Settings.UserName
Label2.Text = My.Settings.Nationality
End Sub
End Class
This is a good way to do things because it does not depend on whether you are using default instance of forms or if you have to pass a reference to the non-default instance.
See https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/objects/my-settings-object for more details on the Settings object.

You also want to make sure that you call My.Settings.Save() or enable Save My.Settings on Shutdown which is located on the Application Tab when you view the properties of your project.

Related

"Reference to a non-shared member requires an object reference" when trying to make a form switch

I'm making a software that needs to go between a large amount of forms when i click a button. Right now, i have it as
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
Me.Visible = False
SortsTrigger.Show()
End Sub
and that seems to bring up the code error. Nothing else is explaining it, How do i fix this?
With the limited code you displayed, I would guess that you have not initialized your other form (assuming that SortTrigger is a reference to a form class object). Using your original code, here is my suggestion:
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles
Button2.Click
Me.Visible = False
If SortsTrigger Is Nothing Then
'Assign the SortsTrigger to a new class reference
SortsTrigger = New frmSorts 'Or whatever the class name is for this form
End If
SortsTrigger.Show()
End Sub

Control Properties of LinkLabel when used for HelpProvider

I am working with Visual Studio 2017, working in VB. I am linking to a .CHM file from a LinkLabel which works fine with following code:
Private Sub LinkLabel2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkLabel2.Click
' Create link to help file
System.Windows.Forms.Help.ShowHelp(Me, "RPM_Help.chm", HelpNavigator.AssociateIndex)
End Sub
I use a simple image to indicate the link to the .chm file that is 32x32 pixels in size, I have been playing with the properties of LinkLabel2 but I just can't figure out how to make the entire label a link space. I did find that unless there is a Text property on the label a MouseOver cursor will not show up, but how can I make the entire label image a cursor link?
As shown in the code and image below this can be solved by using LinkLabel2.AutoSize = False and adding a ToolTip from Common Controls to the form in design mode.
Each control, such as Buttons and TextBoxes, will acquire a ToolTip on toolTip1 property when you add a toolTip1 to your designer view. You can access this property in the Properties pane to set the tool tips.
Public Class frmMain
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LinkLabel2.BackColor = Color.CornflowerBlue
LinkLabel2.AutoSize = False
LinkLabel2.Width = 168
LinkLabel2.Height = 40
LinkLabel2.Text = ""
End Sub
Private Sub LinkLabel1_Click(sender As Object, e As EventArgs) Handles LinkLabel1.Click
' --- Open help file - Table of contents
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.TableOfContents)
End Sub
Private Sub LinkLabel2_Click(sender As Object, e As EventArgs) Handles LinkLabel2.Click
' --- Open help file - Index
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.Index)
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
' --- Open help file - Search
System.Windows.Forms.Help.ShowHelp(Me, "hlp/CHM-example.chm", HelpNavigator.Find, "")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
The code corresponds to the image shown. The values can of course also be set via the properties, but are included here in the FormLoad for documentation.
You may want to use a simple PictureBox1_Click event for your needs as shown below (third item in the "Show help" group box).

VBNET: How to Reset ComboBox after selection is made

How do I reset a ComboBox to default Text i set in properties. Say I have a ComboBox with default text "Ruby" which when SelectedIndexChanged is printed in TextBox afterwards it does'nt reset to default text "Ruby" but the SelectedItem. I want it to read "Ruby" afterwards or all the time if not possible. Thank you
Declare the helper class variable in the form class:
Dim _originalComboText As String
On opening the form, remember the default text of the control in some helper variable. For example, add the following line into the constructor (Sub New()):
_originalComboText = ComboBox1.Text
Every time after the selection is made, restore the text from it:
ComboBox1.Text = _originalComboText
If you inspect the content of the .designer.vb file belonging to your form, you can actually see initialization of text of your control through the assignment (=) – so vb.net does no special magic here. If you want to preserve the text, you have to save it somewhere before it gets lost.
Here is the complete minimum example:
Public Class Form1
Dim _originalComboText As String
Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_originalComboText = ComboBox1.Text
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Text = _originalComboText
End Sub
Private Sub ComboBox1_LocationChanged(sender As Object, e As EventArgs) Handles ComboBox1.LocationChanged
ComboBox1.Text = _originalComboText
End Sub
End Class
And if your two handlers do not differ, then replace them just with one – with two events in Handles clause:
Private Sub ComboBox1_RestoreText(sender As Object, e As EventArgs) _
Handles ComboBox1.Leave, ComboBox1.LocationChanged
ComboBox1.Text = _originalComboText
End Sub
Use this:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Text = "Ruby"
End Sub
Private Sub ComboBox1_LocationChanged(sender As Object, e As EventArgs) Handles ComboBox1.LocationChanged
ComboBox1.Text = "Ruby"
End Sub
Since the user will choose, say print <<"EOF"; ---mytext---EOF from dropdown list and this will automatically print on textbox. He or she must go to textbox or somewhere else.
I know its not coventional to answer own question but if anyone needs help. Here it is

Can I declare a dynamically adding new button globally?

I need help in my VB.NET project. I declare a dynamic textbox which is "dim textbox as new textbox()" inside the button and it will create a new textbox when I press button1 and I also put text on the new textbox created. Now when a dynamic textbox already created. When I press button2 I want to compare the text on the label to the text in dynamic textbox created but I got an error that says, OBJECT REFERENCE NOT SET AN INSTANCE OF AN OBJECT.
I suppose you create the textbox in the click event handler of the button. this way, you can only access it in this event handler method. In order to use it in the other event handler, you need to move the declaration to class level, e.g.:
Private txtBox As Textbox
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
txtBox = New Textbox()
' ...
Me.Controls.Add(txtBox)
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If txtBox IsNot Nothing Then
MsgBox(txtBox.Text)
End If
End Sub
Try it like this. You have to store the reference to your textbox in a variable, e. g. in the scope of the form instance.
Public Class Form1
Private dynTextbox As TextBox
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.dynTextbox = New TextBox
With Me.dynTextbox
.Top = Me.Button1.Top
.Left = Me.Button1.Right + 5
.Text = "test"
End With
Me.Controls.Add(Me.dynTextbox)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If Me.dynTextbox IsNot Nothing Then MessageBox.Show(Me.Label1.Text = Me.dynTextbox.Text)
End Sub
End Class
If you create multiple textboxes dynamically, you may store them in some Array or List (e. g. in a List(Of TextBox)) and then you'll have to find a way to reference the one you need, that depends on your specific project.

How to pass value of a textbox from one form to another form

If I have a value stored into a textbox of form1 and I have to pass that value into an another textbox of another form2. What is the method to do this passing values from one form to another?
There are a no. of ways.
1.Using TextChanged event.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Using Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TextBox1.Text = TextBox1.Text
End Sub
Using LostFocus Event:
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
Form2.TextBox1.Text = TextBox1.Text
End Sub
Similarly you can work with every events.
In order to retrieve a control's value (e.g. TextBox.Text) from another form, the best way is to create a module and create a property for the private variable.
An example of a property to hold a customer's first name:
Module modPrivateVariables
Private strCustomerFirstNameSTR As String
Public Property getCustomerFirstNameSTR() As String
Get
Return strCustomerFirstNameSTR
End Get
Set(ByVal strCustomerFirstName As String)
strCustomerFirstNameSTR = strCustomerFirstName
End Set
End Property
End Module
Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text. For example, if you had a textbox named (txtCustomerFirstName), under its TextChanged event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text.
The textbox's text will now be assigned to getCustomerFirstNameSTR property. Now you'll be able to access this property's value from anywhere and from any form in your application. For example if you had a textbox in another form say Form2 called "txtBoxInForm2" you can call txtBoxInForm2.Text = getCustomerFirstNameSTR.
If you wanted to clear the value of the property then just type getCustomerFirstNameSTR = String.Empty. The main thing to understand is that when you create a variable in one form (class) and try to access its value from another form (another class) then the variable has to be re-instantiated once.
This happens then the variable is reset to its default value which is an empty string. This will cause you to keep getting nothing (an empty textbox) every time you call it from another form. Properties don't need to be re-instantiated because they are accessed through public methods within the property itself (the get and set methods).
if both the forms are running, then you can use
form2.TextBox1.Text=form1.TextBox1.Text
Else you can declare a Public String variable in Form2, on any event,
dim Obj as new Form2
Obj.StrVariable=Me.TextBox1.Text
Obj.Show
and on Form2 Load,
Me.TextBox1.Text=StrVariable
In Form1.vb make sure you use an event such as Button.Click and inside that
Dim obb As New Form2
obb.val = Me.TextBox1.Text()
obb.Show()
Me.Hide()
In Form2.vb use a property called "val"
Public Property val As String
And on an event like MyBase.Load
TextBox1.Text = val
You could use the button1_click and state there:
Dim obj as new form2
Obj.pass=me.textbox1.text
Obj.show()
Then in your form2 before your form2 main class you state:
Public property pass as string
On the load state
Textbox1.text=pass
Now, when you click on the button on form1, form2 will show and the textbox1 on form2 will have the same text as the one in form1. Provided you use this only with text box, labels or other kind of STRING or will work.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' timer1 must be set to true
Timer1.Start() Form1.Show() Me.Hide()
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Form1.TextBox13.Text = TextBox1.Text