473,804 Members | 2,205 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem printing to a listbox from an array

6 New Member
'Here is some of the code I have so far.

Dim Contacts(25, 4) As String

This is where I add to the listbox

Private Sub btnAddToList_Cl ick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnAddToList.Cl ick
Dim f, l As Integer
f = Len(txtFirstNam e.Text)
l = Len(txtLastName .Text)

'print to listbox in columns

lstList.Items.A dd(txtLastName. Text & Space(30 - l) & _
txtFirstName.Te xt & Space(26 - f) & _
txtPhone.Text)

'put into an array

Contacts(lstLis t.Items.Count - 1, 0) = txtLastName.Tex t
Contacts(lstLis t.Items.Count - 1, 1) = txtFirstName.Te xt
Contacts(lstLis t.Items.Count - 1, 2) = txtPhone.Text
Contacts(lstLis t.Items.Count - 1, 3) = txtCatagory.Tex t

'clear the fields for new values

txtFirstName.Te xt = ""
txtLastName.Tex t = ""
txtPhone.Text = ""
txtCatagory.Tex t = ""
txtFirstName.Fo cus()


Private Sub btnDisplay_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnDisplay.Clic k

'clear the listbox
lstList.Items.C lear()

'try to print one item to the listbox from the array named Contacts

lstList.Items.A dd(Contacts(lst List.Items.Coun t - 1, 0))

'here is the error message I get???

An unhandled exception of type 'System.IndexOu tOfRangeExcepti on' occurred in ListProgram.exe
Additional information: Index was outside the bounds of the array.


What do I have wrong here???
Thanks
Gary
Jul 18 '06 #1
3 3995
BSOB
77 New Member
wow.. took me a second. ok, i would need to see more code by ive got my hunch:
<problem>
lets say, perhaps, that your lstlist is empty, right, and you run your code. im assuming it would be empty to start since you are running your program to fill it
Contacts(lstLis t.Items.Count - 1, 0) = txtLastName.Tex t
Contacts(lstLis t.Items.Count - 1, 1) = txtFirstName.Te xt
Contacts(lstLis t.Items.Count - 1, 2) = txtPhone.Text
Contacts(lstLis t.Items.Count - 1, 3) = txtCatagory.Tex t
if your list is empty then it is trying to fill the array contacts(-1,0). array values are positive, i think you have it defined 0 to 24 and -1 is not in that.

<fix>
i think you should have made this +1 instead of -1. but again, i dont know your application.
a round about way to fix this is to have the program add a null list entry to the listbox when it starts.
and... i could just be completely wrong.
-bsob
Jul 18 '06 #2
papa smerf
6 New Member
This is where I am loading the array with values from LastName, FirstName, Phone and Catagory.

Contacts(lstLis t.Items.Count - 1, 0) = txtLastName.Tex t
Contacts(lstLis t.Items.Count - 1, 1) = txtFirstName.Te xt
Contacts(lstLis t.Items.Count - 1, 2) = txtPhone.Text
Contacts(lstLis t.Items.Count - 1, 3) = txtCatagory.Tex t

I tryed +1 instead of -1 same thing happened.

Thanks
Gary
Jul 18 '06 #3
BSOB
77 New Member
next suggestion, instead of a -1 or +1, leave out that bit entirely. Have contacts(lstlis t.items.count, 0) = txtlastname.tex t.
this will still give an error if lstlistitems.co unt is more than 24. and i dont know what your list has in it when you run this subroutine. check that out.
Jul 25 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
4344
by: Kathryn | last post by:
Hiya I have a problem with using some client side and server side scripting together in an ASP. I'm using VBScript. What I'm trying to achieve is this - - Page loads up and some server side vbscript reads the database and populates a listbox on the page with the first field from each record in the recordset. This works fine. - User selects an option on the listbox and, using the OnChange, I
4
9378
by: Arif | last post by:
I C# code prints very slow as compared to a third party barcode printing software. That software prints approximately 10 labels in 2 seconds while my C# code prints 10 labels in 5 to 6 seconds. And this differences increases with the increase number of labels. The code is as follwods: Here rdr = OleDbDataReader Font is Times New Roman, 12pt
7
10069
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if there is already one. C.P.
6
1885
by: hardik | last post by:
hi friends i am very new to java script.if anyone can help me out then i will be thankful. i am using php and mysql in my learning project and i have two list boxes,now whenever user presses first listbox i mean user selects listbox then approprieate records should be come in second listbox.can i do it with javascript? i.e if user selects list item 'class bca' then all the records related to 'class bca' should come to
1
4033
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
1243
by: Iain Wilson | last post by:
I am pulling my hair out trying to print various objects from a .net web page My apologies for cross posting but I need an answer and my previous post has attracted no interest. ASP.Net 2.0 using C# This is a simple web page on our intranet (written as a test) and all I am trying to do is print a document to a named printer. If I use a StreamReader then I get an output to the printer. This
0
1170
by: dotnet2005 | last post by:
Hi Toall , I want to explain you clearly , I am having some panels having some controls CheckBox,ComboBox,Datagridview,Hyperlink,label,Listbox,Picturebox,RadioButt­ on ,Textbox) &(Oval,Line,Rectangle ). with .BorderStyle = BorderStyle.FixedSingle . All these things are generating at runtime
7
2573
by: Iain Wilson | last post by:
I am pulling my hair out trying to print various objects from a .net web page My apologies for cross posting but I need an answer and my previous post has attracted no interest. ASP.Net 2.0 using C# This is a simple web page on our intranet (written as a test) and all I am trying to do is print a document to a named printer. If I use a StreamReader then I get an output to the printer. This
3
3472
by: Sweetiecakes | last post by:
Hi I have an string array containing file paths. I have added them to a ListBox with AddRange(); When I delete an item from the ListBox, how can I delete the same item from the original array? Thanks
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.