listBoxとTimerについて C#

このQ&Aのポイント
  • listBoxとTimerを使用してヤフーニュースのURLを取得し、表示する方法についての質問です。
  • listBox1にヤフーニュースのURLをドラッグ&ドロップすることで、URLを取得し、画面に表示することができます。
  • しかし、listBox2にURLをドラッグ&ドロップした際には、URLの取得はできるものの画面への表示が行われません。Timerの使い方に問題があるか、意見をいただきたいです。
回答を見る
  • ベストアンサー

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) { }

質問者が選んだベストアンサー

  • ベストアンサー
回答No.3

こんにちは 今日は日曜日でお休みでした。(笑 >「url1」を全て表示したら、そのまま「url2」も続けて全て表示させることです。 なんとなくご質問の内容がわかってきました。 単純に「url1」を全て表示したら、そのまま「url2」も続けて全て表示するサンプルを作成しました、 単純にするためにwebBrowser1_DocumentCompletedのイベントハンドラーは使用していまん。 もし、必要な場合はお手数ですがもう一度返信してください。 int urlindex = 0; private void Form1_Load(object sender, EventArgs e) { webBrowser1.ScriptErrorsSuppressed = true;//スクリプトエラーを表示しない timer1.Interval = 3000; timer2.Interval = 3000; 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) { if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox2_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox1_DragDrop(object sender, DragEventArgs e) { string url1 = e.Data.GetData(DataFormats.Text).ToString();      listBox1.Text = url1;       if (e.Data.GetDataPresent(typeof(string)))      {     ListBox target = (ListBox)sender;      string itemText =     (string)e.Data.GetData(typeof(string));     target.Items.Add(url1);     } } private void listBox2_DragDrop(object sender, DragEventArgs e) { string url2 = e.Data.GetData(DataFormats.Text).ToString(); listBox2.Text = url2; if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; string itemText = (string)e.Data.GetData(typeof(string)); target.Items.Add(url2); //MessageBox.Show("表示"); } } private void goButton_Click(object sender, EventArgs e) { urlindex = 0; timer1.Start();//timer1からスタート } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("timer_1_TICK"); if (listBox1.Items.Count != 0 && urlindex < listBox1.Items.Count) { string url1 = (string)listBox1.Items[urlindex]; webBrowser1.Navigate(url1); urlindex++; } //listBox1のURLの表示が終了したらtimer1を停止、timer2を起動、 //urlindex = 0にする else if (urlindex >= listBox1.Items.Count) { timer1.Enabled = false; timer2.Enabled = true; urlindex = 0; } } private void timer2_Tick(object sender, EventArgs e) { if (listBox2.Items.Count != 0 && urlindex < listBox2.Items.Count) { string url2 = (string)listBox2.Items[urlindex]; webBrowser1.Navigate(url2); urlindex++; } //listBox1のURLの表示が終了したらtimer2を停止 else if (urlindex >= listBox2.Items.Count) { timer2.Enabled = false; }       }

udon9257
質問者

お礼

やってみました。 私がやりたかったことはこれです! とても助かりました。 この次はURLをDBへ入れたいと思います。 また、わからないときは質問させて頂きますのでどうぞ宜しくお願い致します。

その他の回答 (2)

回答No.2

続きです。 private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //読み込まれるURLによって処理を分ける if (e.Url.ToString() == url1) { timer2.Enabled=true; } else if (e.Url.ToString() == url2) { timer1.Enabled = true; } } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { } よろしくお願いします。

udon9257
質問者

お礼

bybalsendercase様 本当にありがとうございました。 次にLisBoxに入ったURLを表示させるのではなく、データベースに入れたいと思い買いて見ました。 また躓いてしまったので、もしお時間ございましたら回答頂けると嬉しいです。 以下URLです。↓ http://okwave.jp/qa/q8064923.html

udon9257
質問者

補足

bybalsendercase様 ご回答頂きありがとうございます。 以下のように書き換えて見ました。 これで振り分けは出来るようになりました。 目的達成まであと少しのような気がします。 目的は「url1」を全て表示したら、そのまま「url2」も続けて全て表示させることです。 どう書けばいいのでしょうか? 宜しくお願い致します。 ----------------------------------- 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) { if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox2_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("UniformResourceLocator")) e.Effect = DragDropEffects.Link; else e.Effect = DragDropEffects.None; } private void listBox1_DragDrop(object sender, DragEventArgs e) { string url1 = e.Data.GetData(DataFormats.Text).ToString(); listBox1.Text = url1; if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; string itemText = (string)e.Data.GetData(typeof(string)); target.Items.Add(url1); } } private void listBox2_DragDrop(object sender, DragEventArgs e) { string url2 = e.Data.GetData(DataFormats.Text).ToString(); listBox2.Text = url2; if (e.Data.GetDataPresent(typeof(string))) { ListBox target = (ListBox)sender; string itemText = (string)e.Data.GetData(typeof(string)); target.Items.Add(url2); //MessageBox.Show("表示"); } } private void goButton_Click(object sender, EventArgs e) { urlindex = 0; timer1.Start(); timer2.Start(); } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); MessageBox.Show("timer_1_TICK"); if (listBox1.Items.Count != 0 && urlindex < listBox1.Items.Count) { string url1= (string)listBox1.Items[urlindex]; webBrowser1.Navigate(url1); urlindex++; } } private void timer2_Tick_2(object sender, EventArgs e) { timer2.Stop(); MessageBox.Show("timer_2_TICK"); if (listBox2.Items.Count != 0 && urlindex < listBox2.Items.Count) { string url2 = (string)listBox2.Items[urlindex]; webBrowser1.Navigate(url2); urlindex++; } //Console.WriteLine(listBox2.Items[0]); } private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { timer1.Start(); timer2.Start(); //読み込まれるURLによって処理を分ける string url1 = ""; string url2 = ""; if (e.Url.ToString() == url1) { timer2.Enabled = true; } else if (e.Url.ToString() == url2) { timer1.Enabled = true; } } private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { }

回答No.1

こんばんは >listBox1の中身を画面表示させたら、次にlistBox2の中身も表示したいと思っています。 すいません、この部分の意味がわかりませんでしたので、適当にサンプルを作成しましたので、どこがどのように違うのかできる範囲で結構ですので教えてください。 サンプルではlistBox1とlistBox2に同じ数だけURLを貼り付けて、goButtonをクリックすると交互にlistBox1とlistBox2のURLをwebBrowser1に表示されるようにしてあります。 何回か回答返信の繰り返しをしないと、もしかしたら質問の意味が掴めないかもしれません。 但し、私は明日から出勤ですので、回答できませんので、他の方に委ねます。 public Form1() { InitializeComponent(); } int urlindex1 = 0;//この部分を変更 int urlindex2 = 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) { urlindex1 = 0;//この部分を変更 urlindex2 = 0;//この部分を変更 timer1.Start(); } string url1; private void timer1_Tick(object sender, EventArgs e) { timer1.Enabled = false; if ((listBox1.Items.Count != 0) && (urlindex1 < listBox1.Items.Count)) { url1 = (string)listBox1.Items[urlindex1]; webBrowser1.Navigate(url1); urlindex1++; } } string url2; private void timer2_Tick(object sender, EventArgs e) { timer2.Enabled = false; if ((listBox2.Items.Count != 0) && (urlindex2 < listBox2.Items.Count)) { url2 = (string)listBox2.Items[urlindex2]; webBrowser1.Navigate(url2); urlindex2++; } } DocumentCompletedの部分が文字制限で掲載できませんでしたので、次ページに続きます。

udon9257
質問者

お礼

bybalsendercase様 ご丁寧にありがとうございます。 やってみます!

関連するQ&A

  • 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#でドラッグ&ドロップが機能しない。

    使用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); }

  • ファイルをドラッグドロップで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

  • D&Dで画像のURLを取得したいです。VB.NET

    VB.NET、WinXPなのですが、 Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) If e.Data.GetDataPresent("UniformResourceLocator") Then e.Effect = DragDropEffects.Link Else e.Effect = DragDropEffects.None End If End Sub Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Dim url As String = e.Data.GetData(DataFormats.Text).ToString() TextBox1.Text = url End Sub この方法でファイルWEBサイト上の画像をドラッグ&ドロップすると 例えばこのページのGOOのロゴの場合 テキストボックスには「http://www.goo.ne.jp/」というリンク先のURLが 表示されるのですが、 そうではなくて、ロゴそのもののURL「http://www.goo.ne.jp/img/logo/gootop_logo.gif」 を表示させたいのですが、どのようにしたら良いでしょうか。 よろしくお願いします。m(__)m

  • 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で上記のような方法でドラッグされたテキストを テキストボックスに表示するようにしたいのですが、 この方法だと最初の一文字しかドロップされません。 どこか修正箇所などありましたら、ご教示いただけると助かります。

  • 【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]; } }

  • 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); // 動作確認         }       }     }   } }

  • 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

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

    こんばんは。 テキストボックスにドラッグ&ドロップされたフォルダのパスを取得させたいのですがドラッグの対象をフォルダに限定させたい、もしくはファイルがドラッグ&ドロップされたらそのファイルのカレントディレクトリまでのパスを取得させたいのですが、下記のコードでは取得までは出来るのですがファイルがきた場合にファイルまでのパスが取得されてしまいまいます。 よろしくお願いします。 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

  • C#について・・・

    次のようなブログラムなのですが、ラジオボタンが上手く切り替わりません。 どこを修正すると上手くいくでしょうか? <<文字数オーバーのため省略>> public Form1() { // // Windows フォーム デザイナ サポートに必要です。 // InitializeComponent(); if(isJapanStyle) { label5.Text=string.Format("0時0分0秒"); label6.Text=string.Format("0時0分0秒"); } else { label5.Text=string.Format("00:00:00"); label6.Text=string.Format("00:00:00"); } // // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。 // <<文字数オーバーのため 省略>> static void Main() { Application.Run(new Form1()); } protected void button1_Click(object sender, System.EventArgs e) { if(isJapanStyle) { label5.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label5.Text=DateTime.Now.ToString("T"); } recTime=dt; timer2.Stop(); timer3.Start(); } private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } private void timer1_Tick(object sender, System.EventArgs e) { dt=DateTime.Now; if(isJapanStyle) { label4.Text=string.Format("{0}時{1}分{2}秒",dt.Hour,dt.Minute,dt.Second); } else { label4.Text=DateTime.Now.ToString("T"); } } private void timer3_Tick(object sender, System.EventArgs e) { keika=dt-recTime; if(isJapanStyle) { label6.Text=string.Format("{0}時間{1}分{2}秒",keika.Hours,keika.Minutes,keika.Seconds); } else { label6.Text=string.Format("{0}:{1}:{2}",keika.Hours,keika.Minutes,keika.Seconds); } } private void timer2_Tick(object sender, System.EventArgs e) { timer3.Stop(); } private void radioButton2_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=false; } private void radioButton1_CheckedChanged(object sender, System.EventArgs e) { isJapanStyle=true; } } }

専門家に質問してみよう