Explorerpfad in TextBox zurückschreiben
Michael Hubenschmid
Hallo zusammen:Ich möchte gern per Windows Explorer Dialog ienen ausgewählten Pfad in eine Textbox zurückschreiben. Leider schreibt es mir den ausgewählten Pfad nicht gleich ins Feld zurück sondern erst wenn ich den Button zum Öffnen des Windows Explorer ein 2. Mal betätige.
So viel ich herausgefunden habe liegt es am asynchronen Aufruf des Datei Explorer. D.h. mein restlicher Code läuft weiter nachdem ich den Explorer geöffnet habe. Es wird also nicht gewartet bis etwas im Explorer ausgewählt wurde. Wie kann ich dies umgehen. Habe zur Veranschaulichung schon mal meinen Code angehängt.
Extended Code:
private System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
public string filePath = "";
public static bool IsPathValid(string userPath)
{
try
{
//GetFullPath aufrufen um eine Exception zu erhalten, wenn Pfad ungültig
Path.GetFullPath(userPath);
return true;
}
catch(System.Exception exe)
{
MessageBox.Show("Der eingegebene Pfad ist nicht gültig!", "OK");
return false;
}
}
public void ShowDialog()
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Open));
t.TrySetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
}
private void Open()
{
WindowWrapper sap = new WindowWrapper ();
if (dialog.ShowDialog(sap) == System.Windows.Forms.DialogResult.OK)
{
filePath = dialog.FileName.ToString();
}
//SwissAddonFramework.UI.Dialogs.MessageBox.Show(filePath,"Aha");
}
internal class WindowWrapper : System.Windows.Forms.IWin32Window
{
[DllImport("user32.dll")]
protected static extern int GetForegroundWindow();
public WindowWrapper ()
{
_hwnd = (IntPtr)GetForegroundWindow();
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
New Menu:
...
// EINGABEFORM HANDLER (WENN EXPLORER-BUTTON GEDRÜCKT WURDE)
SwissAddonFramework.UI.EventHandling.ItemEvents.ItemPressedEventHandler btn3PressedHandler = null;
btn3PressedHandler = delegate(SwissAddonFramework.UI.EventHandling.ItemEvents.ItemPressed ip)
{
try
{
ShowDialog();
txt1.Value = filePath;
}
catch (System.Exception exe)
{
MessageBox.Show("FEHLER beim Öffnen des Datei-Explorer: n"+ exe.Message, "OK");
}
};
btn3.AddHandler_ItemPressed(SwissAddonFramework.UI.Components.ModeComponent.FormModes.ALL,btn3PressedHandler);
...
Bin für jeden Rat und Tip dankbar.
Michael Hubenschmid
Michael Egloff
Hallo Michael,du kannst den OpenDialog vom coresuite framework benutzen, der handhabt den windows wrapper schon.
SwissAddonFramework.Utils.Windows.OpenFileDialog ofd = new SwissAddonFramework.Utils.Windows.OpenFileDialog();
ofd.FileSetEvent += new SwissAddonFramework.Utils.Windows.OpenFileDialog.FileSetHandler(delegate(string path)
{
// hier kannst du dein Feld mit dem Pfad füllen
ExtendedTextEdit.GetFromUID(pVal.Form, "21").Value = path;
});
ofd.ShowDialog();
Michael Hubenschmid
Hallo Michael,danke für deinen Tipp, nur weiss ich nun nicht ob ich etwas ersetzen muss mit deinem Copde im Extended Code oder unter New Menu. Vieleicht kannst du mir das nochmal genauer erklären.
Vielen Dank
Michael
Michael Egloff
Hallo Michael,du kannst alles andere löschen, dieser Code hier sollte dein Problem lösen (extended Code brauchst du gar nicht).
Du musst nur noch den Pfad in das von dir gewünschte Feld zurückschreiben.
Attachment
[url=http://www.coresystems.ch/wp-content/../wp-content/forum-file-uploads/michael-egloff/explorerpfad.cocu]explorerpfad.cocu[/url]
Daniel Gesk
Hallo zusammen,ich habe die Lösung ausprobiert. Das Dialogfenster von Windows öffnet sich, jedoch kann ich nur
Dateien und [u]keine Ordner[/u] auswählen. Wasc mache ich falsch? Hier der code:
SwissAddonFramework.Utils.Windows.OpenFileDialog ofd = new SwissAddonFramework.Utils.Windows.OpenFileDialog();
ofd.FileSetEvent += new SwissAddonFramework.Utils.Windows.OpenFileDialog.FileSetHandler(delegate(string path)
{
ExtendedTextEdit.GetFromUID(pVal.Form, "FilePath").Value = path;
});
ofd.ShowDialog();
Vielen Dank für alle Hilfe!
Daniel Gesk
Hallo zusammen,ich habe die Lösung zur Anwahl eines Ordners gefunden. Hier der (extended) code:
[u][u]Globale Methoden und Variablen:[/u][/u]
public string Zielpfad = "";
private System.Windows.Forms.FolderBrowserDialog objDialog = new System.Windows.Forms.FolderBrowserDialog();
public Form Eingabemaske;
public void ShowDialog(Form pEingabemaske)
{
Eingabemaske = pEingabemaske;
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(Open));
t.TrySetApartmentState(System.Threading.ApartmentState.STA);
t.Start();
}
public void Open()
{
Zielpfad = "";
WindowWrapper sap = new WindowWrapper ();
if (objDialog.ShowDialog(sap) == System.Windows.Forms.DialogResult.OK)
{
Zielpfad = objDialog.SelectedPath + "\\";
customize.UI.Components.ExtendedTextEdit.GetFromUID(Eingabemaske, "FilePath").Value = Zielpfad;
}
}
internal class WindowWrapper : System.Windows.Forms.IWin32Window
{
[DllImport("user32.dll")]
protected static extern int GetForegroundWindow();
public WindowWrapper ()
{
_hwnd = (IntPtr) GetForegroundWindow();
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
[u][u]Verwendungs- und Importanweisungen:[/u][/u]
using System.Runtime.InteropServices;
Daniel Gesk
0
Please sign in to leave a comment.
Comments
0 comments