String passing between panels in VB.Net - vb.net

I'm using a VB.NET form that contains two panels; I want the user to be able to enter a string value in a textbox on panel 1, and have it appear in a label in panel 2.

Basic Example:
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Label1.Text = TextBox1.Text
End Sub
Don't worry about whether or not they're in different panels.

Related

How do I add items to a listbox inside a user control?

I have a user control (XListBox) with a listbox (ListBox1) and a button on it. The user control is on a form (Form1) which also has a button on it. The code is shown below. If click the button inside the user control the text 'Add Item from inside XListBox' appears in the Listbox no problem. If I click the button on the form however, the string 'Add item to XListBox from form' does not get added to the listbox although the Debug.Print does show it in the output window. It appears that I am missing something but I have no idea what. Any help would be appreciated. Thanks in advance.
Public Class Form1
Dim lstActions As New XListBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
lstActions.AddItem("Add item to XListBox from form")
End Sub
End Class
Public Class XListBox ' user control
Private Sub XListBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add("Add Item from inside XListBox")
End Sub
Public Sub AddItem(sString As String)
ListBox1.Items.Add(sString)
Debug.Print(sString)
End Sub
End Class
Why do you have this in your code there?
Dim lstActions As New XListBox
Surely you added the user control to your form in the designer, like you would any other control. How do you usually access a control that you added to the form in the designer? Basically, you are ignoring the one you added in the designer, i.e. the one you can see, and you have created another one that you never add to the form, so you can't see it, and that's the one you're adding the items to. Don't. Get rid of that field and use the one you added in the designer.

How to pass values from a textbox to another textbox using variables?

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.

Select (focus) one form from multiple forms (same form) in visual basic.net

I have multiple instances of the same form open, I want to select one instance of that form to get focused. So I made an combo-box which lists all the titles of that form with different names thus when I select one title coming from the combo-box It should get focused and be in front.
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If Application.OpenForms().OfType(Of Customers).Any Then
MessageBox.Show("Opened")
'code to select the form from the .text
'->
End If
End Sub
It might be easier to create a List(Of Form) and as you are populating your ComboBox with the form names, you can add each of the forms to a list. Then when you click on, say, item 0 in your ComboBox, the form at index 0 in the list of forms could be focused - like this maybe?
'put this in your main declarations
Dim ListOfForms As New List(Of Form)
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
ListOfForms(ComboBox1.SelectedIndex).Focus()
End Sub
Private Sub PopulateComboBox()
ComboBox1.Items.Clear()
ListOfForms.Clear()
ComboBox1.Items.Add(frm1.Name)
ListOfForms.Add(frm1)
ComboBox1.Items.Add(frm2.Name)
ListOfForms.Add(frm2)
ComboBox1.Items.Add(frmX.Name)
ListOfForms.Add(frmX)
End Sub

How to get the same value without clicking the button

Using VB.Net
2 textbox in the Form (textbox1.text, textbox2.text)
Once user entered the value in the textbox1 means automatically textbox1 value should appear in the textbox2 without clicking the buttons.
In which event i have to write a code instead of button clicking event....
Need vb.net code Help
Try this..
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TextBox2.Text = TextBox1.Text
End Sub
Every time you type a value in textbox1 it will appear in textbox2

Don't know what I am doing wrong in controls

Ok Here is what I have so far. I am trying to do the checkbox so that way only one is check at a time. But for some reason they both will check. And another thing I have the labels on there but in the radiobuttons no matter which one you check and submit it only stats the bottom one. Can someone help me and tell me what I am doing wrong...
Public Class Form1
Dim Message As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, MaskedTextBox1.TextChanged
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text <> "" Then
ErrorProvider1.Clear()
Label1.Text = TextBox1.Text
Else
ErrorProvider1.SetError(TextBox1, "Please enter text")
End If
End Sub
Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
Me.Text = Me.MonthCalendar1.SelectionRange.Start.ToShortDateString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Text = MaskedTextBox1.Text
Label3.Text = RadioButton1.Text
Label3.Text = RadioButton2.Text
Label3.Text = RadioButton3.Text
Label4.Text = CheckBox1.Text
Label5.Text = CheckBox2.Text
Label6.Text = MonthCalendar1.Text
Dim message As String
message = ""
If CheckBox1.Checked Then
message += "first one clicked"
End If
If CheckBox2.Checked Then
message += " second one clicked"
End If
MessageBox.Show(message)
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
Dim radMessage As RadioButton = CType(sender, RadioButton)
If radMessage.Checked Then
Message = radMessage.Text
End If
End Sub
End Class
Ok and this is suppose to be what my commands are suppose to be doing..
Create a new window Project .change the title of the form to Lab3.drag the following controls to your form,textbox,masked textbox (set to phone number),3 radiobuttons in a groupbox(container control),2 checkboxes, Month calendar,all on the left of the form,add a label for each control on the right, add an errorprovider for the textbox and insure it has data entered, add tooltips to every control, add a button
Task
When the user mouses over any control on the left they get a tooltip, change the text on the button to "Submit", when the user clocks the button, the info from the controls on the left appears in the label on the right,if no data is entered in the textbox use the errorprovider control to prompt the user for data, only use 1 label for all three radiobuttons, Only display each check box data if the check boxes checked, make sure to add code to erase the appropriate label if the user UNCHECKS a check box, set the tab order of the controls so you can tab though them in order down the left side, look on the internet to find out how to rettrieve the selected date from the calendar control when the user clicks the button.
I am not expecting anyone to do this for me. I am trying to do the code but i know i mest up some where. I am still a newbie at this so sorry.
I don't expect this answer to solve all of your problems but hopefully it should prove a starting point.
Firstly, why do you want only one checkbox to be selected at any moment? I can't see that as a requirement in your task? By default checkboxes do not exhibit that behaviour (that is what radio buttons are for), but they can be made to. To do this you would need to look at the selected_change event of the checkbox and when one is selected, deselect all the others. However before you do that, make sure it is what you are after before progressing.
With the radio buttons issue, I am assuming you want to set the text of the selected radio button into a label? Well currently you are assigning the selected text into a field, Message, but you are not using this anywhere else. When you set up your labels you have the following:
Label3.Text = RadioButton1.Text
Label3.Text = RadioButton2.Text
Label3.Text = RadioButton3.Text
Whereas I am guessing you need something along the lines of
Label3.Text = Message
At the moment you are setting the same Label text three times and only the third radio button text will be displayed.
On another note I would possibly think about using different variable names that Message as it could result in some confusion later on.
Hope that gets you somewhere