|
|
You're welcome
|
|
|
|
|
Hi,
I use Visual Basic 2005 from Visual Studio 2005. I read from some artical that TextBox control should have contextmenu property, but I cannot find ContextMenu property (only ContextMenuStrip) in property window, but I can see it from the object attribute like TextBox1.ContextMenu.
Also from ToolBox, I cannot fild ContextMenu control.
Is this nomal?, if now, how can I let it be shown?
Thanks!
|
|
|
|
|
Isn't it called ContextMenuStrip? It is in the 'Menus & ToolBars' group. if you dont see it you can select 'All Windows Forms' group. I guess you are probably looking for 'ContextMenu', it is called ContextMenuStrip.
Hope this helps
|
|
|
|
|
Thanks for reply.
So I should use ContextMenuStrip instead of ContextMenu, right? I saw it.
|
|
|
|
|
|
Hi
I am trying to get the primary field name and the fields that relate to external tables (foreign fields) when i pass in the name of an access table.
something like this:
Private Sub GetPKFK(ByVal myTable as DataTable)
'code here to tell me the PK field and all FK fields in it
'would be great if I also get the names of tables the table/FK's relate to
End Sub
I am able to do this using Typed DataSets for SQL, ie using some very complicated/combination of SQL statements, which obviously can't work with Access.
I read a lot about DataRow.ChildRelation and DataRow.ParentRelations and get codes like this which return nothing when i pass in tablename ...
Private Sub GetPKFK(ByVal myTable as DataTable)
Dim myRelation As DataRelation
Dim arrRows() As DataRow
Dim myRow As DataRow
Dim i As Integer
Dim myColumn As DataColumn
For Each myRelation In myTable.ChildRelations
For Each myRow In myTable.Rows
arrRows = myRow.GetChildRows(myRelation, DataRowVersion.Proposed)
' Print values of rows.
For i = 0 To arrRows.GetUpperBound(0)
For Each myColumn In myTable.Columns
Console.WriteLine(arrRows(i)(myColumn))
Next myColumn
Next i
Next myRow
Next myRelation
End Sub
Thanks for your help
|
|
|
|
|
|
Hi,
I have the below code for transferring file from a client to server. This code works fine but i wanted to know how can i get the file name when i send a file from client to server.when i send a file from client to server a save file dialog will be opened and you i have to write the file name and its extension in order to save it. but i want to get the file name and its extension as it is sent.
//client
Imports System.Threading
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub btBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btBrowse.Click
OpenFileDialog.ShowDialog()
tbFilename.Text = OpenFileDialog.FileName
End Sub
Private Sub btSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSend.Click
Dim filebuffer As Byte()
Dim filestream As Stream
filestream = File.OpenRead(tbFilename.Text)
ReDim filebuffer(filestream.Length)
filestream.Read(filebuffer, 0, filestream.Length)
Dim len As Long = filestream.Length - 1
For i As Long = 0 To len
If i Mod 1000 = 0 Then 'only update UI every 1 Kb copied
ProgressBar1.Value = i * 100 / len
Application.DoEvents()
End If
Next
ProgressBar1.Value = 0
Dim IpHost As IPHostEntry
IpHost = Dns.GetHostByName(tbServer.Text)
Dim clientSocket As New TcpClient(tbServer.Text.ToString, 1234)
Dim networkStream As NetworkStream
networkStream = clientSocket.GetStream()
networkStream.Write(filebuffer, 0, filestream.Length)
End Sub
End Class
///server
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim test = System.Net.IPAddress.Any
Private alSockets As ArrayList
Public Sub listenerThread()
Const portNumber As Integer = 1234
Dim tcpListener As New TcpListener(test, portNumber)
Dim handlerSocket As Socket
Dim thdstHandler As ThreadStart
Dim thdHandler As Thread
tcpListener.Start()
Do
handlerSocket = tcpListener.AcceptSocket()
If handlerSocket.Connected Then
lbConnections.Items.Add( _
handlerSocket.RemoteEndPoint.ToString() + _
" Connected.")
SyncLock (Me)
alSockets.Add(handlerSocket)
End SyncLock
thdstHandler = New ThreadStart(AddressOf _
HandlerThread)
thdHandler = New Thread(thdstHandler)
thdHandler.Start()
End If
Loop
End Sub
Public Sub HandlerThread()
Dim handlerSocket As Socket
handlerSocket = alSockets(alSockets.Count - 1)
Dim networkStream As NetworkStream = New _
NetworkStream(handlerSocket)
Dim blockSize As Int16 = 1024
Dim thisRead As Int16
Dim dataByte(blockSize) As Byte
SyncLock Me
Dim SaveFileName As String = String.Empty
Dim DialogSave As New SaveFileDialog
MessageBox.Show("You have a File", "File transfer", MessageBoxButtons.OK, MessageBoxIcon.Warning)
If MsgBoxResult.Ok Then
Me.Opacity = 100
DialogSave.Filter = "All files (*.*)|*.*|JEPG (*.jpg)|*.jpg"
DialogSave.RestoreDirectory = True
DialogSave.Title = "Where do you want to save the file?"
DialogSave.InitialDirectory = "C:/"
If DialogSave.ShowDialog() = DialogResult.OK Then
SaveFileName = DialogSave.FileName
End If
End If
Dim filestream As Stream
filestream = File.OpenWrite(SaveFileName)
While (True)
thisRead = networkStream.Read(dataByte, _
0, blockSize)
If thisRead = 0 Then Exit While
filestream.Write(dataByte, 0, blockSize.ToString)
End While
filestream.Close()
End SyncLock
lbConnections.Items.Add("File Written")
handlerSocket = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Opacity = 0
Dim IpHost As IPHostEntry
IpHost = Dns.GetHostByName(Dns.GetHostName())
Label1.Text = "IP Address Is " + _
IpHost.AddressList(0).ToString()
alSockets = New ArrayList
Dim thdListener As New Thread(New ThreadStart _
(AddressOf listenerThread))
thdListener.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Opacity = 0
End Sub
End Class
|
|
|
|
|
I would do something like prepend a fixed length block to the beginning of the send from client to server - just the first packet. In the fixed length block you could put the filename, length etc of the file you're going to send. The Server reads the message coming in, strips out the fixed length block to get the filename etc, then continues with the rest of the (packets) as being the contents of the file. Including the length means you can check for having a complete file.
'g'
|
|
|
|
|
Imaging the following:
You have a ListView with some items in it. Using the Control key, you select some items. Then you click a button which iterates through all the items to save them in a text file so they can be retrieved later. However, you also want to remember the items that were selected. This is very simple to do, you just create a ListViewItem object and then you use the Selected propery to check which items are selected and which are not, like so:
For Each Item As ListViewItem In ListView1.Items
If Item.Selected Then
MessageBox.Show(Item.Text & "|" & True & vbCrLf)
Else
MessageBox.Show(Item.Text & "|" & False & vbCrLf)
End If
Next
Then, using a streamreader, i read each line and use the split method to split the line (That's where the | character comes in, its the delimiter) and put both halfs of the line in an array. The first index of the array contains the item text, the second index either True or False. Using an If statement, i check wether the second array index is true or false. Is it true, then select the item. Is it False, don't select it.
All the above works fine and actually is not the problem. The problem is that i need this to work on a ListBox instead rather than a ListView. This is where i get stuck because the ListViewItem datatype does'nt work on ListView items and thus the Selected property is unavailable. I tried using the more generic Object datatype like so:
For Each Item As Object In ListBox1.Items
If Item.Selected Then
MessageBox.Show(Item.Text & "|" & True & vbCrLf)
Else
MessageBox.Show(Item.Text & "|" & False & vbCrLf)
End If
Next
Though the designer does'nt give any errors, but as soon as i press the button, i get this error:
Het openbare lid Selected voor type String is niet gevonden.
For some reason, the error text is in Dutch but basically it says that 'Selected' is not found for the String datatype, which makes sense because listbox items are of type string.
So the baseline question here is:
How do i check which items are selected and which are not without replacing the listbox with a listview?
I hope it makes sense to you.
Thanks.
http://www.themightyatom.nl
|
|
|
|
|
A ListBox holds a SelectedItems [^] collection that you can query;
Dim testBox As ListBox = New ListBox()
testBox.Parent = Me
testBox.Dock = DockStyle.Fill
testBox.SelectionMode = SelectionMode.MultiSimple
Me.Controls.Add(testBox)
Dim i As Integer
For i = 0 to 10
testBox.Items.Add ("test " & i)
Next i
testBox.SelectedItems.Clear()
testBox.SelectedItems.Add(testBox.Items(2))
testBox.SelectedItems.Add(testBox.Items(7))
For Each someItem in testBox.Items
If (testBox.SelectedItems.Contains(someItem)) Then
Debug.WriteLine(someItem & " was selected")
End If
Next someItem
For Each selectedItem In testBox.SelectedItems
Debug.WriteLine(selectedItem & " was selected")
Next
I are Troll
|
|
|
|
|
It's not really working, i get an error saying something about an enumerator and the list being changed (im pretty sure its not).
Also, i can't really use the SelectedItem collection because it would skip over the unselected items. I need to through ALL the items and check which ones are selected and which are not and return both results to me in the form of a messagebox, like so:
For Each someItem in testBox.Items
If testBox.SelectedItems.Contains(someItem) Then
MessageBox.Show(someItem & " is selected")
else
MessageBox.Show(someItem & " is not selected")
End If
Next someItem
[img]http://www.themightyatom.nl/stuff/tma_userbars.png[/img]
|
|
|
|
|
Hi,
ListBox.Items contains objects of arbitrary type (i.e. Object), although most often people just store strings in there.
So you can't call methods that don't exist for those items.
However you can take advantage of SelectedItems somehow. Maybe you could iterate over all items and for each of them check if they are contained in SelectedItems, the collection has a Contains() method for that. Warning: it will trick you if you happen to store value types in the ListBox as they get boxed and will not compare-equal by default.
|
|
|
|
|
Hmmm that actually makes sense, except for your warning.
Im gonna give it a try anyway.
Thanks to both of you.
Though if someone has a better/alternate solution, i'd like to hear about it.
Edit:
This is tough.
For Each Item As String In ListBox1.SelectedItems
ListBox1.SelectedItems.Contains(Item)
Next
This works but it skips over the unselected items. I need to get all the items so i can save them to a file to retrieve them later.
Hmmm, something just came up, let my try something else.
Virtual Space Shuttle Astronaut
modified on Wednesday, August 4, 2010 10:00 AM
|
|
|
|
|
Im having some troubles with this. Here's what i did:
First, i create a List of type string. This list only contains the selected items. This is done like so:
Dim SelectedItemsList As New List(Of String)
For Each SelectedItem As String In ListBox1.SelectedItems
SelectedItemsList.Add(TextBox1.Text)
Next
Now i need to compare the List with the actual listbox list. If item exists in both the list and the listbox, then this item was selected. If its not, then it was an unselected item.
Im trouble figuring that out. How do i check that?
If SelectedItemList = ListBox.Items Then
'Selected
Else
'Not selected
End If
Something like that?
Virtual Space Shuttle Astronaut
|
|
|
|
|
Hi,
1.
when you reply to your own message nobody gets notified, it is only by accident that I saw this. Didn't you get a MessageBox warning you?
2.
you don't need the List at all, this should work:
For Each item As String In ListBox1.Items
If ListBox1.SelectedItems.Contains(item) Then
' do something with the selected item (it should not change the ListBox)
Else
' do something with the unselected item (it should not change the ListBox)
Endif
Next
|
|
|
|
|
I actually do get an error saying that list has changed. It also says something about an enumerator. Does'nt make sense to me.
Here's the code:
For Each item As String In ListBox1.Items
If ListBox1.SelectedItems.Contains(item) Then
MessageBox.Show(item & "|" & True)
Else
MessageBox.Show(item & "|" & False)
End If
Next
I only get one messagebox for the first item, wether its selected or not, but then i get that error.
Virtual Space Shuttle Astronaut
|
|
|
|
|
Hi,
sorry I didn't know this, it seems one cannot iterate both Items and SelectedItems like that at the same time.
I did come up with this code, which works fine:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
Dim s As String = ""
For i As Integer = 0 To ListBox1.Items.Count - 1
Dim item As String = ListBox1.Items(i)
If ListBox1.SelectedItems.Contains(item) Then
s = s & " +" & item
Else
s = s & " -" & item
End If
Next
Label1.Text = s
End Sub
|
|
|
|
|
|
You're welcome.
|
|
|
|
|
Hi, dear all,
I have a project created in VB.net 2005 and I will collect some user input data based on user selection for calculation.
Since the input data number and content are different based on user selection, I have problem about how to layout these input fields.
For example, the SpeedContrlType is a combobox with value 1,2 and 3, for different value, the set of input data is totall different. If user select value 1, I will show data for type 1 and hide data for other 2 types, same for type 2 and 3.
Can you give me some suggenstion how to organize the form for this case? I don't want to show all data at the same time, then if user select type 1, enable type 1 data set and disable other two sets.
Thanks a lot!
|
|
|
|
|
Andraw Tang wrote: Can you give me some suggenstion how to organize the form for this case? I don't want to show all data at the same time, then if user select type 1, enable type 1 data set and disable other two sets.
The easiest option is to put the UI for each type into Panel . Hide panels number 2 & 3 by default, and switch visibility if the user changes the type.
A better choice would be to move it to a UserControl
I are Troll
|
|
|
|
|
Eddy,
Thank you for your reply.
If I use panels to hold data, I should overlap these panels and make one of them visible according to user selection, right?
What did you mean to move it to UserControl? I am new to VB.net, can you give me more explanation? it's best if you can give me some sample.
Thanks again!
|
|
|
|
|
Andraw Tang wrote: If I use panels to hold data, I should overlap these panels and make one of them visible according to user selection, right?
Yup. It would be easier to put them next to each other in the designer, and to move them to the correct location when the form is loaded.
Andraw Tang wrote: What did you mean to move it to UserControl?
A UserControl resembles a blank panel, and is used if you need to build your own controls. Imagine that you're using a Panel over and over again to display a picture and the name of the user. You could copy/paste that code and the panel, or you could create a new control;
Public Class MyPanel
Inherits System.Windows.Forms.UserControl
Public Name As String
End Class
This new control can contain anything you like. There's a walkthrough[^] on MSDN if you want to give it a try
I are Troll
|
|
|
|
|