Listbox

Hi,
How can I select a record at the same row from listbox1 and listbox2 at the same time?

7 Replies

SG Sachin Gaikwad March 9, 2007 11:07 AM UTC



>Hi,
How can I select a record at the same row from listbox1 and listbox2 at the same time?

Hi,
can you please specify the problem in details?
Whatever i have understood from your query is , When you select a record(item) from one listbox , the same item number's record should get selected from another listbox.
if this is your requirement , then on selected Index changed event of listbox1 , you can write

ListBox2.item(itemIndex).selected=true




SN Snow March 14, 2007 02:08 AM UTC



>

>Hi,
How can I select a record at the same row from listbox1 and listbox2 at the same time?

Hi,
can you please specify the problem in details?
Whatever i have understood from your query is , When you select a record(item) from one listbox , the same item number's record should get selected from another listbox.
if this is your requirement , then on selected Index changed event of listbox1 , you can write

ListBox2.item(itemIndex).selected=true




Hi. This is my codes. The items inside my listboxs are data received from the server.

This is an example of what I want.

I have two listbox. I want to do something like when item in listbox1 is selected the corresponding item in listbox2 will also be selected at the same time. But I not sure how to do it. Can anyone help me?

Example:

Listbox1 item contains: 1,2

Listbox2 item contains: hello, bye

I want something like that. when 1 is selected hello will also be selected and when 2 is selected bye is also selected.

Listbox1: | ListBox2:

1 | hello

2 | bye

My Codes:

#Region "Import Libraries"
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Xml
#End Region

Partial Public Class PatientMain
Inherits System.Web.UI.Page
Dim mrnArray As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim reply As New String(Networker.sendAndReceive("" & vbCrLf))
Dim xmlRead As New XmlTextReader(New StringReader(reply))
Dim mrn As String
Try
While xmlRead.Read()
If xmlRead.Name = "patient" Then
mrn = xmlRead.GetAttribute("mrn")
mrnArray.Add(mrn)
PMListBx1.Items.Add(mrn)
PMListBx2.Items.Add(xmlRead.GetAttribute("firstname") & " " & xmlRead.GetAttribute("lastname"))
End If
End While
Catch ex As Exception
Networker.sendAndReceive("" & vbCrLf)
Networker.socket.Close()
End Try
End Sub

Protected Sub PMainBtnProceed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PMBtnProceed.Click
Networker.mrn = mrnArray.Item(PMListBx1.SelectedIndex)
Response.Redirect("~/PatientHistory.aspx")
End Sub

Protected Sub btnOkName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkName.Click
Response.Redirect("~/EditPatientMain.aspx")
End Sub

Protected Sub btnOkMRN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkMRN.Click
Response.Redirect("~/EditPatientMain.aspx")
End Sub

Private Sub PMListBx1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx1.SelectedIndexChanged
'PMListBx2.SelectedIndex = PMListBx1.SelectedIndex
PMListBx2.Items("1").Selected = True
End Sub

Private Sub PMListBx2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx2.SelectedIndexChanged
'PMListBx1.SelectedIndex = PMListBx2.SelectedIndex
PMListBx1.Items("1").Selected = True
End Sub
End Class


KK kk March 14, 2007 04:45 AM UTC

Hi,

here is the code of a example
i hope it will help it out
if not mail me.....

runat="server">
Item1
Item2
Item3
Item4

runat="server">
A
B
C
D







private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ListBox2.SelectedValue=ListBox1.SelectedItem.Value;


}

















>

>

> Hi,
How can I select a record at the same row from listbox1 and listbox2 at the same time?

Hi,
can you please specify the problem in details?
Whatever i have understood from your query is , When you select a record(item) from one listbox , the same item number's record should get selected from another listbox.
if this is your requirement , then on selected Index changed event of listbox1 , you can write

ListBox2.item(itemIndex).selected=true




Hi. This is my codes. The items inside my listboxs are data received from the server.

This is an example of what I want.

I have two listbox. I want to do something like when item in listbox1 is selected the corresponding item in listbox2 will also be selected at the same time. But I not sure how to do it. Can anyone help me?

Example:

Listbox1 item contains: 1,2

Listbox2 item contains: hello, bye

I want something like that. when 1 is selected hello will also be selected and when 2 is selected bye is also selected.

Listbox1: | ListBox2:

1 | hello

2 | bye

My Codes:

#Region "Import Libraries"
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Xml
#End Region

Partial Public Class PatientMain
Inherits System.Web.UI.Page
Dim mrnArray As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim reply As New String(Networker.sendAndReceive("" & vbCrLf))
Dim xmlRead As New XmlTextReader(New StringReader(reply))
Dim mrn As String
Try
While xmlRead.Read()
If xmlRead.Name = "patient" Then
mrn = xmlRead.GetAttribute("mrn")
mrnArray.Add(mrn)
PMListBx1.Items.Add(mrn)
PMListBx2.Items.Add(xmlRead.GetAttribute("firstname") & " " & xmlRead.GetAttribute("lastname"))
End If
End While
Catch ex As Exception
Networker.sendAndReceive("" & vbCrLf)
Networker.socket.Close()
End Try
End Sub

Protected Sub PMainBtnProceed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PMBtnProceed.Click
Networker.mrn = mrnArray.Item(PMListBx1.SelectedIndex)
Response.Redirect("~/PatientHistory.aspx")
End Sub

Protected Sub btnOkName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkName.Click
Response.Redirect("~/EditPatientMain.aspx")
End Sub

Protected Sub btnOkMRN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkMRN.Click
Response.Redirect("~/EditPatientMain.aspx")
End Sub

Private Sub PMListBx1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx1.SelectedIndexChanged
'PMListBx2.SelectedIndex = PMListBx1.SelectedIndex
PMListBx2.Items("1").Selected = True
End Sub

Private Sub PMListBx2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx2.SelectedIndexChanged
'PMListBx1.SelectedIndex = PMListBx2.SelectedIndex
PMListBx1.Items("1").Selected = True
End Sub
End Class


SN Snow March 14, 2007 05:33 AM UTC

Hi kapil Dhawan,
The code you given doesn't work either. Listbox2 item1 just didn't get selected when i click on item1 in listbox1. Did you tried using the code you gave me and did it succeed?

>Hi,

here is the code of a example
i hope it will help it out
if not mail me.....

runat="server">
Item1
Item2
Item3
Item4

runat="server">
A
B
C
D







private void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ListBox2.SelectedValue=ListBox1.SelectedItem.Value;


}

















>

>

>Hi,
How can I select a record at the same row from listbox1 and listbox2 at the same time?

Hi,
can you please specify the problem in details?
Whatever i have understood from your query is , When you select a record(item) from one listbox , the same item number's record should get selected from another listbox.
if this is your requirement , then on selected Index changed event of listbox1 , you can write

ListBox2.item(itemIndex).selected=true




Hi. This is my codes. The items inside my listboxs are data received from the server.

This is an example of what I want.

I have two listbox. I want to do something like when item in listbox1 is selected the corresponding item in listbox2 will also be selected at the same time. But I not sure how to do it. Can anyone help me?

Example:

Listbox1 item contains: 1,2

Listbox2 item contains: hello, bye

I want something like that. when 1 is selected hello will also be selected and when 2 is selected bye is also selected.

Listbox1: | ListBox2:

1 | hello

2 | bye

My Codes:

#Region "Import Libraries"
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Imports System.Xml
#End Region

Partial Public Class PatientMain
Inherits System.Web.UI.Page
Dim mrnArray As New ArrayList
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim reply As New String(Networker.sendAndReceive("" & vbCrLf))
Dim xmlRead As New XmlTextReader(New StringReader(reply))
Dim mrn As String
Try
While xmlRead.Read()
If xmlRead.Name = "patient" Then
mrn = xmlRead.GetAttribute("mrn")
mrnArray.Add(mrn)
PMListBx1.Items.Add(mrn)
PMListBx2.Items.Add(xmlRead.GetAttribute("firstname") & " " & xmlRead.GetAttribute("lastname"))
End If
End While
Catch ex As Exception
Networker.sendAndReceive("" & vbCrLf)
Networker.socket.Close()
End Try
End Sub

Protected Sub PMainBtnProceed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PMBtnProceed.Click
Networker.mrn = mrnArray.Item(PMListBx1.SelectedIndex)
Response.Redirect("~/PatientHistory.aspx")
End Sub

Protected Sub btnOkName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkName.Click
Response.Redirect("~/EditPatientMain.aspx")
End Sub

Protected Sub btnOkMRN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOkMRN.Click
Response.Redirect("~/EditPatientMain.aspx")
End Sub

Private Sub PMListBx1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx1.SelectedIndexChanged
'PMListBx2.SelectedIndex = PMListBx1.SelectedIndex
PMListBx2.Items("1").Selected = True
End Sub

Private Sub PMListBx2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx2.SelectedIndexChanged
'PMListBx1.SelectedIndex = PMListBx2.SelectedIndex
PMListBx1.Items("1").Selected = True
End Sub
End Class


PJ Poornima Joy Balan Syncfusion Team March 22, 2007 06:25 AM UTC

Hi Snow,

Here is the code of a example. You have to set the AutoPostBack property to True for both listboxes. I hope it will help it out.














Protected Sub PMListBx1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx1.SelectedIndexChanged
PMListBx2.SelectedValue = PMListBx1.SelectedItem.Value
End Sub

Protected Sub PMListBx2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles PMListBx2.SelectedIndexChanged
PMListBx1.SelectedValue = PMListBx2.SelectedItem.Value
End Sub

Please let us know if you have any further queries.


Regards,
Poornima


SG Sachin Gaikwad March 23, 2007 05:06 AM UTC

Hi Poornima ,
The solution you have given will work but for that you will have to set AUTOPOSTBACK property of the listbox TRUE , and the action will be happened at server side.
i think to do such a simple thing at server side is not feasible and also its not good practice.
Try to use Javascript and limit this application at client side only to save execution time of the functionality.
You can use the function for the same as below-



after writing this function , just add one attribute to the ListBox1 at pageload event of the webform.
That Code would be as below-
lstBox1.attributes.add

("OnselectedIndexChanged","Javascript:Func_SelectItem();")

This will solve your problem , and code wont go to the server side.That means it will also take less execution time that your solution.


HA Harpreet April 26, 2007 12:28 PM UTC

hi,
good code you gave but its not working on OnselectedIndexChanged attribute. I tried with onChange attribute and it is working so replace add attribute on page load as:

ListBox1.Attributes.Add("onChange", "Javascript:Func_SelectItem();");


>Hi Poornima ,
The solution you have given will work but for that you will have to set AUTOPOSTBACK property of the listbox TRUE , and the action will be happened at server side.
i think to do such a simple thing at server side is not feasible and also its not good practice.
Try to use Javascript and limit this application at client side only to save execution time of the functionality.
You can use the function for the same as below-



after writing this function , just add one attribute to the ListBox1 at pageload event of the webform.
That Code would be as below-
lstBox1.attributes.add

("OnselectedIndexChanged","Javascript:Func_SelectItem();")

This will solve your problem , and code wont go to the server side.That means it will also take less execution time that your solution.

Loader.
Up arrow icon