drag and drop problem... vb .net
-
Please can someone tell me how to drag a file from my explorer window on my form and load the file into a rich text box that i have created at runtime? thank you, in advance
hi, to do this, you need to set the allowdrop porperty of the control to true. then you need to write two event-handlers to handle the events fired when you do drag & drop operation. below is a code sample in C# but you can easily convert it to VB //you need to set the property and assign eventhandlers this.richtextbox1.Allow = true; this.richtextbox1.DragDrop += new DragEventHandler(richtextbox1_DragDrop); this.richtextbox1.DragEnter += new DragEventHandler(richtextbox1_DragEnter); //then write the event handlers private void richtextbox1_DragEnter(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void richtextbox1_DragDrop(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] file = (string[])e.Data.GetData(DataFormats.FileDrop); fileName = file[0]; //Now that you have the file name of the file, you can read it and display its contents } } hope this helps. regards :)
-
Please can someone tell me how to drag a file from my explorer window on my form and load the file into a rich text box that i have created at runtime? thank you, in advance
on formload textbox1.dragdrop=true
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop If (System.IO.File.Exists(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = True) Then If (System.IO.Path.GetExtension(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = ".txt") Then TextBox1.Text = System.IO.File.ReadAllText(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) Else MessageBox.Show("select the .txt extention file") End If End If End Sub Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub
Hope this may help You.:) -
on formload textbox1.dragdrop=true
Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop If (System.IO.File.Exists(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = True) Then If (System.IO.Path.GetExtension(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = ".txt") Then TextBox1.Text = System.IO.File.ReadAllText(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) Else MessageBox.Show("select the .txt extention file") End If End If End Sub Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub
Hope this may help You.:) -
Thanks for the code... technically that should work, but I create my rich text box at runtime, so i cant call its events, or atleast... i am unsure of how to! :confused: please help? thanx guys!
DaxBooysen wrote:
i am unsure of how to! please help?
replace textbox with Richedittextbox Actually i tried it with textbox.It is very simple. replace textbox with richedittextbox
-
DaxBooysen wrote:
i am unsure of how to! please help?
replace textbox with Richedittextbox Actually i tried it with textbox.It is very simple. replace textbox with richedittextbox
-
I have no problem with that, its the fact that i create it at RUNTIME... i dont place it on my form at design time, so when i try make an event for a text box that has not been created yet i cant! know what i mean? thanx again!:confused:
ok What code u r writing?
-
ok What code u r writing?
i create a rtf box in many places and set it as a child control of a tab control... but i call it up like this when i want to change its values...
Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0)
and so you see... since its created at runtime... how do i call its events? thanx, dax :) -
i create a rtf box in many places and set it as a child control of a tab control... but i call it up like this when i want to change its values...
Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0)
and so you see... since its created at runtime... how do i call its events? thanx, dax :)then write the code on each control which is provided to u.
-
i create a rtf box in many places and set it as a child control of a tab control... but i call it up like this when i want to change its values...
Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0)
and so you see... since its created at runtime... how do i call its events? thanx, dax :)DaxBooysen wrote:
Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0)
Doesn't this create a control, then discard it for one that's already within the tab page ? Either way, you can attach events to a control in code, at least you can in C#.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Thanks for the code... technically that should work, but I create my rich text box at runtime, so i cant call its events, or atleast... i am unsure of how to! :confused: please help? thanx guys!
If you create a control at runtime, you have to wire up the events yourself at runtime, just like the designer generated code does when you drop a control on the form. Lookup AddHandler and RemoveHandler on MSDN and it'll show you how to do this...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
If you create a control at runtime, you have to wire up the events yourself at runtime, just like the designer generated code does when you drop a control on the form. Lookup AddHandler and RemoveHandler on MSDN and it'll show you how to do this...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007