hi
this is faha here im new to c#
I was actually trying to load different textboxes
by multiple selecting a listbox..
I added items to the listbox and then used a for loop to iterate the listbox and accordingly enter the value in the textbox but it isnt working...Any help greatly appreciated..
lb1.Items.AddRa nge(new string[] {"Engine", "Water", "Air", "Fire" });
LB1 IS THE LIST BOX. This code is in the load method.
In the listbox method i have this code
private void listBox1_Select edIndexChanged( object sender, EventArgs e)
{
lb1 = listb1;
for (int i = 0; i < lb1.SelectedIte ms.Count; i++)
{
tb.Text = lb1.SelectedIte ms[0].ToString();
wt.Text = lb1.SelectedIte ms[1].ToString();
ft.Text = lb1.SelectedIte ms[2].ToString();
at.Text = lb1.SelectedIte ms[3].ToString();
}
This code isnt't working and when it reaches
wt.Text = lb1.SelectedIte ms[1].ToString();
it throws an indexoutofbound exception
however there are 4 items in da listbox.
4 8915 GaryTexmo 1,501
Recognized Expert Top Contributor
That's because you're assuming multiple items are selected, which may not be the case. SelectedItems is populated as a list of the items that are selected, so it's not guaranteed to match the contents of the actual ListBox. For example... your list box contains four items, "Engine", "Water", "Air", and "Fire". If the first two are selected, SelectedItems will only contain those two items. If the 2nd and 4th are selected, SelectedItems will contain only those items. If nothing is selected, SelectedItems will be empty (or potentially null).
Hopefully you get the idea. So with your code there, you can't assume that SelectedItems[0 - 3] exists or even that it will contain the appropriate item. Also note that you're doing that assignment for the number of times as there are selected items.
Instead, you need a way to map the selected item to an actual control, then populate that control with whatever data you like... which is likely the selected item itself.
There are a number of ways you could do this... one you might consider is setting up a Hashtable object where you map a string key to a textbox value, then you can can loop over your selected items and retrieve the appropriate textbox by the string name of the selected item.
Give this a try, or try a solution of your own devising. If you have troubles with the code, post back here with what you've tried and we'll see what we can do to help :)
Hi…
The output form lets me select multiple entries however if the index is either 1 or i. It works by displaying the selected value in the text boxes.
The real problem here is im not able to tell the program to update a particular
Textbox upon a particular selection in the listbox.
I however tried using a switch case to achieve the same but then the problem here is
It executes only the default case.
private void listBox1_Select edIndexChanged( object sender, EventArgs e)
{
lb1 = listb1;
for (int i = 0; i < lb1.SelectedIte ms.Count; i++)
{
string valida = lb1.SelectedIte ms[i].ToString();
switch (valida)
{
case "1":// if (valida == "Engine")
if(lb1.Selected Items[i].ToString() == "Engine")
tb.Text = lb1.SelectedIte ms[i].ToString();
break;
case "2": if (lb1.SelectedIt ems[i].ToString() == "Water")
wt.Text = lb1.SelectedIte ms[i].ToString();
break;
default: if (lb1.SelectedIt ems[i].ToString() == "Air")
Ab.Text = lb1.SelectedIte ms[i].ToString();
break;
}
}
I’m totally new to c# and this is my first project, it would be great if you could suggest a few websites from were I could get help as Im afraid that i do not understand what you mean by hashtable object mapping.
GaryTexmo 1,501
Recognized Expert Top Contributor
You're performing your switch on valida, which is going to be one of "Engine", "Water", etc... but your cases are "1", "2", etc...
Why would you not go.. - switch (valida)
-
{
-
case "Engine":
-
...
-
}
...?
Thanks that's what i needed to do...It works as expected now.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: mathieu cupryk |
last post by:
I have problems with listboxes in the webform2.cs, the textboxes are
working well when I do a click on next.
I am missing something. It works with the textboxes.
Here is the file:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
|
by: John R. |
last post by:
I want to have a listbox that shows a checkbox and a textbox.
I created a user control that has a checkbox and a textbox in it and have
been trying to add it to a listbox but I can't get it to show up for each
item I add to the listbox.
foreach (Column column in columns)
{
ColumnsListBox.Items.Add(column.Name);
ColumnsListBox.Controls.Add(new ColumnListControl(true, column.Name,
|
by: kfrost |
last post by:
I know this is probably simple but I have a C# form and the class for the
form is called sbaSynch. I have a textbox name txtServerName.
I'm creating a class to manipulate XML functions so I added a class to
project and it's named XmlApi().
In the XmlAPI() class I have simple code as following
XmlAPI()
{
string str = "Some Text";
|
by: Edward |
last post by:
I am having a terrible time getting anything useful out of a listbox
on my web form.
I am populating it with the results from Postcode lookup software, and
it is showing the results fine. What I want to do is to allow the
user to click on the row that corresponds to the correct address, and
have the code behind populate the form's Address1, Address2 etc.
controls with the relevant data items.
I put the code for this into the...
|
by: Janaka |
last post by:
Help! I have two ListBox controls on my web form. The first one gets
populated on entry with values from the DB. I then use JavaScript to copy
options from this ListBox to my second one. (I have also tried changing the
second ListBox to an HtmlSelect control) using bog standard JavaScript code
like so where "used" is the name of my <select> control:
used.options = new Option(name, typeId);
This all works fine and I can move across...
| |
by: Ali Chambers |
last post by:
Hi,
I have created a listbox called "dtlist1" on my VB.NET form. I call a
procedure as follows:
Private Sub openfile(flname As String)
dtlist1.Items.Clear()
etc..
|
by: zdrakec |
last post by:
Hello all:
Clearly, I'm not getting it! Here is the scenario:
On a web page, I have two list boxen and a text box. The first listbox
is populated at page load time (if it is not a postback). This listbox
has AutoPostback = True. When the user selects an item from this list,
the second listbox is populated with more items relevant to this
selection. I am using an SQLDataSource web control for this. These
items are headers. I want, when...
|
by: SanMiguel12 |
last post by:
Hi, I have a huge problem which drives crazy for a month... I am
working on a database program and I am using .net2005 and Sql server
2003. In a form I have a listbox and two texboxes and there's also in
my database a table with three sells: "name", "e-mail" and "address".
The listbox shows the "name" and I want when I choose a name, the two
textboxes to show the other two sells ("name","address") that goes for
the name i chose in the...
|
by: jh |
last post by:
I'd like to copy/paste into a listbox during runtime. I can do this for a
textbox but can't figure out how to accomplish this for a listbox. Any
help? Thanks.
|
by: Telinstryata |
last post by:
I have a WPF user control with the following...
a) A ListBox on the left of the screen
b) A TextBox on the right of the screen
c) A ScrollViewer around both
When my mouse is over the TextBox and I use the scrollwheel, the TextBox scrolls. When I scroll to the bottom of the TextBox the ScrollViewer then takes over and scrolls.
When my mouse is over the ListBox and I use the scrollwheel, the ListBox scrolls. When I scroll to the bottom...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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,...
|
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...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |