C# 如果有多个具有不同属性值的文件,如何设置复选框的不确定状态?
我正在尝试从我的小文件资源管理器[ListView]C# 如果有多个具有不同属性值的文件,如何设置复选框的不确定状态?,c#,winforms,wcf,checkbox,C#,Winforms,Wcf,Checkbox,我正在尝试从我的小文件资源管理器[ListView][与在Windows中右键单击某些选定文件/文件夹并选择“属性”时一样]为选定项目创建属性表单。] 表单显示名称、位置、类型、大小和属性 它是一个[WCF服务]服务器客户机应用程序,因此我从一个名为Multi的方法中从服务器获取属性,该方法在自己的线程中启动 问题是:如何设置CheckState。如果多个文件的[Hidden]和[Readonly]属性值不同,则无法确定 WCF_Client.FM_ServiceReference.FileMa
[与在Windows中右键单击某些选定文件/文件夹并选择“属性”时一样]为选定项目创建属性表单。] 表单显示名称、位置、类型、大小和属性 它是一个[WCF服务]服务器客户机应用程序,因此我从一个名为Multi的方法中从服务器获取属性,该方法在自己的线程中启动 问题是:如何设置CheckState。如果多个文件的[Hidden]和[Readonly]属性值不同,则无法确定
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));
}
}
您应该为第一个文件设置“隐藏”和“只读”复选框,然后如果找到具有不同属性的文件,则应将其设置为“不确定”:
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));
}
}
private void GetAttributes(FileAttributes-fAttributes)
{
此.Invoke((MethodInvoker)委托
{
如果(fAttributes!=0)
{
bool hidden=(fAttributes&FileAttributes.hidden)=FileAttributes.hidden;
如果(Hidden.Checked!=隐藏)
Hidden.CheckState=CheckState.undeterminate;
bool readOnly=(fAttributes&FileAttributes.readOnly)=FileAttributes.readOnly;
if(ReadOnly.Checked!=只读)
ReadOnly.CheckState=CheckState.undeterminate;
}
});
}
公共空间
{
FileAttributes fAttributes=client.GetAttributeOfPath(项[0]。路径)
此.Invoke((MethodInvoker)委托
{
如果(fAttributes!=0)
{
Hidden.Checked=(fAttributes&FileAttributes.Hidden)=FileAttributes.Hidden;
ReadOnly.Checked=(fAttributes&FileAttributes.ReadOnly)==FileAttributes.ReadOnly;
}
});
for(int i=1;i
我在一个列表中添加了所有属性,然后调用Checking方法来确定ReadonlyCheckBox和HiddenCheckBox中的CheckState,Checking方法检查prev attribute==next attribute
以设置CheckState
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;
}
}
}
}
List listtributes=new List();
私有无效HiddenInvoke(CheckState HiddenState)
{
此.Invoke((MethodInvoker)委托
{
Hidden.CheckState=HiddenState;
});
}
私有void readonlynvoke(CheckState ReadOnlyState)
{
此.Invoke((MethodInvoker)委托
{
ReadOnly.CheckState=ReadOnlyState;
});
}
私有无效隐藏检查(布尔检查)
{
此.Invoke((MethodInvoker)委托
{
Hidden.Checked=检查;
});
}
私有void只读检查(bool检查)
{
此.Invoke((MethodInvoker)委托
{
ReadOnly.Checked=检查;
});
}
公共空间
{
尝试
{
长尺寸L=0;
int fileCount=0,folderCount=0;
Loc调用(Loc);
foreach(SelectedItems中的ListViewItem项)
{
//其他一些计算。。
if(client.IsFile(item.ToolTipText))
TypeInvoke(++fileCount,folderCount);
else if(client.IsFolder(item.ToolTipText))
TypeInvoke(fileCount,+folderCount);
//向列表中添加属性
Add(client.GetAttributeOfPath(item.ToolTipText));
//尺寸计算
SizeInvoke(CnvrtUnit(SizeAll+=client.GetSizeOfPath(item.ToolTipText));
}
检查();
完成(“OK”,正确);
}
捕获{}//以防用户在表单完成之前关闭表单
}
私有无效检查()
{
bool-hiddenSet=false;
bool readonlySet=false;
for(int i=1;i
我没有尝试使用您的代码,因为我不能在
Multi()
中使用for
,因为它还计算其他一些东西,如大小、类型
,我很抱歉我没有在问题中提到这是我的错,但我认为您的代码是正确的,你能检查一下我的答案是否100%正确吗?我不明白你为什么不能使用进行。您可以这样做Item Item=itemCollection[i]
使用item
。我当然可以,但是循环应该是从index0
到Count-1
,因为我从item.Path
中获得了一些其他信息。您先使用第一个索引,然后从index1
到Count-1
。我认为这段代码符合您的要求,大卫:是的,我想了很多,直到有人回答我的问题,我才明白我的意思