3

私はこれを機能させるために一日の大部分に夢中になっています。

コードにいくつかのクラスがあります。以下に関連するコードを投稿し、できるだけ短くするようにします

public class ServerSettings
{
    private BindingList<Server> serverList = new BindingList<Server>();

    public ServerSettings()
    {

    }

    private void readSettings()
    {           
        string list = "/Settings/Server";
        XmlNodeList Xn = settings.SelectNodes(list);

        foreach (XmlNode xNode in Xn)
        {
            Server tmpSrv = new Server();
            for (int i=0; i<xNode.ChildNodes.Count; i++)
            {
                if(xNode.ChildNodes[i].Name == "Name")
                    tmpSrv.Name = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Host")
                    tmpSrv.Host = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Username")
                    tmpSrv.Username = xNode.ChildNodes[i].InnerText;
                else if(xNode.ChildNodes[i].Name == "Password")
                    tmpSrv.Password = xNode.ChildNodes[i].InnerText;
            }
            tmpSrv.ID = xNode.Attributes["ID"].Value;
            serverList.Add(tmpSrv);
        }
    }

    public BindingList<Server> getServerList()
    {
        return serverList;
    }

    public void setServer(Server srv, bool isNew)
    {
        if(isNew)
        {
            serverList.Add(srv);
            srvCount++;
        }
        else
        {
            string list = "/Settings/Server[@ID='"+srv.ID+"']";
            XmlNodeList Xn = settings.SelectNodes(list);
            if(Xn.Count == 1)
            {
                XmlNode srvNode = Xn[0];
                for (int i=0; i<srvNode.ChildNodes.Count; i++)
                {
                    if(srvNode.ChildNodes[i].Name == "Name")
                        srvNode.ChildNodes[i].InnerText = srv.Name;
                    else if(srvNode.ChildNodes[i].Name == "Host")
                        srvNode.ChildNodes[i].InnerText = srv.Host;
                    else if(srvNode.ChildNodes[i].Name == "Username")
                        srvNode.ChildNodes[i].InnerText = srv.Username;
                    else if(srvNode.ChildNodes[i].Name == "Password")
                        srvNode.ChildNodes[i].InnerText = srv.Password;
                }
            }
        }

    }
}

また、別のフォームクラスには、プログラムの起動時に1回呼び出される次の関数があります。

public void populateServerListBox(ref BindingList<Server> srvList)
    {
        this.serverListBox.DisplayMember = "Name";
        this.serverListBox.DataSource = srvList;
    }

そして最後に

public partial class NewServerForm : Form
{
    private bool _isEdit = false;
    private bool isCanceled = true;
    private Server selSrv = null;

    public NewServerForm()
    {
        InitializeComponent();
    }

    void NewServerFormOKBtClick(object sender, EventArgs e)
    {
        isCanceled = false;
        this.Close();
    }

    void NewServerFormCancelBtClick(object sender, EventArgs e)
    {
        isCanceled = true;
        this.Close();
    }

    public bool isEdit
    {
        get
        {
            return _isEdit;
        }
        set
        {
            _isEdit = value;
        }
    }

    public void showForm(ref Server srv)
    {
        selSrv = srv;
        isEdit = true;
        this.newServerFormNameTb.Text = selSrv.Name;
        this.newServerFormHostTb.Text = selSrv.Host;
        this.newServerFormUsernameTb.Text = selSrv.Username;
        this.newServerFormPwdTb.Text = selSrv.Password;
        this.ShowDialog();
    }

    public void showForm()
    {
        selSrv = new Server();
        this.ShowDialog();
    }


    void NewServerFormFormClosing(object sender, FormClosingEventArgs e)
    {           
        if(isCanceled || selSrv == null)
            this.Dispose();
        else if(isEdit && selSrv != null)
        {
            selSrv.Name = this.newServerFormNameTb.Text;
            selSrv.Host = this.newServerFormHostTb.Text;
            selSrv.Username = this.newServerFormUsernameTb.Text;
            selSrv.Password = this.newServerFormPwdTb.Text;
            selSrv.BgwConfigPath = this.newServerFormBgwlocTb.Text;
            isCanceled = true;
            MainProgram.serverSettings.setServer(selSrv, false);
            this.Dispose();
        }
        else if(selSrv != null)
        {
            selSrv.Name = this.newServerFormNameTb.Text;
            selSrv.Host = this.newServerFormHostTb.Text;
            selSrv.Username = this.newServerFormUsernameTb.Text;
            selSrv.Password = this.newServerFormPwdTb.Text;

            MainProgram.serverSettings.setServer(selSrv, true);
            isCanceled = true;
            this.Dispose();
        }
    }
}

ユーザーが既存のサーバー設定を追加、削除、または変更できる、さまざまなサーバー用の単純なユーザー設定メニューを作成しようとしています。リストボックスのあるフォームで、ユーザーはボタンをクリックして、ユーザーが入力するためのテキストボックスを含む別のフォームを表示し、[OK]をクリックして変更を実行します。

これらの変更は、bindingListのアイテムに反映されます(新しいアイテムまたは既存のアイテムへの更新)。新しいサーバーアイテムを追加したり、bindingListから削除したりすると、すぐにリストボックスに反映されますが、既存のアイテムに変更を加えると、リストボックスの更新が拒否されます。リストボックスを含むフォームを閉じて再度開くと、変更が表示されますが、リストボックスで変更が行われたときにすぐに機能させることはできません。

リストボックスでrefresh()を呼び出し、BindingListでresetBindings()を呼び出してみました。

ここで何かが足りませんか?

4

1 に答える 1

9

リストボックスのダーティな修正と既知のMicrosoftのバグ:ボックスの内容を更新する必要がある場合は、datasource = nullに設定してから、再バインドします。

更新されない理由は、リスト内のオブジェクトが変更されておらず、オブジェクトの内容ではなく、オブジェクトの参照のみをチェックするためです。

[編集]

適切な方法はINotifyPropertyChanged、「サーバー」クラスにインターフェースを実装することです。参考資料をいくつかあげさせてください。

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=VS.80).aspx

http://www.gavaghan.org/blog/2007/07/17/use-inotifypropertychanged-with-bindinglist/

http://www.codeproject.com/KB/cs/BindBetterINotifyProperty.aspx

INotifyPropertyChangedを使用してリストボックスアイテムを更新する方法

于 2011-04-16T00:22:41.450 に答える