Command to auto remove item from combobox if item is blank (please help!)

xjamesfx

Member
Joined
Apr 3, 2011
Messages
12
Programming Experience
Beginner
Hi,
I have used the command:
For Each Proc as process in process.getprocesses
Combobox1.Items.Add(Proc.MainWindowTitle)
to populate a combo box with the list of current process windows, however for every process that doesnt have a
main window title there is a blank space, is there a way that I could tell it to not insert the item if it's
mainwindowtitle field is blank?
Thanks
 
It would be a simple matter of using an If ... Then statement.

VB.NET:
For Each Proc as process in process.getprocesses
[B]If Proc.MainWindowTitle.IsNullOrWhiteSpace Then[/B]
Combobox1.Items.Add(Proc.MainWindowTitle)
[B]End If [/B]
Next Proc
 
Thanks for reply!
However when i insert it it prompts me to change Proc.MainWIndowTitle to String.IsNullOrW...
but when I do that I get
"Error 1 Argument not specified for parameter 'value' of 'Public Shared Function IsNullOrWhiteSpace(value As String) As Boolean'."
 
Oh, stupid me. That's the wrong way round for the blank string check.
it should be
VB.NET:
if String.IsNullOrWhiteSpace(Proc.MainWindowTitle) then
instead of
VB.NET:
If Proc.MainWindowTitle.IsNullOrWhiteSpace Then

Very sorry.
 
Sorry, I think I must be doing something wrong. When I input your code and start debugging, the combobox goes completly blank. Im fairly new to VB coding so it may just be a silly mistake on my behalf. Heres what ive got:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

For Each Proc As Process In Process.GetProcesses
If String.IsNullOrWhiteSpace(Proc.MainWindowTitle) Then
ComboBox1.Items.Add(Proc.MainWindowTitle)
End If
Next
End Sub
 
You want it to only add if the title isn't blank? Then try

If Not String.IsNullOrWhiteSpace(Proc.MainWindowTitle) Then
 
Hi,
That worked great thanks!
YOu wouldnt happen to know how to make the Name of the selected process in combobox1 appear in a text box would
you?
For example, if you selected 'Untitled - Notepad' in the combobox the corresponding name of the process in
this case 'Notepad' would appear in textbox1
Cheers.
 
Given that the text box is called textbox1, this code should do what you describe.

VB.NET:
    Dim ProcessName(Process.GetProcesses().Count) As String ' Declares an array (list) of process names
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ProcessNameIndex As Integer = 0 ' identifies which process we're assigning
        For Each Proc As Process In Process.GetProcesses
            If Not String.IsNullOrWhiteSpace(Proc.MainWindowTitle) Then ' checks that the process has a title.
                ComboBox1.Items.Add(Proc.MainWindowTitle) ' assigns the title to a row in the combobox
                ProcessName(ProcessNameIndex) = Proc.ProcessName ' Assigns the process name to an array entry
                ProcessNameIndex += 1 ' Moves onto the next process in the array.
            End If
        Next Proc
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As  System.Object, ByVal e As System.EventArgs) Handles  ComboBox1.SelectedIndexChanged ' Updates the text box when the combobox  changes 
        TextBox1.Text = ProcessName(ComboBox1.SelectedIndex) ' Changes the text box to the name.
    End Sub

If the comments don't explain it well enough I can explain how different things work.
Not knowing what you actually want to accomplish, I don't know whether this is the best way to go about it. For example it might be better to use an array of process objects rather than an array of processnames if you'll be doing much more to them.
 
Back
Top