C# listboxのドラックドロップでフォントを変更する方法

このQ&Aのポイント
  • C#のlistboxでドラックドロップを行った際に、フォント(文字の色とサイズ)を変更する方法をまとめました。
  • ドラックドロップしたファイル名をリストボックスに表示し、そのフォントを変更するコードを示します。
  • ドラックドロップしたファイルのフォントをリストボックスに表示するための、C#のlistboxでのドラックドロップの方法について解説します。
回答を見る
  • ベストアンサー

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

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

  • ベストアンサー
  • tasoh
  • ベストアンサー率45% (19/42)
回答No.1

ListBox1_DrawItemイベントのところで ------------------ private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { if (e.Index < 0) return; e.DrawBackground(); Graphics g = e.Graphics; Font cfont = new Font("MS P明朝", 9, FontStyle.Bold); Rectangle rec = e.Bounds; Color col = Color.Black; string txt = ((ListBox)sender).Items[e.Index].ToString(); g.DrawString(txt, cfont, new SolidBrush(col), e.Bounds); } ------------------ とするとちゃんと描画されます。 TextRenderer.DrawText(g, txt, deffont, rec, col, TextFormatFlags.Default); を g.DrawString(txt, cfont, new SolidBrush(col), e.Bounds); に変えただけですが・・・

関連するQ&A

  • 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); }

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

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

  • CLRでのListBoxのオーナードローのやり方。

    Visual C++のCLRでフォームアプリケーションを作っていて、ListBoxのオーナードローの方法がわからないので質問させてもらいました。 CLRでプログラミングしたことが無く書籍などを読んだこともなく見よう見まねで作っています。 やりたいことはとりあえず http://dobon.net/vb/dotnet/control/lbownerdraw.html にあることです。 サンプルプログラムはVBとC#でC++がありません。 MSDN(http://msdn.microsoft.com/ja-jp/library/system.windows.forms.listbox.drawitem.aspx)も同様でVBとC#のサンプルプログラムでC++がありませんでした。 とりあえず。 C#のサンプル----------------------------------------------- //DrawItemイベントハンドラ //項目を描画する private void ListBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //背景を描画する //項目が選択されている時は強調表示される e.DrawBackground(); //文字を描画する色の選択 Brush b = null; if ((e.State & DrawItemState.Selected) != DrawItemState.Selected) { //選択されていない時 switch (e.Index % 3) { case 0: b = new SolidBrush(Color.Red); break; case 1: b = new SolidBrush(Color.Blue); break; case 2: b = new SolidBrush(Color.Green); break; } } else { //選択されている時はそのままの前景色を使う b = new SolidBrush(e.ForeColor); } //描画する文字列の取得 string txt = ((ListBox) sender).Items[e.Index].ToString(); //文字列の描画 e.Graphics.DrawString(txt, e.Font, b, e.Bounds); //後始末 b.Dispose(); //フォーカスを示す四角形を描画 e.DrawFocusRectangle(); } ----------------------------------------------- を参考にしてForm1に private: System::Void ListBox1_DrawItem(System::Object^ sender, System::Windows::Forms::DrawItemEventArgs^ e) { //背景を描画する //項目が選択されている時は強調表示される e->DrawBackground(); //文字を描画する色の選択 } と書いてみたのですが、そのあとどうすればよいかわかりません。 わかる方、回答お願いします。

  • 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

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

  • テキストボックスの改行について質問です。

    VB超初心者です。 現在VBで複数のCSVファイルを処理するアプリケーションを作成しています。 ドラッグドロップでファイルを認識して処理するのが目的です。 そこでまずテキストボックスにドラッグしたファイル名を表示したいと考えており、 ネットで公開されている以下のソースを利用したいと思っています。 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 Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop TextBox1.Text = e.Data.GetData(DataFormats.FileDrop)(0) End Sub しかし、これでは複数のファイルの表示ができませんでした。 テキストボックス内で改行させるにはどこを変更したら良いのか全く分かりません。 また、ファイル名ではなくファイルの階層を表示するのも目的と少し異なってしまいます。 調べてみても良くわかりませんでした。 そこでVBに精通している方にお願いです。 希望通りに表示する方法のアドバイスをお願いします!!

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

  • 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

専門家に質問してみよう