Answer the question
In order to leave comments, you need to log in
How to change the fill color of an item (line in listview ) depending on a file attribute?
How to change the fill color of an item (line in a listview ) depending on a file attribute? What needs to be fixed? I tried to refer to indexes, something like - "listView1.Items[0].SubItems[0].BackColor = Color.Yellow", but an error pops up:
############### ################################################### #################### Unhandled
exception of type 'System.ArgumentOutOfRangeException' in System.Windows.Forms.dll
Additional information: InvalidArgument=Value '0' is invalid for 'index' .
################################################### ###################################
The task of the program is this: by clicking on button1 on listview1, the files in the folder should be displayed. Moreover, the lines with file names should be painted in yellow if the file attribute is "Read-only"
Here is the code:
using System;
using System.IO;
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;
namespace WindowsFormsApplication4 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
textBox1.Text = "tracing";
}
StringBuilder result = new StringBuilder();
String str;
int index = 0;
byte[] buffer = new byte[511];
String word;
private void button1_Click(object sender, EventArgs e) {
FolderBrowserDialog FBD = new FolderBrowserDialog();
listView1.Items.Clear();
FBD.ShowNewFolderButton = false;
FBD.Description = "Выберите папку...";
if (FBD.ShowDialog() == DialogResult.OK) {
string[] allFoundFiles = Directory.GetFiles(FBD.SelectedPath, "*.txt", SearchOption.TopDirectoryOnly);
foreach (string file in allFoundFiles) {
listView1.Items.Add(file);
FileAttributes fA = File.GetAttributes(file);
if ((fA & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
listView1.Items[0].BackColor = Color.Yellow;
}
}
}
private int wordEntry(ref StringBuilder result, ref String word) {
int n = 0, k = 0, count = 0;
while (result.Length != n) {
if (result[n] == word[k]) {
if (k == word.Length - 1) {
count++; k = -1;
}
n++; k++;
}
else {
n++; k = 0;
}
}
return count;
}
private void button2_Click(object sender, EventArgs e) {
word = textBox1.Text;
FileInfo f = new FileInfo(str);
using (FileStream fs = f.Open(FileMode.Open, FileAccess.Read)) {
using (BinaryReader br = new BinaryReader(fs)) {//, Encoding.GetEncoding("Unicode")
int bytesRead = 0;
while ((bytesRead = br.Read(buffer, 0, buffer.Length)) != 0) {
for (int i = 0; i <= bytesRead - 1; i++) {
result.AppendFormat("{0:x2}", (char)buffer[i]);
}
//Array.Clear(buffer, 0, buffer.Length);
}
MessageBox.Show(wordEntry(ref result, ref word).ToString());//
}
}
}
private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {
str = e.Item.Text;
}
}
}
Answer the question
In order to leave comments, you need to log in
You do
Poke your finger at the line, which then fills this collection with at least something
"listView1.Items[0].SubItems[0]"
1. SubItems must be pre-added, so that you may have created columns in the designer beforehand does not affect the number of SubItems you create
2. SubItems inherit their parent's style by default, so you need to untie:
ListViewItem item = new ListViewItem(name);
item.UseItemStyleForSubItems = false;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question