list box

255 pts.
Tags:
VB.NET 2005 Express Edition
In list box i have two items,one is teaher other is student.I created the listbox. If i selet student some code should be displayed,if i select teacher another code should be displayed .how to do it. iam giving my program below and tell me also if i insert values it has store in database. Public Class Adimission Private Sub Page_Init(ByVal Sender As Object, ByVal e As System.EventArgs) End Sub Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click End Sub Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click End Sub Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click End Sub Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged End Sub Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged End Sub Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged End Sub Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged End Sub Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click End Sub Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click End Sub Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click End Sub Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged End Sub Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged End Sub Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged End Sub Private Sub Label13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label13.Click End Sub Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub Adimission_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged TextBox1.Text = ListBox1.SelectedItem End Sub End Class Thanks For helping me shaz

Answer Wiki

Thanks. We'll let you know when a new response is added.

Your question is not clear enough.

With this code, you are displaying the selected value from the list box in the text box:

<pre>Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedItem
End Sub</pre>

If you want to display some other thing depending on the selection, you could do something like this:

<pre> Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedIndex = 0 Then
TextBox1.Text = “display something related to the first item in the list”
Else
TextBox1.Text = “something else”
End If</pre>

or this

<pre> Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem.ToString = “Teacher” Then
TextBox1.Text = “display something related to the Teacher item”
Else
TextBox1.Text = “something else”
End If
End Sub</pre>

—————–

You can create forms at runtime, but you will need to write more code. Why do you need to do it this way ?

Here is an example. Here I create a form and add a couple of controls in the click event of a button:

<pre>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f1 As Form
Dim t1 As TextBox
Dim b1 As Button

‘ creating the form
f1 = New Form

‘ creating the button
b1 = New Button
b1.Text = “test button”
b1.Location = New Point(75, 25)
‘ adding a handler to the button’s click event
AddHandler b1.Click, AddressOf b1_Click

‘ creating a textbox
t1 = New TextBox
t1.Size = New Size(200, 20)
t1.Location = New Point(75, 65)

‘ adding the controls to the form
f1.Controls.Add(b1)
f1.Controls.Add(t1)

‘ showing the form
f1.Show()

End Sub

Private Sub b1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
MsgBox(“Button clicked”)
End Sub
</pre>

As for how to insert the data into a database table, have a look at this example:

<a href=”http://www.java2s.com/Code/VB/Database-ADO.net/InsertdatausingSqlCommand.htm”>Insert data using SqlCommand</a>

Discuss This Question: 8  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Shaz
    Thanks For Replying Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged If ListBox1.SelectedItem.ToString = "Teacher" Then TextBox1.Text = "display something related to the Teacher item" Else TextBox1.Text = "something else" End If End Sub The above has given me hint but not actuall solution.I dont have textbox .I have only listbox .In that i select teacher then the ollowing code should be displayed ,how to do it Private Sub Page_Init(ByVal Sender As Object, ByVal e As System.EventArgs) End Sub Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click End Sub Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click End Sub Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click End Sub Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged End Sub Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged End Sub Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged End Sub Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged End Sub Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click End Sub Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click End Sub Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click End Sub Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged End Sub Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged End Sub Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged End Sub Private Sub Label13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label13.Click End Sub Private Sub Adimission_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub After displayng ,then i have enter the values in textbox ,means i have admission displayed by selecting teaher ,then i have enter the data which has to store in database.Give me solution. Thanks alot for helping me.
    255 pointsBadges:
    report
  • carlosdl
    I'm finding really hard to understand you questions... What code do you want to display, and where do you want to display it ? Do you want to display that program code you posted ? what type of item do you want to display it in ? Also, I don't understand this part, could you try to get some help re-wording it ? "After displayng ,then i have enter the values in textbox ,means i have admission displayed by selecting teaher ,then i have enter the data which has to store in database" You said you didn't have textbox... As for the database part, have you already connected to the database ? do you know how to do that ? what database are you using ?
    69,045 pointsBadges:
    report
  • Shaz
    Dear sir, I have one listbox ,in that i have teaccher,student as option ,If i select student a student admission form should be displayed.the code which i has given to u before is student admission form .the code of admission form is as follows Private Sub Page_Init(ByVal Sender As Object, ByVal e As System.EventArgs) End Sub Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click End Sub Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged End Sub Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click End Sub Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.Click End Sub Private Sub Label5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label5.Click End Sub Private Sub Label6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label6.Click End Sub Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged End Sub Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged End Sub Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged End Sub Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged End Sub Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click End Sub Private Sub Label8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label8.Click End Sub Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click End Sub Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox7.TextChanged End Sub Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged End Sub Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox9.TextChanged End Sub Private Sub Label13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label13.Click End Sub Private Sub Adimission_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Then i how to write this code in list box coding then if select teacher then teacher form should be displayed. how to do it i have connected sql database. Plz guide me thanks shaz
    255 pointsBadges:
    report
  • Shaz
    Dear Carlosdl, I know to Display different form by list box .It is as follows Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged If ListBox1.SelectedItem.ToString = "Student" Then SAdmission.Show() Else TAdmission.Show() End If End Sub I have Admission as main form.In that i have list box whih is given above.By selecting the forms are coming.But i want to write code in such way whithout Creating new forms,we have write whole code in one main form ,Admission form .Is it possible.Second one is now i got student form by selecting,then i will enter values those values should be stored in database how to do it.iam using sql as database. Thanking u shaz
    255 pointsBadges:
    report
  • carlosdl
    Yes, you can create forms at runtime, but you will need to write more code. Why do you need to do it this way ? Here is an example. Here I create a form and add a couple of controls in the click event of a button:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f1 As Form
            Dim t1 As TextBox
            Dim b1 As Button
    
            ' creating the form
            f1 = New Form
    
            ' creating the button
            b1 = New Button
            b1.Text = "test button"
            b1.Location = New Point(75, 25)
            ' adding a handler to the button's click event
            AddHandler b1.Click, AddressOf b1_Click
    
            ' creating a textbox
            t1 = New TextBox
            t1.Size = New Size(200, 20)
            t1.Location = New Point(75, 65)
    
            ' adding the controls to the form
            f1.Controls.Add(b1)
            f1.Controls.Add(t1)
    
            ' showing the form
            f1.Show()
    
        End Sub
    
        Private Sub b1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs)
            MsgBox("Button clicked")
        End Sub
    
    As for how to insert the data into a database table, have a look at this example: Insert data using SqlCommand
    69,045 pointsBadges:
    report
  • Shaz
    Thanks for helping me. But i have one more problem.Before debugging sql onnecction will be open, when i start debugging the sql connectio n will be closed.It shows an error that login failed The following is login form code and the other is Student admission code plz check and tell me,wht is wrong. Imports System.Data Imports System.Data.SqlClient Public Class login Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim myConnection As SqlConnection myConnection = New SqlConnection("Data Source=Microsoft SQL Server;AttachDbFilename=c:Documents and SettingsShaziaMy DocumentsVisual Studio 2005ProjectscollegecollegeDatabase1.mdf;Integrated Security=True; Connect Timeout=30;User Instance=True;") Dim myCommand As SqlCommand myCommand = New SqlCommand("SELECT [User Name],[Password] FROM Users WHERE [User Name] = @admin AND [Password] = @password", myConnection) Dim uName As New SqlParameter("@admin", SqlDbType.NVarChar) Dim uPassword As New SqlParameter("@password", SqlDbType.NVarChar) uName.Value = UsernameTextBox.Text uPassword.Value = PasswordTextBox.Text myCommand.Parameters.Add(uName) myCommand.Parameters.Add(uPassword) myCommand.Connection.Open() Dim myReader As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection) Dim Login As Object = 0 If myReader.HasRows Then myReader.Read() Login = myReader(Login) End If If Login = Nothing Then MessageBox.Show("Sorry, username/password details were incorrect") Else MessageBox.Show("Login successful") Me.Hide() Index.Show() End If End Sub Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click Me.Close() End Sub Private Sub login1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub UsernameLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UsernameLabel.Click End Sub End Class The other one Student Admission Form code Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Data.SqlClient Public Class SAdmission Shared WithEvents con As SqlConnection Shared Sub Main() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Me.Hide() Index.Show() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click con = New SqlConnection("Server=(Microsoft SQL Server ;Initial Catalog=Visual Studio 2005ProjectscollegecollegeDatabase1.mdf;Integrated Security=SSPI") Dim cmd As New SqlCommand() cmd.CommandText = "Insert into Sadmission(Registration No,Name,Date of Birth,Class,Nationality,Mother Tongue,Religion,Blood Group,Second Language,Address1,Address2,Phone No,Date of Joining,Fathers Name,Occupation,Mothers Name,Occupation1)values(" 'Registration No','Name','Date of Birth','Class','Nationality','Mother Tongue','Religion','Blood Group','Second Language','Address1','Address2','Phone No','Date of Joining','Fathers Name','Occupation','Mothers Name','Occupation1'")" cmd.Connection = con Try con.Open() cmd.ExecuteNonQuery() Finally con.Close() End Try End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Me.Close() End Sub End Class Thanking U. shaz
    255 pointsBadges:
    report
  • carlosdl
    What is the exact error message ?
    69,045 pointsBadges:
    report
  • Shaz
    An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) Plz give the solution iam really tried .I saw the forums also iam unable to clear the error .Plz help me fast
    255 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following