我正在为可变分区系统开发内存模拟器。 我想使用c#创建一个列表视图,该列表视图将使用以下规则显示内存:

1.FF分配。

2.解释并说明了四种组合可能性的取消分配。

3.为以上#1和#2的每个实例提供了可用空间状态。

4.命令的处理状态(p#,大小和位置)。

5.必要时进行垃圾压实。

6.压缩后如果没有足够的可用内存,则出现适当的错误消息。

我遇到的问题是当我尝试重新分配内存进程时。 我需要检查要重新分配的分区上方和分区下方是否有可用空间,以便可以将所有分区的可用空间合并到一个分区中。 当我尝试存储上方的行以检查其空闲行或低于行时,出现错误

{“ InvalidArgument ='-1'的值对'index'无效。\\ r \\ n参数名称:indexSystem.ArgumentOutOfRangeException

我似乎找不到其他方法来合并上面和下面的行,如果它们具有可用空间,则只能使用以下代码:string content2 = listView1.Items [listView1.SelectedIndices [-1]]。SubItems [0] 。文本;

这给出了错误。 有没有更简单的方法来合并行并解决错误?

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;

namespace testone
{
    public partial class Form1: Form
    {
        int count = 1;
        int totalMBct = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;
            listView1.Columns.Add("P#", 80);
            listView1.Columns.Add("Size all in MB", 80);
            listView1.Columns.Add("Allocation Bit", 92);
            ListViewItem item1 = new ListViewItem("Free Space");
            SetHeight(listView1, 256);
            item1.BackColor = Color.Yellow;
            item1.UseItemStyleForSubItems = false;
            item1.SubItems.Add(new ListViewItem.ListViewSubItem(item1,
            "1000 MB", Color.Black, Color.Yellow, item1.Font));
            item1.SubItems.Add(new ListViewItem.ListViewSubItem(item1,
            "        0", Color.Black, Color.White, item1.Font));
            listView1.Items.Add(item1);
        }

        private void SetHeight(ListView listView, int height)
        {
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1, height);
            listView1.SmallImageList = imgList;
        }
        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {

            if (listView1.SelectedItems.Count == 1)
            {
                int iIndex = listView1.SelectedItems[0].Index;
                string content = listView1.Items[listView1.SelectedIndices[0]].SubItems[1].Text;
                string contentIF = listView1.Items[listView1.SelectedIndices[0]].Text;
                int newTotalct = +int.Parse(content);
                string iIndex1 = iIndex.ToString();
                string counter1 = listView1.Items.Count.ToString();
                MessageBox.Show("index " + iIndex1, "counter " + counter1 + " content" + totalMBct);
                if (contentIF != "Free Space" && totalMBct <= 1000 && iIndex != listView1.Items.Count - 1 && iIndex > 0)
                {
                    string content2 = listView1.Items[listView1.SelectedIndices[-1]].SubItems[0].Text;///LINE WHERE I GET ERROR NEED TO STORE CONTENT IN STRING!!
                    string content1 = listView1.Items[listView1.SelectedIndices[+1]].SubItems[0].Text;;///LINE WHERE I GET ERROR NEED TO STORE CONTENT IN STRING!!

                    MessageBox.Show("index " + iIndex1, " content" + content2);
                    if (content1 != "Free Space" && content2 != "Free Space")
                    {
                        listView1.Items.Remove(listView1.FocusedItem);
                        ListViewItem item3 = new ListViewItem("Free Space");
                        item3.BackColor = Color.Yellow;
                        item3.UseItemStyleForSubItems = false;
                        item3.SubItems.Add(new ListViewItem.ListViewSubIte(item3,
                        content, Color.Black, Color.Yellow, item3.Font));
                        item3.SubItems.Add(new ListViewItem.ListViewSubIte(item3,
                        "        0", Color.Black, Color.White, item3.Font));
                        listView1.Items.Insert(iIndex, item3);
                        SetHeight(listView1, 256 / listView1.Items.Count);
                        totalMBct = totalMBct - newTotalct;
                        if (totalMBct < 1000)
                        {
                MessageBox.Show("Enabling button again memory less then full!"); 
                            button1.Enabled = true;
                        }
                    }
                }

            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            int userVal = int.Parse(textBox1.Text);
            totalMBct += userVal;

            if (totalMBct >= 1000)
            {
                MessageBox.Show("The memory is Full no more entries!");
                button1.Enabled = false;
            }

            if (listView1.Items.Count > 1)
            {
                listView1.Items.RemoveAt(listView1.Items.Count - 1);
            }
            ListViewItem item = new ListViewItem();
            item.Text = "P" + count;
            item.BackColor = Color.LightSteelBlue;
            item.UseItemStyleForSubItems = false;
            item.SubItems.Add(new ListViewItem.ListViewSubItem(item,
            textBox1.Text, Color.Black, Color.LightSteelBlue, item.Font));
            item.SubItems.Add(new ListViewItem.ListViewSubItem(item,
            "        1", Color.Black, Color.White, item.Font));
            if (count == 1)
            {
                listView1.Items.Clear();
            }
            count++;
            listView1.Items.Add(item);

            if (totalMBct < 1000)
            {
                int NewFs = 1000 - totalMBct;
                string NewFs1 = NewFs.ToString();
                ListViewItem item2 = new ListViewItem("Free Space");
                item2.BackColor = Color.Yellow;
                item2.UseItemStyleForSubItems = false;
                item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2,
                NewFs1, Color.Black, Color.Yellow, item2.Font));
                item2.SubItems.Add(new ListViewItem.ListViewSubItem(item2,
                "        0", Color.Black, Color.White, item2.Font));
                listView1.Items.Add(item2);
                SetHeight(listView1, 256 / listView1.Items.Count);
            }

            textBox1.Clear();
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar&&char.IsDigit.KeyChar&&e.KeyChar= '.'))

            {
                e.Handled = true;
            }

            // only allow one decimal point
            if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') -1))
            {
                e.Handled = true;
            }

        }
    }
}

  ask by Lecy277 translate from so

本文未有回复,本站智能推荐: