473,394 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Richedit / Custom Control help for final university project

Lee
Hi all, I'm trying to programatically change the background color of
lines of text and running into a few difficulties, anyone got any
suggestions?

my relevant code is as follows:

public void colorLines() {
//_Paint = false; //don't want to update while we are doing
this
//loop thru lines like addText() does
//call checkColors(lineNo)
//print line
// _Paint = true; //now we update.
int curChar = 0;
for (int curLine=0; curLine < this.Lines.Length; curLine++)
{
if (this.Lines[curLine].Length > 0)
{
this.Select(curChar, this.Lines[curLine].Length);
this.SelectionBackColor =
checkColorByLine(curLine);
curChar += this.Lines[curLine].Length + 1;
this.SelectionBackColor = Color.Empty;
}
}
}

and

public Color checkColorByLine(int lineNo)
{
//search hash table for line no
//what color should that line no be?
//return LineColor
//System.Console.WriteLine("Checking Line: " + lineNo);
//System.Console.WriteLine("ColorTable Size: " +
colorTable.Count);
foreach (DictionaryEntry de in colorTable)
{
ArrayList tempArray = (ArrayList)de.Value;
for (int x = 0; x < tempArray.Count; x++)
{
if (tempArray[x].Equals(lineNo))
{
return (Color)de.Key;
}
}
}
return Color.Empty;
}

This is all done from within an extended rich text box control

What tends to happen is that more than one line of text gets colored...
I'm fairly sure I've got some broken logic or something, but I've been
staring at this code too long to work out what it is!

Cheers :)

Lee

Apr 20 '06 #1
8 2370
You are updating SelectionBackColor - but IIRC this is simply the color that
appears (for that control) when you select a block of text with the mouse;
you need to edit the RTF content itself - methinks you need to read up on
RTF codes

Marc
Apr 20 '06 #2
Lee
Well, as is it kind of works, it just has a habbit of selecting the
wrong thing.

(Essentially the code selects a block of text and gives it a back color
as if it were done with the mouse, except this isn't painted so it's
hidden from the user)

RTF codes are no use as it's a little tricker to handle them
dynamically (I'd have to parse each line of text, as in the contents
instead of just the line numbers) and as this is all due in on the
28th, I don't have the time to do that! :o

Thanks :)

Apr 20 '06 #3
Please ignore this; I may have the wrong end of the RichTextBox stick...

To attempt to salvage some pride (if possible), I will see if I can find the
actual problem...

Marc
Apr 20 '06 #4
Lee
If you manage to find the problem you not only salvage pride but also
gain a mention in my about box and my eternal gratitude ;)

Lee

Apr 20 '06 #5
Well, what exactly are you seeing? I've taken your code, and hacked it using
a round-robin colouring, and all is fine. Unfortunately, your code (alone)
doesn't illustrate the problem, as a: colorTable isn't defined / populated
(hence why I am using a round-robin), and b: you haven't included any sample
text that raises the problem.

I can, however, get some odd results with different line-endings; you code
assumes 1 character only, but you could get a cr-lf pair - and hence you end
up a character out. In terms of "more than 1 line of text gets coloured",
well it is hard to see what you mean without something that actually
illustrates this in progress.

Marc
Apr 20 '06 #6
Also - curChar is only incremented if the line is non-empty - so you will
get one character out-of-sync for every blank line in the text; perhaps this
is the real issue?

Marc
Apr 20 '06 #7
Lee
I could send you the whole project, but it's a lot.

The basic principle is as follows (sorry if this is too rough)

You type something into a text box
This is sent to a server on a character by character basis
Information is passed to the client telling it the current line we are
working on
this line is locked out from any other edits (i.e. only the person who
has started on this line may add or remove text on it)
This line is colored
The text is added to the second text box on our client
Process repeats.

Below is the entire class file for the control I've been writing (some
of it is a bit of a mess I'm afraid)
---------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace AffinityGui{

public partial class affinityEditBox : RichTextBox
{
///errors that are likely to show up
///if someone trys to insert a line this may go tits up, I
don't know in what way yet though
///amendum: think inserting a line is fixed.
private int cursorPos;
private Hashtable colorTable = new Hashtable();
private ArrayList lockedLines = new ArrayList();
public static bool _Paint = true;
const short WM_PAINT = 0x00f;
const char BACKSPACE = (char)8;

public affinityEditBox()
{
InitializeComponent();
//this.SetStyle(ControlStyles.DoubleBuffer, true);
}
//check if a line is locked based on line no
public Boolean LineLocked(int lineNo)
{
if (lockedLines.Equals(null))
{
return false;
}
else
{
if(lockedLines.Contains(lineNo)) {
return true;
} else {
return false;
}
}
}
public Color checkColorByLine(int lineNo)
{
//search hash table for line no
//what color should that line no be?
//return LineColor
//System.Console.WriteLine("Checking Line: " + lineNo);
//System.Console.WriteLine("ColorTable Size: " +
colorTable.Count);
foreach (DictionaryEntry de in colorTable)
{
ArrayList tempArray = (ArrayList)de.Value;
for (int x = 0; x < tempArray.Count; x++)
{
if (tempArray[x].Equals(lineNo))
{
Console.WriteLine("LineNo: " + lineNo);
Console.WriteLine("Returning: " + de.Key);
return (Color)de.Key;
}
}
}
return Color.Empty;
}
public void colorLines() {
//_Paint = false; //don't want to update while we are doing
this
//loop thru lines like addText() does
//call checkColors(lineNo)
//print line
// _Paint = true; //now we update.
int curChar = 0;
for (int curLine=0; curLine < this.Lines.Length; curLine++)
{
if (this.Lines[curLine].Length > 0)
{
this.Select(curChar, this.Lines[curLine].Length);
this.SelectionBackColor =
checkColorByLine(curLine);
curChar += this.Lines[curLine].Length + 1;
this.SelectionBackColor = Color.Empty;
}
}
}

public void lockLines(int lineNo)
{
lockedLines.Add(lineNo);
}
public void unLockLines(int lineNo)
{
lockedLines.Remove(lineNo);
}

public void addColorBlock(ArrayList lines, Color color)
{
System.Console.WriteLine("Color: " + color.ToString());
//method to add a set of lines to a color
if (!colorTable.ContainsKey(color))
{
colorTable.Add(color, lines);
//System.Console.WriteLine("****colorTableSize: " +
colorTable.Count);
}
else
{
//do something else
colorTable.Remove(color);
colorTable.Add(color, lines);
}
/**
_Paint = false;
colorLines(); //make sure _Paint is still false while we do
this!
_Paint = true;
**/
}

public ArrayList getColorLinesByKey(Color key)
{
//get the lines that a color owns by it's key (user)
foreach (DictionaryEntry de in colorTable)
{
if (de.Key.Equals(key))
{
return (ArrayList)de.Value;
}
}
return null;
}
public void delText(int startPos, int len)
{
_Paint = false; //don't paint
/*
if (len > 1)
{
startPos -= len;
//startPos--;
}*/
string temp = this.Text.Remove(startPos, len);
this.Text = temp;
_Paint = true;
}

//method for insterting text at arbituary points
public void addText(int lineNo, int stringPos, string text)
//text should really be a chr seeing as we are only dealing with chr by
chr
{
/* lineNo is for selecting the line of text we want to edit
* stringPos is for selecting the position of the string we
want to edit from
* this pretty much a find and replace
*/
cursorPos = this.SelectionStart;
ArrayList tempArl = new ArrayList(this.Lines);
if (lineNo == tempArl.Count)
{
//System.Console.WriteLine("LineNo and text edit are
exactly the same size");
string tempStr = " ";
for (int curPos = 0; curPos < stringPos; curPos++)
{
tempStr += " ";
}
//}
//tempArl.Add(tempStr);
//tempArl.Add(tempStr);

}
else if (lineNo > tempArl.Count)
{
//System.Console.WriteLine("lineNo is bigger than text
edit space");
string tempStr = " ";
int diff = lineNo - tempArl.Count;
//diff += 2;
for (int current = 0; current < diff; current++)
{
//if (current == diff)
//{
for (int curPos = 0; curPos < stringPos;
curPos++)
{
tempStr += " ";
}
//}
tempArl.Add(tempStr);
}
//tempArl.Add(tempStr);

}
// Create a string array and store the contents of the
Lines property.
string[] tempArray = new string[tempArl.Count];
//System.Console.WriteLine("lineNo: " + lineNo);
//System.Console.WriteLine("tempArl: " + tempArl.Count);
tempArray = (string[])tempArl.ToArray(typeof(string));
tempArray[lineNo] = tempArray[lineNo].Insert(stringPos,
text); //ohhh a one liner for inserting strings, score!

//loop through array and whack it into the rich text box
(rtbMain)
_Paint = false;
//this.Clear(); <=--- if we put this line of code in we get
flicker...
String tempText = "";
for (int counter = 0; counter < tempArray.Length;
counter++)
{
tempText += tempArray[counter] + "\n"; //done to
minimize the number of updates on the text box
}
this.Text = tempText;
colorLines(); //make sure _Paint is still false while we do
this!
_Paint = true;
this.SelectionStart = cursorPos + text.Length;
this.SelectionLength = 0;
}
public int CurrentColumn
{
get { return CursorPosition.Column(this, SelectionStart); }
}

protected override void WndProc(ref
System.Windows.Forms.Message m)
{

// sometimes we want to eat the paint message so we don't
have to see all the
// flicker from when we select the text to change the
color.
if (m.Msg == WM_PAINT)
{

if (_Paint)
{

base.WndProc(ref m);
}
else
{

m.Result = IntPtr.Zero;
}
}

else
{

base.WndProc(ref m);
}

}
}
internal class CursorPosition
{
[System.Runtime.InteropServices.DllImport("user32")]
public static extern int GetCaretPos(ref Point lpPoint);

private static int GetCorrection(RichTextBox e, int index)
{
Point pt1 = Point.Empty;
GetCaretPos(ref pt1);
Point pt2 = e.GetPositionFromCharIndex(index);

if (pt1 != pt2)
return 1;
else
return 0;
}

public static int Line(RichTextBox e, int index)
{
int correction = GetCorrection(e, index);
return e.GetLineFromCharIndex(index) - correction + 1;
}

public static int Column(RichTextBox e, int index1)
{
int correction = GetCorrection(e, index1);
Point p = e.GetPositionFromCharIndex(index1 - correction);

if (p.X == 1)
return 1;

p.X = 0;
int index2 = e.GetCharIndexFromPosition(p);

int col = index1 - index2 + 1;

return col;
}
}
}

Apr 20 '06 #8
Sorry, but there's way too much there that is unrelated, and nothing
(visibly) here that actually reproduces what you are seeing (i.e. the values
of .Text and colorTable). I think what you need to do here is to boil this
down to the simplest thing that introduces your problem... in fact, your
/previous/ code would suffice, if you just added (somewhere) a definition
for colorTable (along with some suitable contents), and a value for .Text
that shows the problem.This would allow the many readers here to attempt to
reproduce and resolve it with you.

In reality, the simple fact of doing the above usually helps the original
developer find the problem themselves, which is also a bonus. Sorry if this
doesn't sound helpful, but it is the best approach here...

Jon Skeet has a very good article on this:
http://www.pobox.com/~skeet/csharp/complete.html

Marc

Apr 20 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: PowerPlane | last post by:
Hi, all Happy valentine I am struggling to insert an animated-gif into a CRichEditCtrl. Before I post this question here, I did some googling. And I found an article to introduce how to insert a...
1
by: Patty O'Dors | last post by:
Hi I'm trying to create an application with a RichEdit control, I found that CreateWindow always returns a null hWnd unless I call LoadLibrary("riched20.dll") first (the error code from...
10
by: Patty O'Dors | last post by:
Hi In trying to create a RICHEDIT-derived ActiveX control in ATL, I've managed to successfully implement the stock font property, thanks for all your help on this. I'm now stuck on receiving...
3
by: Michael | last post by:
Hi all.. I have a solution containing 5 projects, one of which is a custom control. In one of the projects, I dragged the .ascx file from the custom control project and dropped it onto a form....
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
1
by: Claire | last post by:
I'm writing an application with custom skinned controls. I have a text editor form with a rich edit control. Because there's no toolbuttons in the skinned control set, I have to use normal ones for...
13
by: ravi | last post by:
I am a final year computer engineering student i am looking for some good as well as tough project ideas in C/C++ Can anybody help me by giving their ideas Thanks in advance.
6
by: truezplaya | last post by:
Hi all I am currently in the situation of deciding what to do for my final year project. I was wondering if any had already done such a project and if they could shed any light on the level of...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.