Click here to Skip to main content
16,005,037 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
GeneralRe: A particular bug-bear of mine. Pin
Ed.Poore3-Jan-07 8:50
Ed.Poore3-Jan-07 8:50 
AnswerRe: A particular bug-bear of mine. Pin
Code_Doctor16-Apr-07 18:10
Code_Doctor16-Apr-07 18:10 
QuestionSelection, as if it were text. Pin
Captain See Sharp27-Dec-06 10:01
Captain See Sharp27-Dec-06 10:01 
AnswerRe: Selection, as if it were text. Pin
J. Dunlap28-Dec-06 1:37
J. Dunlap28-Dec-06 1:37 
GeneralRe: Selection, as if it were text. Pin
Captain See Sharp28-Dec-06 8:51
Captain See Sharp28-Dec-06 8:51 
GeneralRe: Selection, as if it were text. Pin
J. Dunlap28-Dec-06 14:58
J. Dunlap28-Dec-06 14:58 
GeneralRe: Selection, as if it were text. Pin
Captain See Sharp28-Dec-06 15:33
Captain See Sharp28-Dec-06 15:33 
GeneralRe: Selection, as if it were text. [modified][modified] Pin
Captain See Sharp3-Jan-07 13:33
Captain See Sharp3-Jan-07 13:33 
I think I got it. I just want to share the code in case you or someone else finds that its flawed or something.

UPDATE: I am currently testing it and it seems to work as planned. I think this should work for about any control that selects sequential data such as a text box or whatever you can think of.

public void SelectData(long start, long end)
{
  //if the range of data being selected does not join with any other selections
  //or there are no selections then this will remain true
  bool addNewRange = true;
  byte inc = 0;
  int insertIndex = _selected.BinarySearch(new Range(start, 0)); //search for the appropriate index to insert a
                                                                 //new selection range
  if (insertIndex < 0)  //if there is not already data selected at the start index then
      insertIndex = ~insertIndex; //figure out where the selection range should be placed

  if (_selected.Count > 0)  //if there is no selected data then checking for range redundancies is not needed
  {
    //if there is only 1 selection range and the new selection needs to be placed at index 1 then
    if (_selected.Count == insertIndex)
    {
      insertIndex--;
      inc = 1;
    }

    if (end >= _selected[insertIndex].MinValue - 1)  //check for reducdancies (overlapping)
    {
      if (start < _selected[insertIndex].MinValue)
      {
        _selected[insertIndex] = new Range(start, _selected[insertIndex].MaxValue);
        addNewRange = false;
      }

      if (end > _selected[insertIndex].MaxValue && start < _selected[insertIndex].MaxValue)
      {
        addNewRange = false;

        _selected[insertIndex] = new Range(_selected[insertIndex].MinValue, end);

        //if the new selection range is longer to the right of the current overlapping range then see how far it goes
        //and remove redundancies
        for (int i = insertIndex + 1; i < _selected.Count; i++)
        {
          if (end < _selected[i].MinValue - 1) break;

          if (end >= _selected[i].MinValue - 1 && end <= _selected[i].MaxValue)
          {
            _selected[insertIndex] = new Range(_selected[insertIndex].MinValue, _selected[i].MaxValue);
            _selected.RemoveAt(i);
            i--;
            continue;
          }
          //if end is greater than i's maxvalue then just remove it because it completely overlaps it
          if (end > _selected[i].MaxValue)
          {
            _selected.RemoveAt(i);
            i--;
          }
        }
      }
    }

    //this block checks to see if the new selection overlaps the selection to the left
    if (insertIndex > 0)
    {
      if (start <= _selected[insertIndex - 1].MaxValue + 1)
      {
        if (!addNewRange)
          end = _selected[insertIndex].MaxValue;

        addNewRange = false;
        _selected[insertIndex - 1] = new Range(_selected[insertIndex - 1].MinValue, end);
      }
    }
  }

  if(addNewRange)
    _selected.Insert(insertIndex + inc, new Range(start, end));

}


-- modified at 1:38 Thursday 4th January, 2007
-- modified at 21:34 Thursday 4th January, 2007



-- modified at 22:11 Thursday 4th January, 2007


█▒▒▒▒▒██▒█▒██
█▒█████▒▒▒▒▒█
█▒██████▒█▒██
█▒█████▒▒▒▒▒█
█▒▒▒▒▒██▒█▒██

QuestionNew Forum Pin
Bradml25-Dec-06 0:13
Bradml25-Dec-06 0:13 
GeneralAwesome! Pin
J. Dunlap24-Dec-06 14:51
J. Dunlap24-Dec-06 14:51 
GeneralFirst Square Post On A New Forum Pin
Bassam Abdul-Baki23-Dec-06 9:04
professionalBassam Abdul-Baki23-Dec-06 9:04 
JokeWhy Discriminate? Pin
CPallini23-Dec-06 23:31
mveCPallini23-Dec-06 23:31 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad26-Dec-06 18:24
professionalPaul Conrad26-Dec-06 18:24 
GeneralRe: First Square Post On A New Forum Pin
User 171649227-Dec-06 1:52
professionalUser 171649227-Dec-06 1:52 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 6:11
professionalPaul Conrad27-Dec-06 6:11 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 7:26
professionalBassam Abdul-Baki27-Dec-06 7:26 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 7:45
professionalPaul Conrad27-Dec-06 7:45 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 9:44
professionalBassam Abdul-Baki27-Dec-06 9:44 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 10:11
professionalPaul Conrad27-Dec-06 10:11 
GeneralRe: First Square Post On A New Forum Pin
User 171649227-Dec-06 12:57
professionalUser 171649227-Dec-06 12:57 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 15:05
professionalPaul Conrad27-Dec-06 15:05 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 2:17
professionalBassam Abdul-Baki27-Dec-06 2:17 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 6:11
professionalPaul Conrad27-Dec-06 6:11 
GeneralRe: First Square Post On A New Forum Pin
Bassam Abdul-Baki27-Dec-06 7:27
professionalBassam Abdul-Baki27-Dec-06 7:27 
GeneralRe: First Square Post On A New Forum Pin
Paul Conrad27-Dec-06 7:47
professionalPaul Conrad27-Dec-06 7:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.