Dim _i As Integer = 1
Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString) ' this will not work.
TextBox1.Text = (ListBox1.Items(_i + 5).ToString) ' this works.
Please Help
The error I get :An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
System.NullReferenceException was unhandled
HResult=-2147467261
Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
Source=WindowsApplication1
StackTrace:
bij WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in Z:\WindowsApplication1\Form1.vb:Line 35
bij System.EventHandler.Invoke(Object sender, EventArgs e)
bij System.Windows.Forms.Form.OnLoad(EventArgs e)
bij System.Windows.Forms.Form.OnCreateControl()
bij System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bij System.Windows.Forms.Control.CreateControl()
bij System.Windows.Forms.Control.WmShowWindow(Message& m)
bij System.Windows.Forms.Control.WndProc(Message& m)
bij System.Windows.Forms.ScrollableControl.WndProc(Message& m)
bij System.Windows.Forms.Form.WmShowWindow(Message& m)
bij System.Windows.Forms.Form.WndProc(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
With this line 35 : Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString)
Thanks
Regards
Last edited by Lieven; Feb 27th, 2021 at 01:40 PM.
I wouldn't've changed the ListBox1 item to a string... It's surly already a string.
I don't like the '_i' although it does seem to work. What's wrong with just 'i', most folks seem to use it, I always assumed it was a form of shorthand for 'Index'.
Poppa
Last edited by Poppa Mintin; Feb 26th, 2021 at 05:47 PM.
Along with the sunshine there has to be a little rain sometime.
The error I get :An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
System.NullReferenceException was unhandled
HResult=-2147467261
Message=De objectverwijzing is niet op een exemplaar van een object ingesteld.
Source=WindowsApplication1
StackTrace:
bij WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in Z:\WindowsApplication1\Form1.vb:Line 35
bij System.EventHandler.Invoke(Object sender, EventArgs e)
bij System.Windows.Forms.Form.OnLoad(EventArgs e)
bij System.Windows.Forms.Form.OnCreateControl()
bij System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
bij System.Windows.Forms.Control.CreateControl()
bij System.Windows.Forms.Control.WmShowWindow(Message& m)
bij System.Windows.Forms.Control.WndProc(Message& m)
bij System.Windows.Forms.ScrollableControl.WndProc(Message& m)
bij System.Windows.Forms.Form.WmShowWindow(Message& m)
bij System.Windows.Forms.Form.WndProc(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bij System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bij System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:
With this line 35 : Controls("TextBox" & _i).Text = (ListBox1.Items(_i).ToString)
Last edited by Lieven; Feb 27th, 2021 at 01:40 PM.
What is the value of _i? Don't forget your ListBox items are zero based. If, for example, you know _i to be equals to 1, do you have a TextBox1 on your form? If TextBox1 is in a Panel, or a GroupBox, you need to use the correct Parent property in your Controls code, i.e. GroupBox1.Controls(' etc
Every Control has a Parent Control, which can be a Form, a Panel, a GroupBox.
Controls(“TextBox” & _i) looks for a Control whose parent is your Form...
This is why Visual Studio provides us with debugging tools. You can set a breakpoint and step through your code, line by line, examining the state of your variables as you go...
I didn't need to run your app, to find your problem. After reading your Form1.Designer.vb file to assess your control hierarchies, it was easy to spot the error in your Form1.vb Form_Load event.