请帮我一个错误 [英] help me with an error please

查看:42
本文介绍了请帮我一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有



i有2个表格,1表格有列表框,选择按钮和列表框的上下文菜单。

另一个表格有文本框和一个保存按钮。



我的问题是



i点击它将获取的选择按钮在listbox中显示文件。然后在列表框中我有编辑选项。如果我点击编辑选项,它将转到另一个表格,并以该表格获取所选项目信息和diaplay。



inthat



i想要编辑并保存所选项目相同。



请帮帮我我的代码是下面



这是form1代码



 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 TagLib;

命名空间播放列表
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
string [] f,p;
private void Form1_Load( object sender,EventArgs e)
{

}

private void button1_Click( object sender,EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (openFileDialog1.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
f = openFileDialog1.SafeFileNames ;
p = openFileDialog1.FileNames;
for int i = 0 ; i < f.Length; i ++)
{
listBox1.Items.Add(f [i]);
}
foreach string d in open.FileNames)
{
listBox1.Items.Add(d);
}
}

}

private void listBox1_MouseDoubleClick( object sender,MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = p [listBox1.SelectedIndex];
}

private void fileToolStripMenuItem_Click( object sender,EventArgs e)
{
// object item = listBox1.SelectedItem;
// Form2 MyForm = new Form2();
// MyForm.Owner = this;
// MyForm.Show();
// Form2 f2 = new Form2();
// f2.ShowDialog();
Form2 f2 = new Form2( this );
f2.ShowDialog();

}
public void SetNewListBoxValue( string newValue)
{
// listBox1.Items .Add(newValue);

listBox1.Update();
}
public string ListBoxValue
{
< span class =code-keyword> get
{ return listBox1.SelectedItem.ToString(); }
}
}
}



然后是form2代码

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 TagLib;

命名空间播放列表
{
public partial class Form2:Form
{
OpenFileDialog ofd = OpenFileDialog();
Form1 f1;
public Form2(Form1 f1)
{
this .f1 = f1;
InitializeComponent();
}

私有 void Form2_Load( object sender,EventArgs e)
{

ofd.Filter = Mp3文件| * .mp3;
if (ofd.ShowDialog()== DialogResult.OK)
{
txtPath.Text = ofd.FileName;
}
TagLib.File mp3 = TagLib.File.Create(txtPath.Text);
// txtComposers.Text = this.f1.ListBoxValue;
txtAlbum。 Text = mp3.Tag.Album;
txtArtist.Text = GetAllStringsFromArrary(mp3.Tag.AlbumArtists, );
txtBitsPerMinute.Text = mp3.Tag.BeatsPerMinute.ToString();
txtComposers.Text = GetAllStringsFromArrary(mp3.Tag.Composers, );
txtCopyright.Text = mp3.Tag.Comment;
txtGenre.Text = GetAllStringsFromArrary(mp3.Tag.Genres, );
txtTitle.Text = mp3.Tag.Title;
txtTrack.Text = mp3.Tag.Track.ToString();
txtYear.Text = mp3.Tag.Year.ToString();
txtLength.Text = mp3.Properties.Duration.ToString();
}

private void button1_Click( object sender,EventArgs e)
{
f1.SetNewListBoxValue(txtPath.Text);
f1.SetNewListBoxValue(txtAlbum.Text);
f1.SetNewListBoxValue(txtArtist.Text);
f1.SetNewListBoxValue(txtBitsPerMinute.Text);
f1.SetNewListBoxValue(txtComposers.Text);
f1.SetNewListBoxValue(txtCopyright.Text);
f1.SetNewListBoxValue(txtGenre.Text);
f1.SetNewListBoxValue(txtTitle.Text);
f1.SetNewListBoxValue(txtTrack.Text);
f1.SetNewListBoxValue(txtYear.Text);
f1.SetNewListBoxValue(txtLength.Text);

}


public string GetAllStringsFromArrary( string [] strArray, string strDelimeter)
{
string strFinal = string .Empty;

for int i = 0 ; i < strArray.Length; i ++)
{
strFinal + = strArray [i];

if (i!= strArray.Length - 1
{
strFinal + = strDelimeter;
}
}
return strFinal;


}

}

}

解决方案

首先,停止将表单传递给新的孩子 - 这是一个非常糟糕的主意。部分是因为它将两种形式的设计锁定在一起使维护成为一个真正的PITA,但主要是因为它要求Form2知道Form1而破坏OOP,Form1必须公开它的控件,而且它甚至没有必要。



而是在Form2中创建属性,并在创建实例时以及在显示实例之前在Form1中设置值。当代码从ShowDialog调用返回时,您可以使用相同的属性来检索任何更改。

请参阅此处:在两个表单之间传输信息,部分1:父母对孩子 [ ^ ]

hi to all

i have 2 form, 1 form have listbox and select button.and context menu for listbox.
another form have textboxes and one save button.

my question is

i click the select button it will fetch and display files in listbox.then in listbox i have edit option .if i click edit option it will go another form and fetch that selected items information and diaplay in that form.

inthat

i want to edit and save that selected item same.

please help me my code is below

this is form1 code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TagLib;

namespace Playlist
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] f, p;
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
          OpenFileDialog open =new OpenFileDialog();
          if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
          {
              f = openFileDialog1.SafeFileNames;
              p = openFileDialog1.FileNames;
              for (int i = 0; i < f.Length; i++)
              {
                  listBox1.Items.Add(f[i]);
              }
              foreach (string d in open.FileNames)
              {
                  listBox1.Items.Add(d);
              }
          }
           
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            axWindowsMediaPlayer1.URL = p[listBox1.SelectedIndex];
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //object item = listBox1.SelectedItem;
            //Form2 MyForm = new Form2();
            //MyForm.Owner = this;
            //MyForm.Show();
            //Form2 f2 = new Form2();
            //f2.ShowDialog();
            Form2 f2 = new Form2(this);
            f2.ShowDialog();

        }
         public void SetNewListBoxValue(string newValue)
        {
            //listBox1.Items.Add(newValue);
          
            listBox1.Update();
        }
        public string ListBoxValue
        {
            get { return listBox1.SelectedItem.ToString(); }
        }
    }
}


then form2 code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TagLib;

namespace Playlist
{
    public partial class Form2 : Form
    {
        OpenFileDialog ofd = new OpenFileDialog();
        Form1 f1;
        public Form2(Form1 f1)
        {
            this.f1 = f1;
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            ofd.Filter = "Mp3 Files | *.mp3";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = ofd.FileName;
            }
            TagLib.File mp3 = TagLib.File.Create(txtPath.Text);
            //txtComposers.Text = this.f1.ListBoxValue;
            txtAlbum.Text = mp3.Tag.Album;
            txtArtist.Text = GetAllStringsFromArrary(mp3.Tag.AlbumArtists, ",");
            txtBitsPerMinute.Text = mp3.Tag.BeatsPerMinute.ToString();
            txtComposers.Text = GetAllStringsFromArrary(mp3.Tag.Composers, ",");
            txtCopyright.Text = mp3.Tag.Comment;
            txtGenre.Text = GetAllStringsFromArrary(mp3.Tag.Genres, ",");
            txtTitle.Text = mp3.Tag.Title;
            txtTrack.Text = mp3.Tag.Track.ToString();
            txtYear.Text = mp3.Tag.Year.ToString();
            txtLength.Text  = mp3.Properties.Duration.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            f1.SetNewListBoxValue(txtPath.Text);
            f1.SetNewListBoxValue(txtAlbum.Text);
            f1.SetNewListBoxValue(txtArtist.Text);
            f1.SetNewListBoxValue(txtBitsPerMinute.Text);
            f1.SetNewListBoxValue(txtComposers.Text);
            f1.SetNewListBoxValue(txtCopyright.Text);
            f1.SetNewListBoxValue(txtGenre.Text);
            f1.SetNewListBoxValue(txtTitle.Text);
            f1.SetNewListBoxValue(txtTrack.Text);
            f1.SetNewListBoxValue(txtYear.Text);
            f1.SetNewListBoxValue(txtLength.Text);
            
        }


        public string GetAllStringsFromArrary(string[] strArray, string strDelimeter)
        {
            string strFinal = string.Empty;

            for (int i = 0; i < strArray.Length; i++)
            {
                strFinal += strArray[i];

                if (i != strArray.Length - 1)
                {
                    strFinal += strDelimeter;
                }
            }
            return strFinal;


        }  

}
       
    }

解决方案

For starters, stop passing your form through to the new "child" - it's a very bad idea. Partly because it locks the design of the two forms together making maintenance a real PITA, but mostly because it breaks OOPs by requiring that Form2 knows about Form1, Form1 has to expose it's controls, and it's not even slightly necessary.

Instead, create properties in Form2 and set the value in Form1 when you create the instance, and before you display it. When the code returns from the ShowDialog call, you can use the same properties to retrieve any changes.
See here: Transferring information between two forms, Part 1: Parent to Child[^]


这篇关于请帮我一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆