|
I populate a combobox as follows:
<code>ComboBox2.Items.Clear()
ComboBox2.Items.AddRange(ScanHistoryList)
ComboBox2.SelectedIndex = 0</code>
ScanHistoryList is a string array.
I want to be able to allow the user to add to/edit the combobox .
How do I do this?
Any help much appreciated - I can't find a property to set
You always pass failure on the way to success.
|
|
|
|
|
I think you need to achive this prgamatilally by traping the events and if the text is changed while drop down event replace the older text with new one and if older text is a blank then add the new Item inthe list.Just try it
|
|
|
|
|
Thanks I'll give that a go
You always pass failure on the way to success.
|
|
|
|
|
Hmmm... I guess you could put a button next to the ComboBox, called Edit, that gets the current SelectedItem in the ComboBox, and put the text of the item into a small form with a TextBox. That form would have a OK and Cancel button. If OK, then the SelectedItem would be replaced with the new text.
|
|
|
|
|
Thanks Dave and Ciacia,
I've solved it.
First thing I did was turn it into a DropDown rather than DropDownList. This allows the users to edit the data
Then I created the following event handlers:
<code> Private Sub ComboBox2_SelectedTextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.TextChanged
textChanged = True
End Sub
Private Sub ComboBox2_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.Leave
If textChanged Then
ComboBox2.Items.Insert(ComboBox2.Items.Count, ComboBox2.Text)
ComboBox2.SelectedIndex = ComboBox2.Items.Count - 1
End If
End Sub
</code>
Note: textChanged is declared as a boolean variable.
What happens then is that when the user leaves the combobox if they have changed any text: this is added to the end of the combobox array collection and this is then made the current selection.
Thanks for your help guys. It's a shame that something which seems so simple can be so tricky!
You always pass failure on the way to success.
|
|
|
|
|
|
I don't see anything wrong with this approach. Quick and easy
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hi All,
Being a dot net beginner, i would like to know about biztalk.
What is biztalk? whats it role and how it can be used?
Please explain with an example.
Thanks.
|
|
|
|
|
|
Sharepoint doesn't have anything to do with the .NET Framework. Read up on it here[^].
d_smita wrote: where and How can i implement it ?
Why would you want to do that when you don't know anything about it?
|
|
|
|
|
How can i make a stopwatch ? (Using VB.NET)
|
|
|
|
|
hope this will help you.
SSK.
|
|
|
|
|
you can also use:
Dim sw As New Stopwatch
sw.start
'do what you want to do
sw.stop
secondspassed = sw.ElapsedMilliseconds/1000
This seems much less complicated than the other way to do
|
|
|
|
|
hi
i want to create a PDF files using a HTML/DOC Source file using vb.net.
can somebody help me?
Thanks
|
|
|
|
|
hope this will help you.
SSK.
|
|
|
|
|
Hey guys, I'm having a little trouble with what I'm trying to do. I think there is just one or two lines of code that need to be entered (to what I already have), but I'm not exatly sure what it is.
What I'm trying to do extract one record from a SQL database, because right now I have the SQL statement in a combobox with visable disabled, and I know that's not correct.
Here is what I was thinking, and if this is totally wrong just let me know.
Sub Temp(ByVal myConnString As String)
Dim mySelectQuery As String = "SELECT MAX(TicketID) AS Expr1 FROM tblTicket"
Dim myConnection As New Data.SqlClient.SqlConnection(myConnString)
Dim myCommand As New Data.SqlClient.SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As Data.SqlClient.SqlDataReader
myReader = myCommand.ExecuteReader()
myReader.Close()
myConnection.Close()
End Sub
The exact bit of data I want to extract is what would be in "Expr1".
I've tried several different things, as in...
Dim Number as Double
...
Number = myReader...
Hopefully there is just a few lines of code that can be entered to extract that data, if anyone can help me I'd really appreciate it.
Thanks,
aqzman
|
|
|
|
|
If you are only ever going to retrieve the first column of the first row (or that the data only has one row, and the row only has one column) then you can use ExecuteScalar() - That will save you faffing around with DataReaders.
Here is an updated snippet:
myConnection.Open()<br />
Dim someValue As Object = myCommand.ExecuteScalar();<br />
myConnection.Close()
Upcoming events:
* Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ...
"I wouldn't say boo to a goose. I'm not a coward, I just realise that it would be largely pointless."
My website
|
|
|
|
|
That worked like a charm!
Thanks a lot for the help!
|
|
|
|
|
I answered your question in the sql forum, please don't cross post and please put more meaningful subject lines in your posts.
Ben
|
|
|
|
|
|
|
Hi, I got a problem that I could not solve when try to read my data from ms access database into the list box. I have the following code:
Under the form public class:
Private WithEvents cnnDonorDb As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\DICDonorDb\DICDornorDb.mdb'; Persist Security Info=False")
I add the following code to form load event to get data from the database to the list box:
Dim cmdOrgType As New OleDbCommand
With cmdOrgType
.Connection = cnnDonorDb
.CommandType = CommandType.Text
.CommandText = "SELECT * FROM tblOrgType.OrgType ORDER BY tblOrgType.OrgType;"
End With
' Open the connection.
cmdOrgType.Connection.Open()
' Execute the command to the return the list of organization type.
cmdOrgType.ExecuteReader() ' HERE IS THE PLACE THAT I GET AN ERROR
Dim drdResult As OleDbDataReader
' Get the list of organization type and put it in the list box.
While drdResult.Read
For colOrgType As Integer = 0 To drdResult.FieldCount - 1
lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
Next
End While
' Close the result.
drdResult.Close()
' Close the connection
cmdOrgType.Connection.Close()
I got an error as the following message:
Could not find file 'C:\Documents and Settings\roathkanel\My Documents\Visual Studio 2005\Projects\DIC Donor Database\DIC Donor Database\bin\Debug\tblOrgType.mdb'.
I really don't understand why the database path in this message is difference from database path in C: drive. As you can see the path in the connection string is C:\DICDonorDb\DICDornorDb.mdb . I'm sure that the file is located in this folder because I didn't type it, i copy the string path and file name into my code editor.
Please help me to solve this problem. I'm using VB 2005.
Thank in advance!!!
|
|
|
|
|
There's a few problems with this...
First, this:
Private WithEvents cnnDonorDb As New System.Data.OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\DICDonorDb\DICDornorDb.mdb';" & _
"Persist Security Info=False")
doesn't need the With Events clause. Well, no unless you're going to handle any of the three events that it exposes. Just about noone does...
Second, in your SQL statement, you specified the name of the database in your table spec. Actually, you treated the name of the table as a database name:
SELECT * FROM tblOrgType.OrgType ORDER BY tblOrgType.OrgType;
assuming that tblOrgType is a table and OrgType is a column in that table:
SELECT OrgType FROM tblOrgType ORDER BY OrgType
Altogether, your code should read more like this: Do NOT hold open a database connection object for the life of your application.
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=""C:\DICDonorDb\DICDornorDb.mdb""; Persist Security Info=False;"
Dim conn As New OldDbConnection(connString)
Dim cmdOrgType As New OleDbCommand
With cmdOrgType
.Connection = cnnDonorDb
.CommandType = CommandType.Text
.CommandText = "SELECT OrgType FROM tblOrgType ORDER BY OrgType"
End With
' Open the connection.
cmdOrgType.Connection.Open()
' Execute the command to the return the list of organization type.
Dim drdResult As OleDbDataReader
drdResult = cmdOrgType.ExecuteReader()
' Get the list of organization type and put it in the list box.
While drdResult.Read
For colOrgType As Integer = 0 To drdResult.FieldCount - 1
lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
Next
End While
' Close the result.
drdResult.Close()
' Close the connection
cmdOrgType.Connection.Close()
This, of course, does NOT mean this is good code. There's no exception handling in your code and it makes a bunch of assumptions where it's running in a "perfect world" where everything work the way it should.
|
|
|
|
|
Hi, thank you for your comment and the previous problem are solved based on your code. But another problem is occure in the above code:
...
cmdOrgType.Connection.Open()
Dim drdResult As OleDbDataReader
drdResult = cmdOrgType.ExecuteReader()
While drdResult.Read ' Here is the problem occure.
For colOrgType As Integer = 0 To drdResult.FieldCount - 1 lsbOrgInfo.Items.Add(drdResult(colOrgType).ToString)
Next
End While
drdResult.Close()
cmdOrgType.Connection.Close()
Here is an error message:
Variable 'drdResult' is used before it has been assigned a value. A null reference exception could result at runtime
I think that the reason of cause the problem because my table do not have any record so the data reader object could not read the value. If it is possible, could you please let me know the sample code to check whether there is an existing record in the table?
Thank in advance!!!
|
|
|
|
|
You can check to see if a DataReader was actually returned with:
drdResult = cmdOrgType.ExecuteReader()
If Not drdResult Is Nothing Then
While ...
|
|
|
|