Windows Develop Bookmark and Share   
 index > Windows Forms General > Delete files using CommonDialog
 

Delete files using CommonDialog

Hello,
I want to permit to user to delete files using Common Dialog. DeleteFileDialog doesn't exist, and I use OpenFileDialog control to do it. But button to click to do the operation shows Open and not Delete.
Which is the correct solution?

Thank,

Mauro

MauroGv  Tuesday, August 07, 2007 4:03 PM

MauroGv wrote:

Hello Rodolfo,

but still remains the problem that button to click to do the operation shows Open and not Delete.

Thank,

Mauro

You can override the WndProc method and use SetWindowText API to do the trick, take the following step:
1). Find the handle of the OpenFileDialog via m.LParam

2). Find the handle of the Open button via FindWindowEx function

3). Change the text of Open button via SetWindowText function.

I wirte the following sample for your information, put attention to the code in bold, hope it helps you and others.


Code Snippet

partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindowEx(IntPtr hwndParent,

IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]

static extern bool SetWindowText(IntPtr hWnd, string lpString);

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog delete = new OpenFileDialog();

delete.Title = "Delete File";

if (delete.ShowDialog() == DialogResult.OK)

{

if (MessageBox.Show("do you want to delete this file?",

"comfirm delete", MessageBoxButtons.YesNoCancel)

== DialogResult.Yes)

{

File.Delete(delete.FileName);

}

}

}

protected override void WndProc(ref Message m)

{

// Leting the Form take care off all messages.

base.WndProc(ref m);

if (m.Msg == 289) // Notify of message loop

{

IntPtr dialogHandle = (IntPtr)m.LParam; // Handle of the file dialog

// Getting the handle of the open button in the OpenFileDialog dialog.

IntPtr openButtonHandle = FindWindowEx(dialogHandle, IntPtr.Zero,

"Button", "&Open");

SetWindowText(openButtonHandle, "Delete");

}

}

}

For VB.NET code, I translate it as following and have a test, no problem.

Code Snippet

System.Runtime.InteropServices

System.IO

Class Form3

<DllImport("user32.dll", SetLastError:=True)> _

Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter _

As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr

End Function

<DllImport("user32.dll")> _

Private Shared Function SetWindowText(ByVal hWnd As IntPtr, ByVal lpString As String) _

As Boolean

End Function

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim delete As New OpenFileDialog()

delete.Title = "Delete File"

If delete.ShowDialog() = Windows.Forms.DialogResult.OK Then

If MessageBox.Show("do you want to delete this file?", _

"comfirm delete", MessageBoxButtons.YesNoCancel) _

= Windows.Forms.DialogResult.Yes Then

File.Delete(delete.FileName)

End If

End If

End Sub

Protected Overloads Overrides Sub WndProc(ByRef m As Message)

' Leting the Form take care off all messages.

MyBase.WndProc(m)

If m.Msg = 289 Then

' Notify of message loop

Dim dialogHandle As IntPtr = DirectCast(m.LParam, IntPtr)

' Handle of the file dialog

' Getting the handle of the open button in the OpenFileDialog dialog.

Dim openButtonHandle As IntPtr = _

FindWindowEx(dialogHandle, IntPtr.Zero, "Button", "&Open")

SetWindowText(openButtonHandle, "Delete")

End If

End Sub

Class

Zhi-Xin Ye  Wednesday, August 15, 2007 1:29 AM

I think you can easy solve this problen using the openDialogFile just add this code, the OpenDialogFile will be show to the user and if he select a file this will be deleted. Easy dude...

OpenFileDialog delete= new OpenFileDialog();

if(delete.ShowDialog(this) == DialogResult.OK)

File.Delete(delete.FileName);

Rodolfo Garcia  Tuesday, August 07, 2007 10:41 PM

Try this link though in VB you might get some help..

http://www.developerfusion.co.uk/show/162/

Karthikeya Pavan Kumar .B  Wednesday, August 08, 2007 7:30 AM

Hello Rodolfo,

but still remains the problem that button to click to do the operation shows Open and not Delete.

Thank,

Mauro

MauroGv  Wednesday, August 08, 2007 12:43 PM
MauroGv wrote:

Hello Rodolfo,

but still remains the problem that button to click to do the operation shows Open and not Delete.

Thank,

Mauro

MauroGv  Friday, August 10, 2007 5:09 PM

Sorry... i thought you already got the answer the easy way to complete the task of delete a file with a OpenFileDialog is this.

set the OpenFileDialog Title property to "What you want" and that's it.

example:

OpenFileDialog delete= new OpenFileDialog();

delete.Title="Delete File";

if(delete.ShowDialog(this) == DialogResult.OK)

File.Delete(delete.FileName);

Rodolfo Garcia  Friday, August 10, 2007 5:19 PM

When the OpenFileDialog control is displayed, the user has almost complete control of the file system through the context menu. How would you prevent him from deleting a file? Changing the behaviour of the Dialog controls is not as easy as it seems it should be.

JohnWein  Friday, August 10, 2007 5:46 PM

MauroGv wrote:

Hello Rodolfo,

but still remains the problem that button to click to do the operation shows Open and not Delete.

Thank,

Mauro

You can override the WndProc method and use SetWindowText API to do the trick, take the following step:
1). Find the handle of the OpenFileDialog via m.LParam

2). Find the handle of the Open button via FindWindowEx function

3). Change the text of Open button via SetWindowText function.

I wirte the following sample for your information, put attention to the code in bold, hope it helps you and others.


Code Snippet

partial class Form3 : Form

{

public Form3()

{

InitializeComponent();

}

[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindowEx(IntPtr hwndParent,

IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("user32.dll")]

static extern bool SetWindowText(IntPtr hWnd, string lpString);

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog delete = new OpenFileDialog();

delete.Title = "Delete File";

if (delete.ShowDialog() == DialogResult.OK)

{

if (MessageBox.Show("do you want to delete this file?",

"comfirm delete", MessageBoxButtons.YesNoCancel)

== DialogResult.Yes)

{

File.Delete(delete.FileName);

}

}

}

protected override void WndProc(ref Message m)

{

// Leting the Form take care off all messages.

base.WndProc(ref m);

if (m.Msg == 289) // Notify of message loop

{

IntPtr dialogHandle = (IntPtr)m.LParam; // Handle of the file dialog

// Getting the handle of the open button in the OpenFileDialog dialog.

IntPtr openButtonHandle = FindWindowEx(dialogHandle, IntPtr.Zero,

"Button", "&Open");

SetWindowText(openButtonHandle, "Delete");

}

}

}

In case anyone is curious, that value 289 is WM_ENTERIDLE (= 0x121).

sirjis  Monday, August 13, 2007 11:48 AM

Hello and thanks for your interest.
Considering that I never used Windows's API and that for me is high programming, I've translated C# in VB.NET.
In the example:
[DllImport("user32.dll", SetLastError = true)]

static extern IntPtr FindWindowEx(IntPtr hwndParent,

IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

"user32.dll", SetLastError:=True)> _

Public Shared Function FindWindowEx(ByVal hwndParent As IntPtr, _

ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr

End Function

But my wndProc sub:

Protected Overrides Sub WndProc(ByRef m As Message)

'' Leting the Form take care off all messages.

MyBase.WndProc(m)

' Notify of message loop

If m.Msg = 289 Then

.....

end if

end sub

Never enter in the block if , probably because m.msg never reach the value 289.
I don't know.
Regards,

Mauro

MauroGv  Tuesday, August 14, 2007 7:06 PM

For VB.NET code, I translate it as following and have a test, no problem.

Code Snippet

System.Runtime.InteropServices

System.IO

Class Form3

<DllImport("user32.dll", SetLastError:=True)> _

Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter _

As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr

End Function

<DllImport("user32.dll")> _

Private Shared Function SetWindowText(ByVal hWnd As IntPtr, ByVal lpString As String) _

As Boolean

End Function

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

Dim delete As New OpenFileDialog()

delete.Title = "Delete File"

If delete.ShowDialog() = Windows.Forms.DialogResult.OK Then

If MessageBox.Show("do you want to delete this file?", _

"comfirm delete", MessageBoxButtons.YesNoCancel) _

= Windows.Forms.DialogResult.Yes Then

File.Delete(delete.FileName)

End If

End If

End Sub

Protected Overloads Overrides Sub WndProc(ByRef m As Message)

' Leting the Form take care off all messages.

MyBase.WndProc(m)

If m.Msg = 289 Then

' Notify of message loop

Dim dialogHandle As IntPtr = DirectCast(m.LParam, IntPtr)

' Handle of the file dialog

' Getting the handle of the open button in the OpenFileDialog dialog.

Dim openButtonHandle As IntPtr = _

FindWindowEx(dialogHandle, IntPtr.Zero, "Button", "&Open")

SetWindowText(openButtonHandle, "Delete")

End If

End Sub

Class

Zhi-Xin Ye  Wednesday, August 15, 2007 1:29 AM

Hello,
my translation from VB.NET to C# was good, similar to you.
Probably I didn't declare a new OpenFileDialog.
Now my codegoes in if block with m.msg=289.
At first moment the execution didn't change Open in Delete, because my globalization is not English.
I changed &Open in what I see in my text button and ..... it works!!!!

Thanks and regards,
Mauro

MauroGv  Wednesday, August 15, 2007 7:53 PM

You can use google to search for other answers

Custom Search

More Threads

• Show/Hide Form
• Flow charting diagram in vb.net (basic visio type of program) Create a drawing
• Datagridview background colour
• Tooltip not showing in custom control after click
• Drawing in graph
• get text length of listbox's selected item
• Key transferred from a KeyUp to another
• Why System.Windows.Forms.SendKeys.Send("{CAPSLOCK}") is not working?
• Building Application
• Easily disable tabs in TabControl