• 締切済み

C#でドラッグ&ドロップが機能しない。

使用OSはWindows8で 開発環境はVisualStudio2012 (.NET Framework4.5)です。 ListBox1にドラッグ&ドロップでファイル名を表示する機能を追加したいと思い、以下のようなコードを記述しました。また、ListBox1のイベントととしてListBox1ListBox1_DragEnterとListBox1_DragDropを関連づけました。 この状態でビルドして動作を確認してみると、問題なく動作しました しかし、debugフォルダ内のEXEファイルを直接起動するとドラッグ&ドロップ不可のマークが出てしまいます。 なにが問題が問題なのか分からず困っています。ご回答お願いします。 private void Form1_Load(object sender, EventArgs e) {   this.AllowDrop = true;   ListBox1.AllowDrop = true; } private void ListBox1_DragEnter(object sender,System.Windows.Forms.DragEventArgs e) {   e.Effect = DragDropEffects.Copy; } private void ListBox1_DragDrop(object sender,System.Windows.Forms.DragEventArgs e) {   string[] fileName =(string[])e.Data.GetData(DataFormats.FileDrop, false);   ListBox1.Items.AddRange(fileName); }

みんなの回答

noname#251971
noname#251971
回答No.1

記載されたコードを見る限りでは、ドラッグ受け入れについては特に問題なさそうに見えます。 ですので、質問文に記載されたところ以外に問題があるのではないかと思われます。 (ドロップ時処理(ListBox1_DragDrop)はファイル以外がドロップされることを想定していないので問題がありますが、質問内容と無関係なのでここでは触れず) 可能性の1つとして、ドラッグ&ドロップはドラッグ元とドロップ先が双方同じユーザーの権限で動作している必要がありますが、その点は問題ないでしょうか? どちらか一方のみがUACによって管理者権限に昇格して実行されている場合、両者間でドラッグ&ドロップは不可になります。 そのほかは。。。。 コード内のほかの箇所でなにかやらかしていないかを調べるとか、Load/DragEnterイベントハンドラが外れてしまっていないかとか、実行ファイルを消してリビルドしてみるとか、、、 質問記載内容から読み取れる範囲では明確なところはちょっとおもいつきません。

関連するQ&A

  • listBoxとTimerについて C#

    listBoxとTimerについて C# ヤフーニュースのURLをlistboxへ入れます。 そしてlistBox1の中身を画面表示させたら、次にlistBox2の中身も表示したいと思っています。 最初listBoxが1つだったときはうまく行っていたのですが、listBoxを増やすとうまく行かなくなりました。 この状態だと、listBox2の中身だけ表示させて終わってしまいます。 Timerの使い方が怪しいと思うのですが、どうでしょうか? 些細なことでも何でもいいのでご意見頂ければ助かります。 -----以下コード抜粋------ public partial class Form1 : Form { public Form1() { InitializeComponent(); } int urlindex = 0; private void Form1_Load(object sender, EventArgs e) { listBox1.AllowDrop = true; listBox1.DragEnter += new DragEventHandler(listBox1_DragEnter); listBox1.DragDrop += new DragEventHandler(listBox1_DragDrop); listBox2.AllowDrop = true; listBox2.DragEnter += new DragEventHandler(listBox2_DragEnter); listBox2.DragDrop += new DragEventHandler(listBox2_DragDrop); } private void listBox1_DragEnter(object sender, DragEventArgs e) { //URLのみ受け入れる//@ITより if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox2_DragEnter(object sender, DragEventArgs e) { //URLのみ受け入れる//@ITより if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox1_DragDrop(object sender, DragEventArgs e) { //ドロップされたリンクのURLを取得する//@ITより string url = e.Data.GetData(DataFormats.Text).ToString(); //結果を表示 listBox1.Text = url; //MessageBox.Show(url); //ドロップされたデータがstring型か調べる if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; //ドロップされたデータ(string型)を取得 string itemText = (string)e.Data.GetData(typeof(string)); //ドロップされたデータをリストボックスに追加する target.Items.Add(url); //MessageBox.Show("表示"); } } private void listBox2_DragDrop(object sender, DragEventArgs e) { //ドロップされたリンクのURLを取得する//@ITより string url = e.Data.GetData(DataFormats.Text).ToString(); //結果を表示 listBox2.Text = url; //MessageBox.Show(url); //ドロップされたデータがstring型か調べる if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; string itemText = (string)e.Data.GetData(typeof(string)); target.Items.Add(url); } } private void goButton_Click(object sender, EventArgs e) { urlindex = 0; timer1.Start(); timer2.Start(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); if (listBox1.Items.Count != 0 && urlindex < listBox1.Items.Count) { string url = (string)listBox1.Items[urlindex]; webBrowser1.Navigate(url); urlindex++; } } private void timer2_Tick_1(object sender, EventArgs e) { timer2.Stop(); if (listBox2.Items.Count != 0 && urlindex < listBox2.Items.Count) { string url = (string)listBox2.Items[urlindex]; } } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { timer1.Start(); timer2.Start(); } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { }

  • ドラッグドロップ(AllowDrop)について

    Form1にドラッグドロップ機能を導入しようと思い AllowDropプロパティをTrueにしてDragDropイベントとDragEnterイベントを記述しました。 実際にデバックしてみるとうまく機能して安心したのですが 後で、bin/debug/AAA.exeを直接クリックしてみるとなぜか機能しません。 MyBase.LoadイベントにMyBase.AllowDrop=Trueと記述してみたいのですがダメでした。 何か設定が間違ってるのでしょうか? ぜひご回答お願いします。 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   MyBase.AllowDrop = True End Sub Private Sub drag_drop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop   Dim list As String() = CType(e.Data.GetData(DataFormats.FileDrop, False), String())   For Each a In list      MsgBox(a)   Next End Sub Private Sub drag_enter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter   e.Effect = DragDropEffects.Copy End Sub

  • C# listbox

    どうしても解らないのでご教授お願いたします. やりたいことは単純なのですが,リストボックスにドラックドロップしたときに表示する フォント(文字の色とサイズ)を変更したいのですが,下記のプログラムではドラッグドロップ するとリストが真っ白になってしまいます.いろいろ調べたのですが何が原因が解りません. 何卒よろしくお願いいたします. private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { listBox1.Items.Clear(); listBox1.DrawMode = DrawMode.OwnerDrawVariable; //コントロール内にドロップされたとき実行される //ドロップされたすべてのファイル名を取得する string[] fileName = (string[])e.Data.GetData(DataFormats.FileDrop, false); //ListBoxに追加する listBox1.Items.AddRange(fileName); listBox1.Sorted = true; } private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { //コントロール内にドラッグされたとき実行される if (e.Data.GetDataPresent(DataFormats.FileDrop)) { //ドラッグされたデータ形式を調べ、ファイルのときはコピーとする e.Effect = DragDropEffects.Copy; } else { //ファイル以外は受け付けない e.Effect = DragDropEffects.None; } } //DrawItemイベントハンドラ //項目を描画する private void ListBox1_DrawItem(object sender,System.Windows.Forms.DrawItemEventArgs e) { //背景を描画する Font cfont = new Font("MS P明朝", 9, FontStyle.Bold); //適切な色で背景を描画する。 e.DrawBackground(); Rectangle rec = e.Bounds; Graphics g = e.Graphics; Color col = Color.Black; Font deffont = e.Font; deffont = cfont; string txt = ((ListBox)sender).Items[e.Index].ToString(); TextRenderer.DrawText(g, txt, deffont, rec, col, TextFormatFlags.Default); }

  • 【C# 2010】 テキストへのドラック&ドロップ

    VisualStudio C#2010 Express の環境で、テキストボックスにエクスプローラからフォルダを指定(複数)して、ドラック&ドロップすると、そのフォルダのフルパスをテキストボックスへ表示させるアプリケーションを作成しております。 調べながら作成し、フォルダのフルパスをテキストボックスへ表示させられるようになったのですが、わからない点として、複数のフォルダを指定しても、テキストボックスに表示されるのは1フォルダ分のフルパスだけになってしまうという点です。 テキストボックス側のプロパティで、複数行で表示させる設定(Multiline)は有効(True)にしてありますし、ユーザーがドラックしたデータを受け入れるかの設定(AllDrop)も有効にしてあります。 その他、気づいた点として、☆ ドロップイベントの配列(pass)には指定したフォルダ分のフルパスが格納されていますが、textbox1.Text = pass[i] の部分で全てのフルパスがテキストに表示されません。 原因を考えましたが、どうしてもわからなかったので、どなたかお分かりになられる方がいらっしゃい ましたら、ご教授のほどお願いできますでしょうか? 何卒、よろしくお願いいたします。 ----------------------------------------------------------------------------- public Form1() { InitializeComponent(); this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop); this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter); } // ☆ ドラッグイベント private void textBox1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.All; else e.Effect = DragDropEffects.None; } // ☆ ドロップイベント private void textBox1_DragDrop(object sender, DragEventArgs e) { string[] pass = (string[])e.Data.GetData(DataFormats.FileDrop, false); int i; for (i = 0; i < pass.Length; i++) { textBox1.Text = pass[i]; } }

  • ファイルをドラッグドロップでTextBooxにファイル名を出したい

    下記のサンプルコードをWEBサイトで見つけたので ListBox1というところをTextBox1と直して テキストボックスにファイルのフルパスが出るように したかったのですが、Listbox1だと正常なのですが、 TextBox1に変更すると動作しなくなってしまいます。 最後の部分の ListBox1.Items.AddRange(e.Data.GetData(DataFormats.FileDrop))は TextBox1.Text=e.Data.GetData(DataFormats.FileDrop) に変更してあります。 正常に動作するにはどこを直したらよいか教えて頂きたいです。 宜しくお願いします。m(__)m --------------------------------------------------- Private Sub ListBox1_DragEnter(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles ListBox1.DragEnter '関連連づけの確認をしています。 'この場合、ドラッグアンドドロップの形式であるかどうか? If e.Data.GetDataPresent(DataFormats.FileDrop) Then 'ドロップ効果を取得 e.Effect = DragDropEffects.Copy Else 'ドロップ効果を破棄 e.Effect = DragDropEffects.None End If End Sub Private Sub ListBox1_DragDrop(ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles ListBox1.DragDrop 'DragEnterイベントで、取得した情報を追加 ListBox1.Items.AddRange(e.Data.GetData(DataFormats.FileDrop)) End Sub

  • C#での画像ファイルをドラッグアンドドロップで描画

    C#で画像ファイルをドラッグアンドドロップで描画させるプログラムを作りたいのですが、うまくできません。 ドラッグしたファイル名を読み取る部分までは動作確認できています。このファイル名の画像ファイルをForm1に描画させる部分でエラーになってしまいます。 どのように修正したらよいのかわからないのでお助けください。 ----- using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Drag {   public partial class Form1 : Form   {     public Form1()     {       InitializeComponent();     }     private void Form1_DragEnter(object sender, DragEventArgs e)     {       e.Effect = DragDropEffects.All;     }     private void Form1_DragDrop(object sender, DragEventArgs e)     {       Graphics g = e.Graphics;   // <------ エラーになる。       if (e.Data.GetDataPresent(DataFormats.FileDrop))       {         foreach (string fileName             in (string[])e.Data.GetData(DataFormats.FileDrop))         {           g.DrawImage(new Bitmap(fileName, new PointF(10F, 50F)));           // Console.WriteLine(fileName); // 動作確認         }       }     }   } }

  • ドラッグ&ドロップの対象をフォルダに限定したい

    こんばんは。 テキストボックスにドラッグ&ドロップされたフォルダのパスを取得させたいのですがドラッグの対象をフォルダに限定させたい、もしくはファイルがドラッグ&ドロップされたらそのファイルのカレントディレクトリまでのパスを取得させたいのですが、下記のコードでは取得までは出来るのですがファイルがきた場合にファイルまでのパスが取得されてしまいまいます。 よろしくお願いします。 Dim ddpath As String Private Sub TxtPath_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TxtPath.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub TxtPath_DragDrop(ByVal sender As Object, ByVal e As _System.Windows.Forms.DragEventArgs) Handles TxtPath.DragDrop ddpath = e.Data.GetData(DataFormats.FileDrop)(0) If Dir(ddpath, FileAttribute.Directory) <> "" Then TxtPath.Text = ddpath End If End Sub

  • D&Dでファイルパスを取得

    フォームにD&Dでファイルパスを取得する プログラムを作ろうと思っているのですが、うまくいきません。 ファイルをフォームにドラッグしても禁止マークがでて イベントハンドラがイベントをキャッチしてくれないようです。 なにが問題かアドバイスを頂けないでしょうか? よろしくお願いします。 Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.AllowDrop = True End Sub Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = DragDropEffects.Copy End If End Sub Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop Dim FileName As String FileName = e.Data.GetData(DataFormats.FileDrop)(0) MsgBox(FileName) End Sub End Class ---- 開発環境:VS2005 pro OS:WindowsVista

  • TextBoxへ文字列をD&Dをする方法を教えてください。

    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.Text)) Then e.Effect = DragDropEffects.Copy Else e.Effect = DragDropEffects.None End If End Sub Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop TextBox1.Text = e.Data.GetData(DataFormats.Text)(0) End Sub VB.NETで上記のような方法でドラッグされたテキストを テキストボックスに表示するようにしたいのですが、 この方法だと最初の一文字しかドロップされません。 どこか修正箇所などありましたら、ご教示いただけると助かります。

  • VC#でドラッグアンドドロップ時のカーソルの制御

    プログラミング初心者です。 ファイル管理ソフトを作りたいなと、種々の言語を比較した結果VC#が一番やりやすいかなと思い、基礎知識もなくvisual studioをインストールしてあれこれ試行錯誤しながら、メニューやボタンやリストビューを作るところまでは行きました。ドラッグアンドドロップ機能を付けたいと思い、色々と検索してドラッグアンドドロップでlistView1に特定の拡張子のファイルを入れる下記のコードは見つかりました。さらに、特定の拡張子(ここでは.txt)以外のファイルをドラッグしてlistView1の領域に入った時、カーソルを禁止のマークに変えたいのですがそのコードがわかりません。ご教授いただければと存じます。 // もちろん基礎知識も平行して勉強しているところです。 // 下記のlistView1_DragEnterイベントハンドラ内に // if(ドラッグしたファイルの拡張子が.txt以外だったら){Cursor.Current = Cursors.No;} // とすると思い色々試したのですがうまくいきませんでした。(素人の試行錯誤なので提示できません) private void listView1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Link; } } private void listView1_DragDrop(object sender, DragEventArgs e) { string[] handles = (string[])e.Data.GetData(DataFormats.FileDrop, false); foreach (string s in handles) { if (File.Exists(s)) { if (string.Compare(Path.GetExtension(s), ".txt", true) == 0) { AddFileToListview(s); } } else if (Directory.Exists(s)) { DirectoryInfo di = new DirectoryInfo(s); FileInfo[] files = di.GetFiles("*.txt"); foreach (FileInfo file in files) AddFileToListview(file.FullName); } } } private void AddFileToListview(string fullFilePath) { if (!File.Exists(fullFilePath)) return; string fileName = Path.GetFileNameWithoutExtension(fullFilePath); string dirName = Path.GetDirectoryName(fullFilePath); if (dirName.EndsWith(Convert.ToString(Path.DirectorySeparatorChar))) dirName = dirName.Substring(0, dirName.Length - 1); //hack off the trailing \ ListViewItem itm = listView1.Items.Add(fileName); itm.SubItems.Add(dirName); //second column = path }

専門家に質問してみよう