在C#中将排序和未排序的整数添加到列表框

时间:2017-12-08 10:31:20

标签: c# sorting listbox

在我的系统中,我有一个文本框和按钮,允许用户在列表框中输入一个整数。但是,我想要包括两个单选按钮 - 排序和未排序,以便用户必须选择是否要在添加时对整数进行排序或取消排序。

这是我到目前为止添加按钮的代码;

private void buttonAdd_Click(object sender, EventArgs e)
{
    listBoxAddedIntegers.Items.Add(textBoxInsert.Text);
    textBoxInsert.Text = string.Empty;
    //MessageBox.Show(listBoxAddedIntegers.SelectedIndex.ToString());
    MessageBox.Show(listBoxAddedIntegers.Items.Count.ToString());
}

这是单选按钮的代码'已排序;

private void radioButtonSorted_CheckedChanged(object sender, EventArgs e)
{
   radioButtonSorted.Checked = true;
}

这是'未排序'单选按钮的代码 -

private void radioButtonUnsorted_CheckedChanged(object sender, EventArgs e)
{
   radioButtonSorted.Checked = false; 
}

有没有人对如何在列表中添加整数有任何建议,以便当用户选择单选按钮'已排序'然后单击添加整数时,整数会被添加到排序列表中?谢谢。

2 个答案:

答案 0 :(得分:2)

使用单选按钮切换列表框的Sorted属性。根据{{​​3}},它也确保了

  

[as]项目被添加到已排序的ListBox中,这些项目将被移动到已排序列表中的相应位置

所以,你可以写

private void radioButtonSorted_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked) listBoxAddedIntegers.Sorted = true;
}

private void radioButtonUnsorted_CheckedChanged(object sender, EventArgs e)
{
    if((sender as RadioButton).Checked) listBoxAddedIntegers.Sorted = false; 
}

您使用了CheckedChanged事件。它不仅会在选择单选按钮时触发,而且还会触发另一个未选中的单选按钮。因此,处理程序中需要documentation

但是有一个缺点:Sorted仅限于字母顺序。如果你得到1分,10分,2分,3分,但是期望1,2,3,10,那么你可以用零填充你的整数得到0001,002,0003,0010;或者应用预先对数据进行排序的query the actual check state,然后刷新整个列表框。

答案 1 :(得分:0)

如果选中已排序,这将对列表框中的所有项目进行排序,但我认为没有必要使用2个redio按钮。您可以用单个复选框替换它

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    // Set the new schema version. This must be greater than the previously used
    // version (if you've never set a schema version before, the version is 0).
    config.schemaVersion = 6;

    // Set the block which will be called automatically when opening a Realm with a
    // schema version lower than the one set above
    config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {

    };

    // Move the realm database to the shared group
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *currentRelamUrl = config.fileURL;

    NSURL *appGroupUrl = [fileManager containerURLForSecurityApplicationGroupIdentifier:@"*****"];
    NSURL *realmUrl = [appGroupUrl URLByAppendingPathComponent:@"default.realm"];

    //Moves the realm to the new location if it hasn't been done previously
    if ([fileManager fileExistsAtPath:currentRelamUrl.path] &&
        ![fileManager fileExistsAtPath:realmUrl.path]) {

        NSError *error = nil;
        [fileManager moveItemAtURL:currentRelamUrl
                             toURL:realmUrl
                             error:&error];

        config.fileURL = realmUrl;

        if (error) {
            NSLog(@"Error moving realm file url %@", error);
        }
    } else if (![config.fileURL.path isEqualToString:realmUrl.path]) {
        config.fileURL = realmUrl;
    }

    config.objectClasses = @[ChatMessage.class, [User class]
                             ];



    // Tell Realm to use this new configuration object for the default Realm
    [RLMRealmConfiguration setDefaultConfiguration:config];



    // Now that we've told Realm how to handle the schema change, opening the file
    // will automatically perform the migration
    [RLMRealm defaultRealm];

    return config;