Show or Hide other fields on the event of ComboBox SelectedIndexChanged - c#

I have a ComboBox called Number of Candidates and I have 10 other comboBoxes, Upon selecting the number of candidates from the drop down menu that 10 ComboBoxes should be shown or hidden.
For example : I select number of candidates as 3 then ComboBox1 ComboBox2, ComboBox3 should be visible. If I select 2 then only ComboBox1 and ComboBox2 should be visible, rest should be hidden.
I have written below code which works fine for step up like 2 to 3, 3 to 5 like that.. For step down like 4 to 2 it won't work. Could you guys please help me
private void noOfCandidates_SelectedIndexChanged(object sender, EventArgs e)
{
int value = Convert.ToInt16(noOfCandidates.SelectedItem);
if (value == 1)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
}
else if (value == 2)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = true; candidate3lbl.Visible = true;
}
else if (value == 3)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = true; candidate3lbl.Visible = true;
candidateBox3.Visible = true; candidate4lbl.Visible = true;
}
else if (value == 4)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = true; candidate3lbl.Visible = true;
candidateBox3.Visible = true; candidate4lbl.Visible = true;
candidateBox4.Visible = true; candidate5lbl.Visible = true;
}
}

use this code :
private void noOfCandidates_SelectedIndexChanged(object sender, EventArgs e)
{
int value = Convert.ToInt16(noOfCandidates.SelectedItem);
if (value == 1)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = false; candidate3lbl.Visible = false;
candidateBox3.Visible = false; candidate4lbl.Visible = false;
candidateBox4.Visible = false; candidate5lbl.Visible = false;
}
else if (value == 2)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = true; candidate3lbl.Visible = true;
candidateBox3.Visible = false; candidate4lbl.Visible = false;
candidateBox4.Visible = false; candidate5lbl.Visible = false;
}
else if (value == 3)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = true; candidate3lbl.Visible = true;
candidateBox3.Visible = true; candidate4lbl.Visible = true;
candidateBox4.Visible = false; candidate5lbl.Visible = false;
}
else if (value == 4)
{
candidateBox1.Visible = true; candidate2lbl.Visible = true;
candidateBox2.Visible = true; candidate3lbl.Visible = true;
candidateBox3.Visible = true; candidate4lbl.Visible = true;
candidateBox4.Visible = true; candidate5lbl.Visible = true;
}
}
I hope it was useful.

Related

First row is selected by default upon form loading

private void FormatdgvParts(DataGridView d)
{
d.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
d.MultiSelect = false;
d.ReadOnly = true;
d.RowHeadersVisible = false;
d.AllowUserToAddRows = false;
}
private void FormatdgvProds(DataGridView d)
{
d.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
d.MultiSelect = false;
d.ReadOnly = true;
d.RowHeadersVisible = false;
d.AllowUserToAddRows = false;
}
private void Display()
{
dgvParts.AutoGenerateColumns = true;
dgvParts.DataSource = Inventory.AllParts;
dgvProds.AutoGenerateColumns = true;
dgvProds.DataSource = Inventory.Products;
}
public Form1()
{
InitializeComponent();
FormatdgvParts(dgvParts);
FormatdgvProds(dgvProds);
Display();
}
Any ideas on how to not get the first row selected when it loads?
I've tried d.CurrentRow.Selected = false; - threw an exception
d.Rows[0].Selected = false; - threw same exception

Wpf Button IsEnabled not working

i have a method that enabling and disabling button in here.My if-else block should do when enter a number to lbDivide the 'öde', '0' and '00' buttons should be active but only activing öde button.How do i solve this ?
öde = make payment
Kişi Sayısı = How many person?
private void Bol_Click(object sender, RoutedEventArgs e)
{
lbDivide.Text = "0";
btnBol.Opacity = 0.5;
btnBol.IsEnabled = false;
lbPayment.Visibility = Visibility.Hidden;
if (lbDivide.Text == "0")
{
btnQr.Opacity = 0.5;
btnQr.IsEnabled = false;
zero.Opacity = 0.2;
zero.IsEnabled = false;
double_zero.IsEnabled = false;
double_zero.Opacity = 0.2;
}
else
{
btnQr.Opacity = 1;
btnQr.IsEnabled = true;
zero.Opacity = 1;
double_zero.Opacity = 1;
zero.IsEnabled = true;
double_zero.IsEnabled = true;
}
I think I know where the error is.
private void Bol_Click(object sender, RoutedEventArgs e)
{
lbDivide.Text = "0"; /// in this line of code you're basically setting lbDivide.text to be 0 every time the button is clicked, so the else condition will never be met.
btnBol.Opacity = 0.5;
btnBol.IsEnabled = false; /// you're basically disabling the button after the first click.
lbPayment.Visibility = Visibility.Hidden;
if (lbDivide.Text == "0")
{
btnQr.Opacity = 0.5;
btnQr.IsEnabled = false;
zero.Opacity = 0.2;
zero.IsEnabled = false;
double_zero.IsEnabled = false;
double_zero.Opacity = 0.2;
}
else
{
btnQr.Opacity = 1;
btnQr.IsEnabled = true;
zero.Opacity = 1;
double_zero.Opacity = 1;
zero.IsEnabled = true;
double_zero.IsEnabled = true;
}
}
change if(lbDivide.Text == "0") by if(lbDivide.Text.Equals("0"))

String value not changing

Hi, I have code that reads and writes to a COM port. When the program reads from the COM port it searches for a string value and puts it in a variable. After it does this, it again listens to the COM port. I need to write to the COM port and read some new data, but I'm not seeing the value has changing to a new value.
Here is my code:
private void timer1_Tick(object sender, EventArgs e)
{
sq = "777";
if (CommunicationManager.myQ.Count != 0)
{
sq = CommunicationManager.myQ.Dequeue().ToString();
textBox1.Text = sq + textBox1.Text;
buffer = Regex.Match(textBox1.Text, #"\
((.+?)\,15,").Groups[1].Value;
}
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= numtst; i++)
{
listView1.Items[i].BackColor = Color.White;
fl[i] = false;
}
nt = 0;
flon = false;
flag[0] = false;
comm.WriteData("AT\r\n");
wait(700);
if (buffer.lenght == 16)
{
flag[0] = true
}
if (flag[0] == true)
{
flon = true;
CommunicationManager.myQ.Clear();
break;
}
}
if (flon == true)
{
listView1.Items[nt].BackColor = Color.LightGreen;
fl[nt] = true;
}
else
{
listView1.Items[nt].BackColor = Color.Red;
if (flag[76] == true)
{
button1.Enabled = true;
button1.BackColor = Color.Red;
button1.Text = "Test ERROR";
return;
}
}
comm.WriteData("ATT\r\n");
wait(3700);
comm.WriteData("AT4\r\n");
nt = 1;
flon = false;
flag[1] = false;
if (buffer == text4.text)
{
flag[1] = true
}
wait(700);
if (flag[1] == true)
{
flon = true;
CommunicationManager.myQ.Clear();
break;
}
if (flag[76] == true)
{
button1.Enabled = true;
return;
}
}
if (flon == true)
{
listView1.Items[nt].BackColor = Color.LightGreen;
fl[nt] = true;
}
else
{
listView1.Items[nt].BackColor = Color.Red;
if (flag[76] == true)
{
button1.Enabled = true;
button1.BackColor = Color.Red;
button1.Text = "Test ERROR";
return;
}
}
in the second part if (buffer == text4.text) i see only first value of the buffer variable.
i checked in the terminal and all commands working good.

Getting this timer to work in C# (newbie) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I enable this timer in C#?
Im trying to get a little project running. When I use a break point it goes through the code correctly, but when running the program at normal speed it the sequence runs too fast. Im trying to get the traffic lights sequence to change every 1 second. What is wrong with this code? Its a simple sequence of traffic lights, incase your interested :). Newbie project.
}
public int counter = 0;
private void rbStart_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Start();
counter++;
if (counter == 1)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else if (counter == 2)
{
pbRed.Visible = true;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 3)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = true;
}
else if (counter == 4)
{
pbRed.Visible = false;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 5)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else
{
counter = 0;
}
}
private void rbStop_CheckedChanged(object sender, EventArgs e)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
Light_timer.Tick += new EventHandler(rbStart_CheckedChanged);
Light_timer.Interval = 1000;
Light_timer.Stop();
}
}
}
You're hooking up the event handler every time the timer elapses and so on... Try this:
private void Form1_Load(object sender, EventArgs e)
{
Light_timer = new Timer();
Light_timer.Tick += new EventHandler(TimerElapsed);
Light_timer.Interval = 1000;
}
private void TimerElapsed(object sender, EventArgs e)
{
counter++;
if (counter == 1)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else if (counter == 2)
{
pbRed.Visible = true;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 3)
{
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = true;
}
else if (counter == 4)
{
pbRed.Visible = false;
pbAmber.Visible = true;
pbGreen.Visible = false;
}
else if (counter == 5)
{
pbRed.Visible = true;
pbAmber.Visible = false;
pbGreen.Visible = false;
}
else
{
counter = 0;
Light_timer.Stop();
}
}
private void rbStart_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Start();
}
private void rbStop_CheckedChanged(object sender, EventArgs e)
{
Light_timer.Stop();
pbRed.Visible = false;
pbAmber.Visible = false;
pbGreen.Visible = false;
}

Update text in textboxes from selected item in listbox. Listbox works fine

I built a WinForm app that works fine with SQL database stored online. I am now trying to move to asp.net app. After learning everything I needed to build the WinForm app I am frustrated over new learning curve!
This is a basic app that has a listbox with all people in the database. The listbox display the combined NameLast + ‘, ‘ + NameFirst as NameFull showing something like “Smith, John”. When the user selects a person the textboxes should display the related information. Once I get started it should be easy however… How do I build a OnSelectedChanged type command to fill the textboxes. In this example I would have four (4) textboxes: NameFirstTxt, NameMiddleTxt, NameLastTxt and RecordIDTxt.
I have attached code from the WinForm app used during the selected index change event.
Any advice and comments (good or bad) would be helpful. Thank you in advance.
private void peopleDetailsDirectoryLbx_SelectedIndexChanged(object sender, EventArgs e)
{
peopleDirectoryGbx.Text = "People Records : " + peopleDetailsDirectoryLbx.Items.Count.ToString();
DataRowView PeopleRow = peopleDetailsDirectoryLbx.SelectedItem as DataRowView;
if (PeopleRow != null && PeopleRow.Row != null && PeopleRow.Row.ItemArray != null)
{
DataRow row = PeopleRow.Row;
if (row.ItemArray.Count() > 0)
try
{
peopleDetailsIDTxt.Text = Convert.ToString(row["ID"]).ToString();
try
{
peopleDetailsPhotoImg.Load(#"http://www.officertech.com/PeoplePhotoBase/" + peopleDetailsIDTxt.Text + ".jpg");
}
catch
{
peopleDetailsPhotoImg.Load(#"http://www.officertech.com/PeoplePhotoBase/NoPhoto.jpg");
}
if (Convert.ToString(row["Active"].ToString()) == "True")
{
peopleDetailsActiveChk.Checked = true;
}
else
{
peopleDetailsActiveChk.Checked = false;
}
peopleDetailsNameFirstTxt.Text = (row["NameFirst"]).ToString();
peopleDetailsNameMiddleTxt.Text = (row["NameMiddle"]).ToString();
peopleDetailsNameLastTxt.Text = (row["NameLast"]).ToString();
peopleDetailsNameAka1Txt.Text = (row["NameAKA1"]).ToString();
peopleDetailsNameAka2Txt.Text = (row["NameAKA2"]).ToString();
peopleDetailsDobTxt.Text = Convert.ToDateTime(row["DOB"]).ToShortDateString();
peopleDetailsRaceCbx.Text = (row["Race"]).ToString();
peopleDetailsGenderCbx.Text = (row["Gender"]).ToString();
peopleDetailsAddStreetNumTxt.Text = (row["AddStreetNum"]).ToString();
peopleDetailsStreetNameCbx.Text = (row["AddStreetName"]).ToString();
peopleDetailsAddCityTxt.Text = (row["AddCity"]).ToString();
peopleDetailsAddStateTxt.Text = (row["AddState"]).ToString();
peopleDetailsAddZipTxt.Text = (row["AddZip"]).ToString();
peopleDetailsAddCountyTxt.Text = (row["AddCounty"]).ToString();
peopleDetailsDriverLicenseTxt.Text = (row["DriverLicense"]).ToString();
peopleDetailsDriverLicenseStateTxt.Text = (row["DriverLicenseState"]).ToString();
peopleDetailsSSNTxt.Text = (row["SSN"]).ToString();
peopleDetailsTrackingLab.ForeColor = Color.Black;
if (row["Tracking"].ToString() == "True")
{
peopleDetailsTrackingYesRad.Checked = true;
peopleDetailsTrackingLab.ForeColor = Color.Red;
}
else
{
peopleDetailsTrackingNoRad.Checked = true;
peopleDetailsTrackingLab.ForeColor = Color.Black;
}
peopleDetailsPhysIDColorHairCbx.Text = (row["PhysIDColorHair"].ToString());
peopleDetailsPhysIDColorEyesCbx.Text = (row["PhysIDColorEyes"].ToString());
peopleDetailsPhysIDWeightTxt.Text = (row["PhysIDWeight"].ToString());
peopleDetailsPhysIDHeightTxt.Text = (row["PhysIDHeight"].ToString());
peopleDetailsPhysIDScarTattTxt.Text = (row["PhysIDScarTatt"].ToString());
//////
if (Convert.ToString(row["School"].ToString()) == "True")
{
peopleDetailsSchoolRad.Checked = true;
}
else
{
peopleDetailsSchoolRad.Checked = false;
}
//////
if (Convert.ToString(row["Work"].ToString()) == "True")
{
peopleDetailsWorkRad.Checked = true;
}
else
{
peopleDetailsWorkRad.Checked = false;
}
//////
if (Convert.ToString(row["ciAssault"].ToString()) == "True")
{
peopleCIAssaultChk.Checked = true;
peopleCIAssaultChk.ForeColor = Color.Red;
}
else
{
peopleCIAssaultChk.Checked = false;
peopleCIAssaultChk.ForeColor = Color.Black;
}
//////
peopleDetailsSchoolWorkNameTxt.Text = (row["SchoolWorkName"]).ToString();
//////
if (Convert.ToString(row["ciBattery"].ToString()) == "True")
{
peopleCIBatteryChk.Checked = true;
peopleCIBatteryChk.ForeColor = Color.Red;
}
else
{
peopleCIBatteryChk.Checked = false;
peopleCIBatteryChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciBatteryAgg"].ToString()) == "True")
{
peopleCIBatteryAggChk.Checked = true;
peopleCIBatteryAggChk.ForeColor = Color.Red;
}
else
{
peopleCIBatteryAggChk.Checked = false;
peopleCIBatteryAggChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciBatteryDVR"].ToString()) == "True")
{
peopleCIBatteryDVRChk.Checked = true;
peopleCIBatteryDVRChk.ForeColor = Color.Red;
}
else
{
peopleCIBatteryDVRChk.Checked = false;
peopleCIBatteryDVRChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciBatteryLEO"].ToString()) == "True")
{
peopleCIBatteryLEOChk.Checked = true;
peopleCIBatteryLEOChk.ForeColor = Color.Red;
}
else
{
peopleCIBatteryLEOChk.Checked = false;
peopleCIBatteryLEOChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciBurglary"].ToString()) == "True")
{
peopleCIBurglaryChk.Checked = true;
peopleCIBurglaryChk.ForeColor = Color.Red;
}
else
{
peopleCIBurglaryChk.Checked = false;
peopleCIBurglaryChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciChildCrimes"].ToString()) == "True")
{
peopleCIChildCrimeChk.Checked = true;
peopleCIChildCrimeChk.ForeColor = Color.Red;
}
else
{
peopleCIChildCrimeChk.Checked = false;
peopleCIChildCrimeChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciCrimMisch"].ToString()) == "True")
{
peopleCICrimMischChk.Checked = true;
peopleCICrimMischChk.ForeColor = Color.Red;
}
else
{
peopleCICrimMischChk.Checked = false;
peopleCICrimMischChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciDealStolen"].ToString()) == "True")
{
peopleCIDealStolenChk.Checked = true;
peopleCIDealStolenChk.ForeColor = Color.Red;
}
else
{
peopleCIDealStolenChk.Checked = false;
peopleCIDealStolenChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciDisorderly"].ToString()) == "True")
{
peopleCIDisorderlyChk.Checked = true;
peopleCIDisorderlyChk.ForeColor = Color.Red;
}
else
{
peopleCIDisorderlyChk.Checked = false;
peopleCIDisorderlyChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciDrugs"].ToString()) == "True")
{
peopleCIDrugsChk.Checked = true;
peopleCIDrugsChk.ForeColor = Color.Red;
}
else
{
peopleCIDrugsChk.Checked = false;
peopleCIDrugsChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciDUI"].ToString()) == "True")
{
peopleCIDUIChk.Checked = true;
peopleCIDUIChk.ForeColor = Color.Red;
}
else
{
peopleCIDUIChk.Checked = false;
peopleCIDUIChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciDWLSR"].ToString()) == "True")
{
peopleCIDWLSRChk.Checked = true;
peopleCIDWLSRChk.ForeColor = Color.Red;
}
else
{
peopleCIDWLSRChk.Checked = false;
peopleCIDWLSRChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciFraud"].ToString()) == "True")
{
peopleCIFraudChk.Checked = true;
peopleCIFraudChk.ForeColor = Color.Red;
}
else
{
peopleCIFraudChk.Checked = false;
peopleCIFraudChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciHomicide"].ToString()) == "True")
{
peopleCIHomicideChk.Checked = true;
peopleCIHomicideChk.ForeColor = Color.Red;
}
else
{
peopleCIHomicideChk.Checked = false;
peopleCIHomicideChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciLewdLasciv"].ToString()) == "True")
{
peopleCILewdLascivChk.Checked = true;
peopleCILewdLascivChk.ForeColor = Color.Red;
}
else
{
peopleCILewdLascivChk.Checked = false;
peopleCILewdLascivChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciMental"].ToString()) == "True")
{
peopleCIMentalChk.Checked = true;
peopleCIMentalChk.ForeColor = Color.Red;
}
else
{
peopleCIMentalChk.Checked = false;
peopleCIMentalChk.ForeColor = Color.Black;
}
//////
peopleCIProbationLab.ForeColor = Color.Black;
if (row["ciProbation"].ToString() == "True")
{
peopleCIProbationYesRad.Checked = true;
peopleCIProbationLab.ForeColor = Color.Red;
}
else
{
peopleCIProbationNoRad.Checked = true;
peopleCIProbationLab.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciResisting"].ToString()) == "True")
{
peopleCIResistingChk.Checked = true;
peopleCIResistingChk.ForeColor = Color.Red;
}
else
{
peopleCIResistingChk.Checked = false;
peopleCIResistingChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciRobbery"].ToString()) == "True")
{
peopleCIRobberyChk.Checked = true;
peopleCIRobberyChk.ForeColor = Color.Red;
}
else
{
peopleCIRobberyChk.Checked = false;
peopleCIRobberyChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciSexCrime"].ToString()) == "True")
{
peopleCISexCrimeChk.Checked = true;
peopleCISexCrimeChk.ForeColor = Color.Red;
}
else
{
peopleCISexCrimeChk.Checked = false;
peopleCISexCrimeChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciShoplift"].ToString()) == "True")
{
peopleCIShopLiftChk.Checked = true;
peopleCIShopLiftChk.ForeColor = Color.Red;
}
else
{
peopleCIShopLiftChk.Checked = false;
peopleCIShopLiftChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciTheft"].ToString()) == "True")
{
peopleCITheftChk.Checked = true;
peopleCITheftChk.ForeColor = Color.Red;
}
else
{
peopleCITheftChk.Checked = false;
peopleCITheftChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciTraffic"].ToString()) == "True")
{
peopleCITrafficChk.Checked = true;
peopleCITrafficChk.ForeColor = Color.Red;
}
else
{
peopleCITrafficChk.Checked = false;
peopleCITrafficChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciTrespass"].ToString()) == "True")
{
peopleCITrespassChk.Checked = true;
peopleCITrespassChk.ForeColor = Color.Red;
}
else
{
peopleCITrespassChk.Checked = false;
peopleCITrespassChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciWarrants"].ToString()) == "True")
{
peopleCIWarrantsChk.Checked = true;
peopleCIWarrantsChk.ForeColor = Color.Red;
}
else
{
peopleCIWarrantsChk.Checked = false;
peopleCIWarrantsChk.ForeColor = Color.Black;
}
//////
if (Convert.ToString(row["ciWeapons"].ToString()) == "True")
{
peopleCIWeaponsChk.Checked = true;
peopleCIWeaponsChk.ForeColor = Color.Red;
}
else
{
peopleCIWeaponsChk.Checked = false;
peopleCIWeaponsChk.ForeColor = Color.Black;
}
//////
CalcAge_People();
//arrestFieldsFill();
}
catch (Exception ex)
{
MessageBox.Show("Problem Locating Person Information" + ex, "CARDS 2012 | Message Center", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
Make sure the AutoPostback property of ListBox is set to true.
Aspx Code:
<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>1st Person</asp:ListItem>
<asp:ListItem>2nd Person</asp:ListItem>
<asp:ListItem>3rd person</asp:ListItem>
</asp:ListBox>
<br />
FIRST NAME:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
LASTNAME:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
EMAIL:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
Code behind:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
{
// what ever you want to assign to TextBox
this.TextBox1.Text = li.Text + "First Name"; // (row["NameFirst"]).ToString();
this.TextBox2.Text = li.Text + "Last Name";
this.TextBox3.Text = li.Text + "Email";
}
}
}
Hope this Helps.

Categories