Results 1 to 25 of 25

Thread: Play sounds/Using WMP Control in C#

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63

    Play sounds/Using WMP Control in C#

    See my last post!

    here's the code for VB .net

    Code:
                Select Case e.KeyChar
                    Case "1"c, "2"c, "3"c   '...to "0"c
                        ' All valid character for use in Textbox1
                        ' Do Nothing
                    Case Microsoft.VisualBasic.ControlChars.Back
                        ' BackSpace
                        ' Do Nothing
                    Case Else
                        ' Ignore all others
                        e.Handled = True
                End Select
    How to do the same thing in C#??

    Note. I'm migrating all of my VB .net projects to C# now. I wish I can learn to handle the language by doing this and wish this (my asking many questions) not to bring you guys too much problems


    See my last post!
    Last edited by session; May 12th, 2003 at 07:21 PM.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Here:
    Code:
    // Check to see if it is a number, if not, go in and check to see if
    // it is a backspace (char)8.  If not a backspace, set the handled
    // to true.
    
    if(!char.IsNumber(e.KeyChar))
    {			
           if(!(e.KeyChar == (char)8)) 
           {
                 e.Handled = true;
           }
    }

  3. #3

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    hmmmmm... what if i just wanna let users to enter 'some' numbers eg... say, only 1, 2, 3 are allowed? and what if i don't want user to enter *any number but letters? :S

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can add cases together:
    Code:
    case 'a':
    case 'b':
    case 'c':
      // do something
    break;
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    There is also the IsLetter method of the char object.

  6. #6

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    Originally posted by CornedBee
    You can add cases together:
    Code:
    case 'a':
    case 'b':
    case 'c':
      // do something
    break;

    er... what i meant was how to prevent anything other than 1, 2, 3 entered in textbox

  7. #7

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    anyone?

  8. #8
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    PHP Code:
    int key;

    try
    {
        
    key int.Parse(e.KeyChar.ToString());

        switch(
    key)       
        {   
        
    // Just add a return statement below each number you
        // want to accept.
            
    case 1:
               return;
            case 
    2:
               return;
            case 
    3:
               return;
            case 
    4:         
            case 
    5:                    
            case 
    6:            
            case 
    7:         
            case 
    8:                    
            case 
    9:
            case 
    0:
               
    e.Handled true;
               break;
            default:
               
    e.Handled true;
               break;
        }
    }
    catch
    {
        if(
    e.KeyChar == (char)8
        {
            return;
        }
        else
        {
            
    e.Handled true;
            return;
         }

    Try something like this.
    Last edited by hellswraith; May 8th, 2003 at 10:50 PM.

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    thank you for help and it works great! btw i just got your pm after i sent an email to you. im sorry for that.
    Last edited by session; May 8th, 2003 at 11:13 PM.

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Glad it works for you. There is probably more efficient ways though, I didn't take a whole lot of time on it.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can of course bundle the accepted keys together just like the non-accepted. Makes for less returns
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    First off, I wanna make only 1, 2, 3 avaliable when a variable intNumber greater or equal to 3, when intNumber is = or < 2, only 1 and 2 is avaliable to be entered in this textBox. I'm just wondering how do I code for intNumber>=3 in C#?

    Code:
    if(intNumber _____ 3)
    {
         <code>
    }
    else if(intNumber _____ 2)
    {
         <code>
    }
    else
    {
         <code>
    }
    what should I put int the blanks? just put <= or >= ???

    2nd-ly, what should I use in C# to replace Len function in VB .NET?

    3rd,
    i found these somewhere but there's no definition and explanations for those, can anyone explain for me?

    Assignment: = *= /= %= += -= <<= >>= &= ^= |=
    Logical AND &
    Logical XOR ^
    Logical OR |
    Conditional AND &&
    Conditional OR ||
    Multiplicative * / %
    Additive + -
    Shift << >>
    Relational and type testing < > <= >= is as
    And what is the symbol/expression for not equal??? :s

    Finally,
    How do i use Exit Sub in C#?? break?
    Thanks for help!
    Last edited by session; May 9th, 2003 at 07:45 PM.

  13. #13
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    1. <= means less than or equal to
    >= means greater than or equal to
    See number 3 for a lot more information about these and other operators.

    2. By Len, do you mean get the length of a string? Just use the Length property of the string if that is the case. Example:
    string myString = "Hello";
    int stringLength = myString.Length;
    // stringLength should now equal 5

    3. Just a link for you:
    http://msdn.microsoft.com/library/de...poperators.asp

    4. Use return; That will exit the method.

  14. #14

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    aww thanks a lot. you always help me solve problem n they always work

    //edit: but i cant find 'not equal!!!' :s

    //found it. it is!
    Last edited by session; May 10th, 2003 at 09:24 PM.

  15. #15

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    oh btw, could you tell me some more useful function or stuff?

    eg. (in VB/VB .NET)
    InStr
    Mid
    ToLower (LCase)
    ToUpeer (UCase)
    (of the following 3, I believe we use Try... Catch... Finally...)
    On Error GoTo <tag>
    On Error Resume Next
    resume Next
    Date
    Time...
    Some string/variable handle functions and something that you think are basic and I should know?

    Thanks a lot!

  16. #16
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    InStr I believe is now the IndexOf method of the string itself:
    int foundAt = myString.IndexOf("Hello");
    You can add arguments as it is an overloaded method. You can start the search at any position in the string.

    Mid is now SubString() method:
    string myMidString = myWholeString.SubString(startIndex, length);

    ToLower and ToUpper are pretty simple also:
    string myUpperString = myOriginalString.ToUpper();
    string myLowerString = myOriginalString.ToLower();

    You are right, you use try...catch...finally blocks to do error handling.

    To create a datetime variable:
    DateTime myDateTime = DateTime.Now;
    That will get the current date time. You can do a whole lot with the DateTime object, so use the intellisense to look around at its methods and properties.

    The best place to look for information is at:
    www.msdn.microsoft.com/library
    Just do a search, and don't get discouraged if you don't find it right away...it is there...change the terms a little till you get what you need. There is very little that is not there at the msdn library.

  17. #17

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    thank you so much for help. I just added them to myu c# note!

  18. #18

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63

    if and #if

    when i was looking around at msdn, i saw there's an introduction on #if (http://msdn.microsoft.com/library/de...poperators.asp). Just wondering what the differences are between if and #if, else and #else

    A
    Code:
          #if (DEBUG && !VC_V7)
             Console.WriteLine("DEBUG is defined");
          #elif (!DEBUG && VC_V7)
             Console.WriteLine("VC_V7 is defined");
          #elif (DEBUG && VC_V7)
             Console.WriteLine("DEBUG and VC_V7 are defined");
          #else
             Console.WriteLine("DEBUG and VC_V7 are not defined");
          #endif
    B
    Code:
          if (DEBUG && !VC_V7)
             {Console.WriteLine("DEBUG is defined");}
          else if (!DEBUG && VC_V7)
             {Console.WriteLine("VC_V7 is defined");}
          else if (DEBUG && VC_V7)
             {Console.WriteLine("DEBUG and VC_V7 are defined");}
          else
             {Console.WriteLine("DEBUG and VC_V7 are not defined");}
    //What are the differences between A and B
    appreciate your help!
    Last edited by session; May 10th, 2003 at 09:23 PM.

  19. #19
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    #if is a preproccesor directive. When you compile, the preprocessor goes through and evaluates your #if..#endif blocks. For example, consider your example:
    Code:
          #if (DEBUG && !VC_V7)
             Console.WriteLine("DEBUG is defined");
          #elif (!DEBUG && VC_V7)
             Console.WriteLine("VC_V7 is defined");
          #elif (DEBUG && VC_V7)
             Console.WriteLine("DEBUG and VC_V7 are defined");
          #else
             Console.WriteLine("DEBUG and VC_V7 are not defined");
          #endif
    suppose DEBUG is true and VC_V7 is true. Your compiled code will contain only the following statement:
    Code:
    Console.WriteLine("DEBUG and VC_V7 are defined");
    whereas if you wrote it with the standard if...endif blocks, the expressions would be evaluated at run time.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  20. #20

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    so do you mean that #if code will run before run-time :S and if code block is running during run time?

  21. #21
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    correct. #if...#end if is evaluated at compile time.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  22. #22

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    so what can we use #if...#end if? D'we need to do anything in compile time?

  23. #23
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    imagine u have a code that u only want to run in debug mode..for example that has various debug boxes in it..u just put the code for that debug boxes inside of:

    Code:
    #if DEBUG
    code
    ...
    #end-if
    and then when u put the project to compile to RELEASE you'll see as that boxes and things wont appear in your app and you still didnt have to go throgh all the code deleting that code that you later might want again for debuggin reasons
    \m/\m/

  24. #24

    Thread Starter
    Member
    Join Date
    Apr 2003
    Location
    CANADA
    Posts
    63
    Thanks!

    How d'I play a sound in C#?
    I tried to use Windows Media Player control it doesn't seem to work.

    Code:
    			if(dtTimeNow.Hour==Convert.ToInt16(txtHour.Text) && dtTimeNow.Minute==Convert.ToInt16(txtMin.Text))
    			{
    				axWMP.URL="D:\type.wav";
    			}
    and btw, how to change the information for your program (like adding author's name, company and information so when you right click on the exe file, the information will show up?)
    Last edited by session; May 12th, 2003 at 10:37 PM.

  25. #25
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Please post new threads for that stuff.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width