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.
Related
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 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
In Microsoft Access, when you click on a label, the textbox associated with that label gets the focus. As far as I can tell, VB.NET does not have this same functionality. I know I can always add something in to the click event of the label, like so...
TextBox1.Focus()
But I have dozens of fields on the form, and it would make it so much easier if I did not need to add this to the click event of each label.
I guess it would be possible to make an event for all labels that forces a tab to the next control, and assuming that I have the Tab indices set up correctly, then this would work. The problem would occur when adding new fields to the form, in which case all tab indices would need re-verified.
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click
'code to tab to next field...
End Sub
Is there any easier way?
First, set the controls' TabIndex orders on your form then use this code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls
If TypeOf c Is Label Then AddHandler c.Click, AddressOf Label_Click
Next
End Sub
Private Sub Label_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.SelectNextControl(sender, True, True, True, True)
End Sub
End Class
Now whenever you click on a label, the following control in the order will be focused.
How about creating a dictionary where the label is the key and the control to focus is the value, then add a Click event handler to all of the label in the Dictionary. Everytime you add a label/control 'all' you need to do is add another KeyValuePair to the Dictionary
Simple Example:
Public Class Form1
Protected Friend DicLabelToControl As Dictionary(Of Label, Control)
Protected Friend Sub InitLabelDic()
DicLabelToControl = New Dictionary(Of Label, Control) From {{Label1, TextBox1}, {Label2, TextBox2}}
End Sub
Protected Friend Sub AddClickEventsToLabels()
For Each lb As Label In DicLabelToControl.Keys
AddHandler lb.Click, AddressOf HandleLabelClick
Next
End Sub
Private Sub HandleLabelClick(sender As Object, e As EventArgs)
DicLabelToControl(CType(sender, Label)).Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitLabelDic()
AddClickEventsToLabels()
End Sub
End Class
I have a combobox in a form1 and a datagridview in another form2.
I want to get the combobox selected with a value from the datagridview in the second form
I use the code below in form2 and it works:
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
form1.CBO_fournisseur.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
Me.Close()
End Sub
But what I want to do is that the name of the form is passed dynamically to avoid using and IFELSE clause to enumerate all the forms I have in my project that use form2
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
If formbon2.Name = "FRM_BN_RECEPTION_CUIR" Then
FRM_BN_RECEPTION_CUIR.CBO_fournisseur.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
ElseIf formbon2.Name = "frm_reception_acc_provisoire" Then
frm_reception_acc_provisoire.CBO_1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
End If
Me.Close()
End Sub
I think I've got what you want to do. I strongly suggest you stop using the Form as a Shared resource.
Use a constructor like this in your Form2:
Private ParentFormCombo as Combobox
Public Sub New(ByVal pCmb as Combobox)
ParentFormCombo = pCmb
End Sub
Then in your doubleclick you just change the text of ParentFormCombo
ParentFormCombo.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
Then you have to stop using:
FrmList_View.Show()
Now you should always use the constructor instead (New()). So do the following instead:
Dim f As New FrmList_View(CBO_fournisseur)
'or
Dim f As New FrmList_View(CBO_1)
f.Show()
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