none
I want to adapt or transform from C# code to F#. RRS feed

  • Question

  • Hello:
    I want to adapt or transform from C# code to F#.

    Code C#:

    using System;
    
    namespace radioButton_consola_02_cs
    {
        class Program
        {
            #region Variables.
            private static readonly string[] TEXTO = new string[]
            {
                "( ) Option A        ",
                "( ) Option B        ",
                "( ) Option C        ",
                "    EXIT"
            };
    
            private static int itemSeñalado;
    
            private static int itemSeleccionado;
            #endregion
    
            static void Main(string[] args)
            {
                // Window title.
                Console.Title = "RadioButton";
    
                // Window size.
                Console.SetWindowSize(20, 5);
    
                // Green background.
                Console.BackgroundColor = ConsoleColor.Blue;
    
                // Black letters.
                Console.ForegroundColor = ConsoleColor.White;
    
                // I hide the cursor.
                Console.CursorVisible = false;
    
                // Stores the key pressed in the variable.
                ConsoleKey teclaInicial;
    
                do
                {
                    // Clean screen.
                    Console.Clear();
    
                    // Cursor position of the MAIN MENU title.
                    Console.SetCursorPosition(0, 0);
    
                    // Title.
                    Console.Write("   MENÚ PRINCIPAL   ");
    
                    // Time position.
                    Console.SetCursorPosition(4, 2);
    
                    // Numeric format dd/MM/yyyy.
                    Console.Write(DateTime.Now.ToString("ddd dd MMM"));
    
                    // Stores a pressed key in the variable.
                    teclaInicial = Console.ReadKey(true).Key;
    
                    // Did you press the Enter key?
                    if (teclaInicial == ConsoleKey.Enter)
                    {
                        // Yes. This function is executed.
                        MenuPrincipal();
                    }
                } while (teclaInicial != ConsoleKey.Escape);
            }
    
            #region Main menu.
            private static void MenuPrincipal()
            {
                bool salir = false;
    
                // In itemSelecionado:
                 // -1 = Not selected with * any option.
                 // 0 = Select with * Option A.
                 // 1 = Select with * Option B.
                 // 2 = Select with * Option C.
    
                // Capture key and then validate.
                ConsoleKey tecla;
    
                // Every time you return to the menu, it is marked with the *.
                itemSeñalado = 0;  
    
                do
                {
                    //******************************************************************
                     // I draw the main menu.
    
                     // Clean screen.
                    Console.Clear();
    
                    for (int k = 0; k < TEXTO.Length; k++)
                    {
                        Console.SetCursorPosition(0, k);
                        Console.Write(itemSeñalado == k ? "> " : "  ");
                        Console.Write(TEXTO[k]);
                        Console.SetCursorPosition(3, k);
                        Console.Write(itemSeleccionado == k ? "*" : " ");
                    }
    
                    // End of painting the main menu.
                    //******************************************************************
    
                    // Read key entered by user.
                    tecla = Console.ReadKey(true).Key;
    
                    switch (tecla)
                    {
                        case ConsoleKey.Enter:
                            if (itemSeñalado < 3)
                            {
                                itemSeleccionado = itemSeñalado;
                            }
                            salir = (itemSeñalado == TEXTO.Length - 1);
                            break;
    
                        case ConsoleKey.DownArrow:
                            if (++itemSeñalado >= TEXTO.Length)
                            {
                                itemSeñalado = 0;
                            }
                            break;
    
                        case ConsoleKey.UpArrow:
                            if (--itemSeñalado < 0)
                            {
                                itemSeñalado = TEXTO.Length - 1;
                            }
                            break;
                    }
                    // I use the escape key as output.
                } while (!salir);
            }
            #endregion
        }
    }

    Thank you.


    http://electronica-pic.blogspot.com

    Wednesday, June 17, 2020 8:23 AM

Answers

  • namespace radioButton_consola_02_cs
    
    open System
    
    module Program =
    
      let TEXTO = [
                  "( ) Option A        "
                  "( ) Option B        "
                  "( ) Option C        "
                  "    EXIT"
                  ]
      let sl = TEXTO |> Seq.length
    
      do
        Console.Title <- "RadioButton"
        Console.SetWindowSize(20, 5)
        Console.BackgroundColor <- ConsoleColor.Blue
        Console.ForegroundColor <- ConsoleColor.White
        Console.CursorVisible <- false
    
      let rec private MenuPrincipal itemSeñalado itemSeleccionado =
          Console.Clear()
          TEXTO
          |> List.iteri(fun k line ->
              Console.SetCursorPosition(0, k)
              Console.Write(if itemSeñalado = k then "> " else "  ")
              Console.Write(line)
              Console.SetCursorPosition(3, k)
              Console.Write(if itemSeleccionado = k then "*" else " ")
          )
    
          match Console.ReadKey(true).Key with
          | ConsoleKey.DownArrow ->
            MenuPrincipal
              ((itemSeñalado + 1) % sl)
              itemSeleccionado
          | ConsoleKey.UpArrow ->
            MenuPrincipal
              (if itemSeñalado = 0 then (sl - 1) else itemSeñalado - 1)
              itemSeleccionado
          | ConsoleKey.Enter ->
            if itemSeñalado = sl - 1
            then itemSeleccionado
            else MenuPrincipal itemSeñalado itemSeñalado
          | _ -> MenuPrincipal itemSeñalado itemSeleccionado
    
      let rec private doSetup() =
        Console.Clear()
        Console.SetCursorPosition(0, 0)
        Console.Write("   MENÚ PRINCIPAL   ")
        Console.SetCursorPosition(4, 2)
        Console.Write(DateTime.Now.ToString("ddd dd MMM"))
        match Console.ReadKey(true).Key with
        | ConsoleKey.Enter -> MenuPrincipal 0 (-1)
                               |> ignore // since nothing gets done with it
                              doSetup()
        | ConsoleKey.Escape -> ()
        | _ -> doSetup()
    
      [<EntryPoint>]
      let private main argv =
        doSetup()
        0 // return an integer exit code


    • Marked as answer by Metaconta Thursday, June 18, 2020 2:38 AM
    Wednesday, June 17, 2020 11:48 AM
  • In that case, change

        | ConsoleKey.Enter -> MenuPrincipal 0 (-1)
    to
        | ConsoleKey.Enter -> MenuPrincipal 0 0

    • Marked as answer by Metaconta Thursday, June 18, 2020 2:38 AM
    Wednesday, June 17, 2020 5:29 PM

All replies

  • namespace radioButton_consola_02_cs
    
    open System
    
    module Program =
    
      let TEXTO = [
                  "( ) Option A        "
                  "( ) Option B        "
                  "( ) Option C        "
                  "    EXIT"
                  ]
      let sl = TEXTO |> Seq.length
    
      do
        Console.Title <- "RadioButton"
        Console.SetWindowSize(20, 5)
        Console.BackgroundColor <- ConsoleColor.Blue
        Console.ForegroundColor <- ConsoleColor.White
        Console.CursorVisible <- false
    
      let rec private MenuPrincipal itemSeñalado itemSeleccionado =
          Console.Clear()
          TEXTO
          |> List.iteri(fun k line ->
              Console.SetCursorPosition(0, k)
              Console.Write(if itemSeñalado = k then "> " else "  ")
              Console.Write(line)
              Console.SetCursorPosition(3, k)
              Console.Write(if itemSeleccionado = k then "*" else " ")
          )
    
          match Console.ReadKey(true).Key with
          | ConsoleKey.DownArrow ->
            MenuPrincipal
              ((itemSeñalado + 1) % sl)
              itemSeleccionado
          | ConsoleKey.UpArrow ->
            MenuPrincipal
              (if itemSeñalado = 0 then (sl - 1) else itemSeñalado - 1)
              itemSeleccionado
          | ConsoleKey.Enter ->
            if itemSeñalado = sl - 1
            then itemSeleccionado
            else MenuPrincipal itemSeñalado itemSeñalado
          | _ -> MenuPrincipal itemSeñalado itemSeleccionado
    
      let rec private doSetup() =
        Console.Clear()
        Console.SetCursorPosition(0, 0)
        Console.Write("   MENÚ PRINCIPAL   ")
        Console.SetCursorPosition(4, 2)
        Console.Write(DateTime.Now.ToString("ddd dd MMM"))
        match Console.ReadKey(true).Key with
        | ConsoleKey.Enter -> MenuPrincipal 0 (-1)
                               |> ignore // since nothing gets done with it
                              doSetup()
        | ConsoleKey.Escape -> ()
        | _ -> doSetup()
    
      [<EntryPoint>]
      let private main argv =
        doSetup()
        0 // return an integer exit code


    • Marked as answer by Metaconta Thursday, June 18, 2020 2:38 AM
    Wednesday, June 17, 2020 11:48 AM
  • Hello:

    Everything but one thing works.

    When pressing Enter, the options must have (*) Option A marked with asterisk.

    There is no option marked with *.

    Thanks for everything.

    http://electronica-pic.blogspot.com

    Wednesday, June 17, 2020 2:07 PM
  • In that case, change

        | ConsoleKey.Enter -> MenuPrincipal 0 (-1)
    to
        | ConsoleKey.Enter -> MenuPrincipal 0 0

    • Marked as answer by Metaconta Thursday, June 18, 2020 2:38 AM
    Wednesday, June 17, 2020 5:29 PM
  • Thank you.

    http://electronica-pic.blogspot.com

    Thursday, June 18, 2020 2:39 AM