cancel
Showing results for 
Search instead for 
Did you mean: 

Caixa de dialogo visivel

Former Member
0 Kudos

Boa tarde a todos, estou com um problema quando o usuario clica em um botão do formulario que serve para abrir uma caixa de dialogo com os diretorios da maquina, não estou conseguindo fazer com que ao clicar, essa caixa que é um Windows forms e estou usando a propriedade FolderBrowserDialog() para chama - la no codigo venha para a frente do b1.

Atualmente estou usando o ALT + TAB após o click.

Caso alguem já tenha feito isso e posso me ajudar.

Att,

Vitor Avila (Flag SAP Solution Factory)

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Olá Vitor.

   Ja passei por isso sim, e tem que fazer um "WorkAround" pra resolver isso, tem que criar uma classe de wrapper, para resolver segue abaixo os codes:

Classe UIL.Base.WindowWrapper

using System;

namespace UIL.Base {
  public class WindowWrapper : System.Windows.Forms.IWin32Window {
    private readonly IntPtr HANDLE_WINDOW;

    // Property
    /// <summary>
    /// Gets the handle to the window represented by the implementer.
    /// </summary>
    /// <value></value>
    /// <returns>A handle to the window represented by the implementer.</returns>
    public virtual IntPtr Handle {
      get { return HANDLE_WINDOW; } // get
    }

    // Constructor
    /// <summary>
    /// Initializes a new instance of the <see cref="WindowWrapper"></see> class.
    /// </summary>
    /// <param name="__Handle">the handle.</param>
    public WindowWrapper(IntPtr __Handle) {
      HANDLE_WINDOW = __Handle;
    }
  }
}

Classe UIL.Base.SBOFileDialog

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace UIL.Base {
  public class SBOFileDialog : IDisposable {
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();

    public enum BsTypeDialog {
      td_OPEN_DIALOG = 1,
      td_SAVE_DIALOG = 2
    }

    private readonly FileDialog FILE_DIALOG;

    /// <summary>
    /// Initializes a new instance of the <see cref="SBOFileDialog"></see> class.
    /// </summary>
    /// <param name="__DialogType">DocumentType of the dialog.</param>
    /// <param name="__Filter">the filter.</param>
    public SBOFileDialog(BsTypeDialog __DialogType, string __Filter) {
      switch (__DialogType) {
        case BsTypeDialog.td_OPEN_DIALOG:
          FILE_DIALOG = new OpenFileDialog();
          break;
        case BsTypeDialog.td_SAVE_DIALOG:
          FILE_DIALOG = new SaveFileDialog();
          break;
        default:
          throw new Exception("BsTypeDialogBox Unknow");
      }
      Filter = __Filter; //"txt files (*.txt)|*.txt|All files (*.*)|*.*";
      InitialDirectory = "."; // Último utilizado
    }

    /// <summary>
    /// Gets or sets the name of the file.
    /// </summary>
    /// <value>The name of the file.</value>
    public string FileName {
      get { return FILE_DIALOG.FileName; } // get
      set { FILE_DIALOG.FileName = value; } // set
    }

    /// <summary>
    /// Gets or sets the filter.
    /// </summary>
    /// <value>The filter.</value>
    public string Filter {
      get { return FILE_DIALOG.Filter; } // get
      set { FILE_DIALOG.Filter = value; } // set
    } // Filter

    /// <summary>
    /// Gets or sets the initial directory.
    /// </summary>
    /// <value>The initial directory.</value>
    public string InitialDirectory {
      get { return FILE_DIALOG.InitialDirectory; } // get
      set { FILE_DIALOG.InitialDirectory = value; } // set
    } // InitialDirectory

    // Methods
    /// <summary>
    /// Gets the name of the file.
    /// </summary>
    private void ShowForegroundDialog() {
      IntPtr _Ptr = GetForegroundWindow();
      WindowWrapper _Window = new WindowWrapper(_Ptr);

      FILE_DIALOG.ShowDialog(_Window);
      _Window = null;
    } // GetFileName()

    /// <summary>
    /// Shows the file dialog.
    /// </summary>
    /// <returns></returns>
    public string ShowFileDialog() {
      Thread _ThreadOpenFile = new Thread(ShowForegroundDialog);
      _ThreadOpenFile.SetApartmentState(ApartmentState.STA);

      _ThreadOpenFile.Start();

      // Espera a tela abrir
      while (!_ThreadOpenFile.IsAlive) {
        ;
      }

      Thread.Sleep(1); // Wait a sec more

      // Espera a tela ser fechada
      _ThreadOpenFile.Join();
      _ThreadOpenFile = null;

      return FILE_DIALOG.FileName;
    }

    #region IDisposable Members
    /// <summary>
    /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
    /// </summary>
    public void Dispose() {
      FILE_DIALOG.Dispose();
    }
    #endregion
  }
}

Depois onde você quiser utilizar faça por exemplo:

SBOFileDialog _OpenFileDialog = new SBOFileDialog(SBOFileDialog.BsTypeDialog.td_OPEN_DIALOG, "Arquivos de Retorno (*.ret;*.txt)|*.ret;*.txt|Todos os arquivos (*.*)|*.*");
string _FilePath = _OpenFileDialog.ShowFileDialog();

Atenciosamente,

Rodolpho Brock

Gerente de Projetos

+55 (48) 3953-3318

+55 (48) 9977-8264

Sistema Informática Empresarial

Rod. SC 401 Km 1, 600 - Ed. Alfama 4º Andar Sala 406

88030-000 - Florianópolis - SC

Conheça nossos add-ons:

BankSync - Sincronização Bancária (CNAB)

GetOne - Gestão de Empresas de Transporte para o SAP Business One

   [www.rodocred.com.br]

Answers (1)

Answers (1)

Former Member
0 Kudos

Rodolpho muito obrigado amigo não só resolveu o problema como já me ajudou caso futuramente precise pegar arquivos.

Att,

Vitor Avila (Flag SAP Solution Factory)