C# と VB.NET の質問掲示板

ASP.NET、C++/CLI、Java 何でもどうぞ

ログ内検索
  • キーワードを複数指定する場合は 半角スペース で区切ってください。
  • 検索条件は、(AND)=[A かつ B] (OR)=[A または B] となっています。
  • [返信]をクリックすると返信ページへ移動します。
キーワード/ 検索条件 /
検索範囲/ 強調表示/ ON (自動リンクOFF)
結果表示件数/ 記事No検索/ ON
大文字と小文字を区別する

全過去ログを検索

<< 0 >>
■89555  レポートで複数行表示
□投稿者/ 無知守 -(2018/12/04(Tue) 14:18:29)

    分類:[C#] 

    C#のレポートを使って帳票作成していますが
    Form画面でTextBoxを複数行表示するには、MultilineをTrueにすれば問題ないけど、
    レポートの画面でTextBoxを複数行表示するにはどうしたらよろしいですか?


    よろしくお願いします
親記事 /過去ログ154より / 関連記事表示
削除チェック/

■90070  Re[2]: ユーザーコントロールの使い方
□投稿者/ ルパン -(2019/02/04(Mon) 11:25:36)
    No90069 (魔界の仮面弁士 さん) に返信
    ありがとうございます。
    
    削除部分は以下のように変更しました。
    
    >ucTiem が Diposed となったときに、lblTime がまだ Disposed 状態でないことは
    >保証できているのでしょうか。
    保証できるようにするにはどうすればよいのでしょうか?
    
    フォームでは FormClose に入れていたのですが
    ユーザーコントロールではこれに該当するものがわからなかったので
    ネットで調べたら 「UserControl の後処理は Dispose で行う」とあったので、
    ユーザーコントロールのコンストラクタに
    this.Disposed += UcMakeAbilityTime_Disposed;
    を追加し Dispose のイベントに Properties への保存処理を入れました。
    
    現状以下のようになっています。
    
    frmMain.cs              ucTime.cs
    ┌──────┬─────────┐  ┌─────────┐
    │┌────┐│         │  │┌──┐┌───┐│
    ││btnTime ││         │  ││lbl ││btn  ││
    │└────┘│         │  ││Time││Close ││
    │      │         │  │└──┘└───┘│
    │pnlMain   │pnlSub      │  │UserControl    │
    └──────┴─────────┘  └─────────┘
    
    ≡ frMain.cs ≡
    private void Button_Click(object sender, EventArgs e)
    {
     SelectButton((Control)sender);
    }
    
    private void SelectButton(Control SelectButton)
    {
     while (pnlSub.Controls.Count > 0){
      pnlSub.Controls.Remove(pnlSub.Controls[0]);
      pnlSub.Controls[0].Dispose();
     }
     if (sender== btnTime) AddUserControl(new ucTime());
    }
    
    private void AddUserControl(Control cnt)
    {
     cnt.Dock = DockStyle.Fill;
     pnlSub.Controls.Add(cnt);
    }
    
    ≡ ucTime.cs ≡
    public ucTime()
    {
     InitializeComponent();
     this.Disposed += UcTime_Disposed;
    }
    
    private void UcMakeAbilityTime_Disposed(object sender, EventArgs e)
    {
     Properties.Settings.Default.ucMakeAbilityTime = lblTime.Text;
     Properties.Settings.Default.Save();
    }
    
    private void btnClose_Click(object sender, EventArgs e)
    {
     this.ParentForm.Close();     ←正しいかどうかわかりませんが、
    }                  この場合は正常に保存されている。
    
記事No.90068 のレス /過去ログ155より / 関連記事表示
削除チェック/

■90071  Re[3]: ユーザーコントロールの使い方
□投稿者/ 魔界の仮面弁士 -(2019/02/04(Mon) 13:47:10)
    2019/02/04(Mon) 14:32:50 編集(投稿者)

    No90070 (ルパン さん) に返信
    >   pnlSub.Controls.Remove(pnlSub.Controls[0]);
    >   pnlSub.Controls[0].Dispose();

    そのコードは正しくありません。
    1 行目が指している [0] と 2 行目が指している [0] が
    別のコントロールを指していることに注意してください。


    提示のコードだと、pnlSub 上の子コントロールが【奇数個】の場合に
    最後の 1 個が Remove されないことになってしまいます。
    ※例外:ArgumentOutOfRangeException

    また、【偶数個】であった場合、一見するとすべて Remove されるように
    見えますが、実際には偶数・奇数いずれの場合にも、
    「半数は Remove しただけ(Dispose していない)」
    「半数は Dispose しただけ(自動的に Remove される)」
    という動作に陥っていることに注意してください。



    代案1:削除対象のコレクションと列挙用のコレクションを分ける

    Control[] children = pnlSub.Controls.OfType<Control>().ToArray();
    foreach (Control c in children)
    {
     pnlSub.Controls.Remove(c);
     c.Dispose();
    }


    代案2:子アイテムを変数等に保持しておき、コレクションから除去後に破棄する
    while (pnlSub.Controls.Count > 0)
    {
     using (pnlSub.Controls[0])
     {
      pnlSub.Controls.RemoveAt(0);
     }
    }


    代案3:後ろから前に Dispose する
    for (int i = pnlSub.Controls.Count - 1; i >= 0; i--)
    {
     // Dispose されると自動的に Remove される
     pnlSub.Controls[i].Dispose();
    }



    > ネットで調べたら 「UserControl の後処理は Dispose で行う」とあったので、

    親コントロールが破棄される際には、それに先んじて
    子コントロールが破棄されますので、Disposed イベント中には
    他のコントロール(もちろん Label にも)アクセスすることはできません。

    故にたとえば Dispose 時には、「子コントロールそのもの」に
    アクセスするのではなく、予め保持しておいた
    「子コントロールが持っていた値」を使って処理するようにします。


    新規プロジェクトに下記を貼り、実行してフォームを閉じてみてください。


    using System.Diagnostics;
    public partial class Form1 : Form
    {
     private UserControl uc;
     public Form1()
     {
      InitializeComponent();
      Controls.Add(uc = new UC() { Dock = DockStyle.Fill });
     }
    }

    public class UC : UserControl
    {
     private Label lbl;
     private string _labelText = null;
     private string LabelText { get { return _labelText; } }
     public UC()
     {
      Controls.Add(lbl = new Label() { Text = "lbl", Dock = DockStyle.Fill });
      
      // Label が破棄された後でも Text を拾えるよう、保持しておく
      lbl.TextChanged += delegate { _labelText = lbl.Text; };
      _labelText = lbl.Text;

      //
      lbl.Disposed += delegate
      {
       Debug.WriteLine("Label.Disposed");
       Debug.WriteLine(" UserControl.Disposing = " + this.Disposing);
       Debug.WriteLine(" UserControl.IsDisposed = " + this.IsDisposed);
       Debug.WriteLine(" Label.Disposing = " + lbl.Disposing);
       Debug.WriteLine(" Label.IsDisposed = " + lbl.IsDisposed);
       Debug.WriteLine(" Label.Text = [" + lbl.Text + "]");
       Debug.WriteLine(" LabelText = [" + this.LabelText + "]");
      };
      this.Disposed += delegate
      {
       Debug.WriteLine("UserControl.Disposed");
       Debug.WriteLine(" UserControl.Disposing = " + this.Disposing);
       Debug.WriteLine(" UserControl.IsDisposed = " + this.IsDisposed);
       Debug.WriteLine(" Label.Disposing = " + lbl.Disposing);
       Debug.WriteLine(" Label.IsDisposed = " + lbl.IsDisposed);
       Debug.WriteLine(" Label.Text = [" + lbl.Text + "]");
       Debug.WriteLine(" LabelText = [" + this.LabelText + "]");
      };
     }
    }


    ---- 実行結果 ----
    Label.Disposed
     UserControl.Disposing = True
     UserControl.IsDisposed = False
     Label.Disposing = True
     Label.IsDisposed = False
     Label.Text = []
     LabelText = [lbl]
    UserControl.Disposed
     UserControl.Disposing = True
     UserControl.IsDisposed = False
     Label.Disposing = False
     Label.IsDisposed = True
     Label.Text = []
     LabelText = [lbl]
記事No.90068 のレス /過去ログ155より / 関連記事表示
削除チェック/

■94820  Re[1]: インデックスが配列の境界外です。が出てしまいました。
□投稿者/ エイ -(2020/05/21(Thu) 23:08:51)
    コード
    
    Imports libZPlay_player
    Imports Microsoft.VisualBasic
    Imports System
    Imports System.Collections
    Imports System.Collections.Generic
    Imports System.Data
    Imports System.Drawing
    Imports System.Diagnostics
    Imports System.Windows.Forms
    Imports System.Linq
    Imports System.Xml.Linq
    Imports libZPlay_player.libZPlay
    
    Partial Public Class Form1
    Dim a As String()
        Dim file As String()
        Dim IntSelectIndexPre As Integer = 0
        Private ReadOnly history1 As New HashSet(Of String)(StringComparer.OrdinalIgnoreCase)
    
    Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
            ' If ListBox2.SelectedItem <> "" Then
            If ListBox2.SelectedItem IsNot "" Then
                TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem)
            Else
                Exit Sub
            End If
    
            If IntSelectIndexPre = ListBox2.SelectedIndex Then
                Exit Sub
            End If
    
            IntSelectIndexPre = ListBox2.SelectedIndex
    
    
            player.OpenFile(file(IntSelectIndexPre), TStreamFormat.sfAutodetect)
    
    
            showinfo()
            player.StartPlayback()
        End Sub
        Private Sub OpenFileDialog1_FileOk(sender As Object, e As ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    
            Dim files = OpenFileDialog1.FileNames
            Dim conflict = files.Where(AddressOf history1.Contains)
    
            If conflict.Any() Then
                e.Cancel = True
                Dim fileNames = conflict.Select(AddressOf System.IO.Path.GetFileName)
                MessageBox.Show("同じファイルがあります。" & vbCrLf & String.Join(vbCrLf, fileNames), "競合", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                Array.ForEach(files, AddressOf history1.Add)
            End If
            If LoadMode = 0 Then
                player.Close()
                If Not (player.OpenFile(OpenFileDialog1.FileName, TStreamFormat.sfAutodetect)) Then
                    MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
            ElseIf LoadMode = 1 Then
                player.Close()
    
                Dim format As TStreamFormat = player.GetFileFormat(OpenFileDialog1.FileName)
    
                Dim fInfo As New System.IO.FileInfo(OpenFileDialog1.FileName)
                Dim numBytes As Long = fInfo.Length
                Dim fStream As New System.IO.FileStream(OpenFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
                Dim br As New System.IO.BinaryReader(fStream)
                Dim stream_data() As Byte = Nothing
    
                stream_data = br.ReadBytes(CInt(Fix(numBytes)))
                If Not (player.OpenStream(True, False, stream_data, CUInt(numBytes), format)) Then
                    MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
    
                br.Close()
                fStream.Close()
            ElseIf LoadMode = 2 Then
                player.Close()
    
                Dim format As TStreamFormat = player.GetFileFormat(OpenFileDialog1.FileName)
                BufferCounter = 0
    
                Dim fInfo As New System.IO.FileInfo(OpenFileDialog1.FileName)
                Dim numBytes As UInteger = CUInt(fInfo.Length)
                If br IsNot Nothing Then
                    br.Close()
                End If
                If fStream IsNot Nothing Then
                    fStream.Close()
                End If
    
                br = Nothing
                fStream = Nothing
    
                fStream = New System.IO.FileStream(OpenFileDialog1.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
                br = New System.IO.BinaryReader(fStream)
                Dim stream_data() As Byte = Nothing
                Dim small_chunk As UInteger = 0
                small_chunk = CType(Math.Min(100000, numBytes), UInteger)
                ' read small chunk of data
                stream_data = br.ReadBytes(CInt(Fix(small_chunk)))
                ' open stream
                If Not (player.OpenStream(True, True, stream_data, CUInt(stream_data.Length), format)) Then
                    MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
    
                ' read more data and push into stream
                stream_data = br.ReadBytes(CInt(Fix(small_chunk)))
                player.PushDataToStream(stream_data, CUInt(stream_data.Length))
    
            ElseIf LoadMode = 3 Then
                If Not (player.AddFile(OpenFileDialog1.FileName, TStreamFormat.sfAutodetect)) Then
                    MessageBox.Show(player.GetError(), String.Empty, MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Exit Sub
                End If
    
            End If
    
    
            showinfo()
            player.StartPlayback()
    
    
        End Sub
        Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
            LoadMode = 3
            OpenFileDialog1.FileName = ""
            OpenFileDialog1.Multiselect = True
            OpenFileDialog1.Title = "開くファイルを指定"
            OpenFileDialog1.Filter = "All Supported Files|*.mp3;*.mp2;*.mp1;*.ogg;*.oga;*.flac;*.wav;*.ac3;*.aac|Mp3 Files|*.mp3|Mp2 Files|*.mp2|Mp1 Files|*.mp1|Ogg Files|*.ogg|FLAC files|*.flac|Wav files|*.wav|AC-3|*.ac3|AAC|*.aac"
            OpenFileDialog1.FilterIndex = 1
            OpenFileDialog1.RestoreDirectory = True
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                file = OpenFileDialog1.SafeFileNames
                a = OpenFileDialog1.FileNames
                For b As Integer = 0 To file.Length - 1
                    ListBox2.Items.Add(file(b))
                    player.StartPlayback()
                Next
                ListBox2.SelectedIndex = 0
                TextBox1.Text = System.IO.Path.GetFileName(ListBox2.SelectedItem)
    
            End If
        End Sub
    
    エラーが出る場所は
      player.OpenFile(file(IntSelectIndexPre), TStreamFormat.sfAutodetect)
    ここで「インデックスが配列の境界外です。」と出ます。
    
    コードが1万行以上あるのですいません。
    
    インデックスが配列の境界外です。と出る場合、どのように修正したらいいんでしょうか?
    
    元のコードをリストボックス追加して1曲目を開きました。
    その次の2曲目を開き、2番目のリストボックスをクリックするとインデックスが配列の境界外です。と出ます。
    お願い申し上げます。
    
    
    
    サイトのダウンロードはDOWNLOAD: http://libzplay.sourceforge.net/download.html
     
    libZPlay-2.02-SDK (2.12 MB)を使用しました。
    
    libzplay.dllとlibZPlay.vbが必要です。
    
    
    
    
記事No.94819 のレス /過去ログ164より / 関連記事表示
削除チェック/



<< 0 >>

パスワード/

- Child Tree -