如果我有一个具有不同属性值的多个文件,我如何设置复选框的不确定状态?

时间:2012-04-16 05:29:58

标签: c# winforms wcf checkbox

我正在尝试从我的小文件浏览器[ListView]为所选项目创建一个“属性”表单。[就像在Windows中右键单击某些选定文件/文件夹并选择“属性”一样。]

表单显示名称,位置,类型,大小和属性

这是一个[WCF服务]服务器客户端应用程序,所以我从一个名为 Multi 的方法中从服务器获取属性,该方法以自己的线程开始。

问题是:如果多个文件具有[隐藏]和[只读]的不同属性值,我如何设置CheckState.Indeterminate。

enter image description here

WCF_Client.FM_ServiceReference.FileManagerClient client;

private void Form_MultiProp_Load(object sender, EventArgs e)
    {
        Thread th = new Thread(Multi);
        th.Start();
    }

private void GetAttributes(FileAttributes fAttributes)
    {
        this.Invoke((MethodInvoker)delegate
        {
            if (fAttributes != 0)
            {
                if ((fAttributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    Hidden.Checked = true;
                if ((fAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    ReadOnly.Checked = true;
            }
        });
    }

    public void Multi()
    {
        foreach (Item item in itemCollection)
        {
            GetAttributes(client.GetAttributeOfPath(item.Path));
        }
    }

2 个答案:

答案 0 :(得分:0)

您应该为第一个文件设置Hidden和ReadOnly复选框,如果找到具有不同属性的文件,则将它们设置为Indeterminate:

private void GetAttributes(FileAttributes fAttributes)
{
    this.Invoke((MethodInvoker)delegate
    {
        if (fAttributes != 0)
        {
            bool hidden = (fAttributes & FileAttributes.Hidden) == FileAttributes.Hidden;
            if (Hidden.Checked != hidden)
                Hidden.CheckState = CheckState.Indeterminate;
            bool readOnly = (fAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
            if (ReadOnly.Checked != readOnly)
                ReadOnly.CheckState = CheckState.Indeterminate;
        }
    });
}

public void Multi()
{
    FileAttributes fAttributes = client.GetAttributeOfPath(item[0].Path)
    this.Invoke((MethodInvoker)delegate
    {
        if (fAttributes != 0)
        {
            Hidden.Checked = (fAttributes & FileAttributes.Hidden) == FileAttributes.Hidden;
            ReadOnly.Checked = (fAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
        }
    });
    for (int i = 1; i < itemCollection.Count; i++)
    {
        Item item = itemCollection[i];
        GetAttributes(client.GetAttributeOfPath(item.Path));
    }
}

答案 1 :(得分:0)

我已经在列表中添加了所有属性,然后调用了Check方法来确定CheckState应该在ReadonlyCheckBox和HiddenCheckBox中,检查方法检查是否prev attribute == next attribute来设置CheckState。 / p>

List<FileAttributes> listAttributes = new List<FileAttributes>();

private void HiddenInvoke(CheckState HiddenState)
    {
        this.Invoke((MethodInvoker)delegate
        {
            Hidden.CheckState = HiddenState;
        });
    }
    private void ReadOnlyInvoke(CheckState ReadOnlyState)
    {
        this.Invoke((MethodInvoker)delegate
        {
            ReadOnly.CheckState = ReadOnlyState;
        });
    }
    private void HiddenCheck(bool check)
    {
        this.Invoke((MethodInvoker)delegate
        {
            Hidden.Checked = check;
        });
    }
    private void ReadOnlyCheck(bool check)
    {
        this.Invoke((MethodInvoker)delegate
        {
            ReadOnly.Checked = check;
        });
    }

public void Multi()
    {
        try
        {
            long SizeAll = 0;
            int fileCount = 0, folderCount = 0;
            LocInvoke(Loc);
            foreach (ListViewItem item in SelectedItems)
            {
               // some other calculations..
                if (client.IsFile(item.ToolTipText))
                    TypeInvoke(++fileCount, folderCount);
                else if (client.IsFolder(item.ToolTipText))
                    TypeInvoke(fileCount, ++folderCount);
               // Adding Attributes to a list
                listAttributes.Add(client.GetAttributeOfPath(item.ToolTipText));
               //Size Calculation
                SizeInvoke(CnvrtUnit(SizeAll += client.GetSizeOfPath(item.ToolTipText)));
            }
            Checking();
            Finished("OK", true);
        }
        catch { } //in case user closes the form before it finishes
    }

private void Checking()
    {
        bool hiddenSet = false;
        bool readonlySet = false;
        for (int i = 1; i < listAttributes.Count; i++)
        {
            if (hiddenSet && readonlySet)   //checks if they already different then there's no need to check again
                return;
            if (!hiddenSet)
            {
                if ((listAttributes[i - 1] & FileAttributes.Hidden) == (listAttributes[i] & FileAttributes.Hidden))
                {
                    HiddenCheck((listAttributes[i] & FileAttributes.Hidden) == FileAttributes.Hidden);
                }
                else
                {
                    HiddenInvoke(CheckState.Indeterminate);
                    hiddenSet = true;
                }
            }
            if (!readonlySet)
            {
                if ((listAttributes[i - 1] & FileAttributes.ReadOnly) == (listAttributes[i] & FileAttributes.ReadOnly))
                {
                    ReadOnlyCheck((listAttributes[i] & FileAttributes.ReadOnly) == FileAttributes.ReadOnly);
                }
                else
                {
                    ReadOnlyInvoke(CheckState.Indeterminate);
                    readonlySet = true;
                }
            }
        }
    }

enter image description here