Simon Wpgn Lewis February 2016

Richedit and remove all text from a person content

So I got talking to one of my people and there feel it would be a good idea to hash out all the current chat that was sent by a blocked person a bit like how TwitchTV chat works if person is blocked it would remove all the persons chats and replace with ***

now it was late when we were taking so my brain don't work so well at 5am :D but now am up I been thinking even more on how to do this I guess it would have to be string list with current incoming chat then search for the persons name and then remove all the text after persons name

chat format is as so

Simon: my test message

so after being muted the chat out come would look like

Simon: ** **** *******

any one ever done this at all seams very complex

Thanks

Answers


MartynA February 2016

If I'm understanding correctly what you want to do, it should actually be straightforward;

Forget the TRichEdit for a moment, because the solution to this problem is about data structures, and the RichEdit is merely a way of surfacing what you want in your GUI.

Suppose you have a TStringList, ChatMessages, to which you append a user's name and what they've typed, and another TStringList, Users, which contains a list of their names.

Then, to achieve the functionality you want, you need to keep track of "who said what", and the Objects property of the ChatMessages TStringList can be used for that.

So, you might have a couple of methods like this:

function TForm1.GetUserID(const UserName : String) : Integer;
begin
  Result := Users.IndexOf(UserName) + 1;
  if Result = 0 then begin
    Users.Add(UserName);
    Result := Users.IndexOf(UserName) + 1;
  end;
  Assert(Result > 0);
end;

procedure TForm1.AddChatMsg(UserName, Msg : String);
var
  UserID : Integer;
begin
  UserID := GetUserID(UserName);
  ChatMessages.Add('');
  ChatMessages.AddObject(UserName + ': ' + Msg, TObject(UserID));
end;

and another procedure to periodically update the RichEdit's contents from ChatMessages.

Then, when you want to "twit" a user, you can search through ChatMessage's Objects looking for their ID number. You can then replace what follows in their name-tag lines by asterisks, likewise in the following lines until you find another user's user-ID in the objects or reach the end of ChatMessages. Coding of that is left as an exercise for the reader.

It doesn't matter whether you do the asterisks in the ChatMessage text, or as you copy its text into the richEdit, except on course that if you do it in the RichEdit you can "untwit" the user later by refreshing it from ChatMessages without the asterisk substitution.

Note that GetUserID above bases the user's ID on their index in the Users list (plus 1

Post Status

Asked in February 2016
Viewed 3,126 times
Voted 10
Answered 1 times

Search




Leave an answer