• Welcome to Valhalla Legends Archive.
 

[VB]Creating Whisper Windows

Started by MailMan, March 25, 2003, 04:18 PM

Previous topic - Next topic

MailMan

So far I've got the simple part done. I created a whisper window form, and when I recieve a whisper, I did something along the lines of:


Dim whisper_window as new WhisperForm
With whisper_window
        .caption = from_this_person
        .show
End with


My question is, is there a way to check your currently open forms to see if there's already an open window for that user. I set the caption of each form to the name of the person whispering me, so I could search for the forms or something.. but I'm not sure how.

Thanks in advance.

Camel

dim whisperwindows() as WhisperForm
...
whisperwindows(x) = new WhisperForm

Banana fanna fo fanna

Add all your whisperforms to a Collection, with their key as the username. Search the Collection for the username, if it doesn't exist, add a new one, otherwise get it from the Collection and dispatch the event.

Etheran

#3
inefficient!!!  But definatly practical and easy.  :)

CupHead

Rather than waste more memory with an extra array or collection, you can use VB's predefined collection Forms and cycle through that.  My whisper window code looks something like this:


   Dim j As Integer
   If frmWhisper.Count = 0 Then Call CreateWWindow(Username)
   For j = 0 To Forms.Count - 1
       If Forms(j).Tag = Username Then
           'Forms(j) is the form that already exists with that user's whispers.
       End If
   Next j
   Call CreateWWindow(Username)
   j = Forms.Count - 1

MailMan


Camel

Quote from: CupHead on March 25, 2003, 08:09 PM

   If frmWhisper.Count = 0 Then Call CreateWWindow(Username)


you don't really need that line; if frmWhisper.Count is 0, it will jump over the for looa and call that function _again_

CupHead

#7
Quoteyou don't really need that line; if frmWhisper.Count is 0, it will jump over the for looa and call that function _again_

Actually, idiot, the Forms collection contains *all* available forms therefore no, it will cycle through the loop for each available form including those that are not whisper windows.  By adding that check before the loop, I can avoid having to cycle through when the program knows that no frmWhispers exist.

Anyway, for Camel, quick VB lesson:

  Dim j As Integer
   If frmWhisper.Count = 0 Then Call CreateWWindow(Username)  '#1
   For j = 0 To Forms.Count - 1
       If Forms(j).Tag = Username Then
           'Forms(j) is the form that already exists with that user's whispers.  #2
       End If
   Next j
   Call CreateWWindow(Username)  '#3
   j = Forms.Count - 1


Ok, we have line #1 because if no frmWhispers exist, then we absolutely have to create one for this new whisper.  By creating it before the loop, we allow the loop to find it and add the whisper to it without having to cycle through all of the loaded forms.  See line #2?  That's where you'd jump past the second window creation (line #3) since a form was found that you want to add your text to.  Line #1 provides a single use instance that prevents the program from having to look through all available forms.

Edit: Running the loop from Forms.Count - 1 to 0 Step -1 is actually going to be faster than running from 0 to Forms.Count - 1, if anyone implements this.