|
Hello,
I'd like to make a (read only) console component.
Almost a multiline TextBox in read only with a fixed number of lines.
As a DOS console which use a fixed buffer, the old lines should be discarded.
To limit the line number, I tryed to limit the character number (which is ok in my case) like so :
int maxNumCharacter = 10000;
public void AppendText(String text)
{
textBox.AppendText(text);
if (textBox.Text.Length > MaxNumCharacter)
textBox.Text = textBox.Text.Remove(0, textBox.Text.Length - maxNumCharacter);
}
But setting the Text property is not that good because :
- It replaces all the textBox characters. It's slow.
- It also has the effect to move the scrollbar to the top and I need it allways at the bottom if the user doesn't move it.
Is there any way to do such a component ?
Thanks.
|
|
|
|
|
Hi,
if your Control is to be line-oriented then I would go for a ListBox or a ListBox derivative.
The advantages are:
- its Items collection holds items of arbitrary type, could be simply strings.
- lines never get concatenated, they are kept in the Items collection; fast and easy
- you can limit the number of items you keep, i.e. you could append to the bottom,
and, if required, remove lines from the top
- it offers scroll bars, so you can easily do a scrolling DOS window, holding much more
lines than your screen could show at once.
The disadvantages are:
- selection, if required, is by lines, not an arbitrary from-to selection
- maybe some more, nothing comes to mind
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Thanks for your answer.
I thought about using a listbox but the input text should not allways be formated line by line. As you pointed the selection cannot be arbitraty in this case.
Also, I think its quite heavy to use a listbox for this simple stuff (actually its nothing more than a fixed text buffer). I need it to be really fast and light.
I use it as an output log for a little game editor. This component can be filled 60 times per second if a log is made in the update loop.
|
|
|
|
|
ListBox is ideal for logging; there is simply nothing else I would consider for logging.
I would never consider a TextBox for big, live pieces of text (anything larger than
3 lines of text, and/or adding/replacing 3 lines per second).
I have ListBoxes that consume 100 lines per second and more.
And I forgot at least one more advantage:
you can make the ListBox OwnerDraw, meaning you draw the lines yourself.
I do this for highlighting (painting some lines red or bold).
If handled correctly, there should be no speed penalty involved.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I agree. Listbox can be really good in many cases. But I guess it a question of taste.
For example in visual studio you can use both, the Output and/or the Tasklist.
I prefer to use the Output.
In my case the output text is formated by the user (a developper).
I cannot assume any formating, he is free. He should be able to write anything he wants and then select on one or multiple line (columns selection would be great too, as a CMD window)
As you said, with listbox you can use color, draw the lines yourself, etc..
It has a cost. Drawing a simple character buffer and updating only what needs to be updated is a lot faster than the insertion of listbox items, the selection of their colors (according a keyword in the line for example, etc...)
I may use a listbox component later for this purpose. Right now I need something different.
Thanks.
|
|
|
|
|
I have an nterview where I was asked What is useful to use interface or abstract classes.
When We use interface and when we should use abstract classes?
Thanks in advance
seema
|
|
|
|
|
|
An interface can be used in multiple, unrelated classes ( think IDisposable ). An abstract base class allows you to store a base type, for example, whenever I write paint tools, I use an abstract base type 'Tool' class, and store an instance of it in the control that uses it, it's always a type of tool, but the base class gives me a way to store any of them.
I've actually never had an interview for a company that didn't end up offering me a job. My first ever interview was the only one I failed, and I bought books and learned all I needed to go back a year later, and get a job from them. You need to move past the specific questions they asked and ask yourself what your inability to answer them tells you about broader areas in which you need to improve your knowledge.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
The usage of interface is like a contract. That is to use it to control the entry port and the output to fit some rule that you need.
And abstract class is like a context. It's containing the common attributes and deflaut behaviors those could be extented in the subclasses.
When we use it is meaning how do we use interface and abstract classes.
Use interfaces when something will change frequently. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent for it. It let you define some behaviors, they force your subclasses to provide others
Jeff
|
|
|
|
|
Hi Seema,
Interfaces are essential when programming using Component Orientated Design as opposed to OOD. In general I tend towards using Interface inheritance as I code for components, not objects. Components are more flexible and reusable, in no small part due to the way they interact using Interfaces and thus separate interface from implementation, making reimplementation or reuse easier.
Interface based inheritance means simplifies greatly the use of inheritance and polymorphism as abstract classes may implement member variables or provide virtual functions as well as abstract functions which can make the hierarchy more fragile and prone to reuse issues. If you need common functionality across a number of subclasses then an abstract class is useful; it is, however, also useful to make that abstract class implement an interface for its subclasses to use as their contract with their clients as well.
A class may only implement one base class, but can implement multiple interfaces. If an interface implements another interfaces then classes that implement that interface will have to implement the methods for both. Conflicts may be resolved via explicit interface inference or by channeling both into one method (or a combination). As with abstract classes, interfaces may be Generic and decorated with attributes.
Interfaces are a very useful and powerful construct. I would recommend their use heavily to achieve a component based architecture rather than an object based architecture.
Toby
Toby Russell
|
|
|
|
|
Hi everyone
I need to add a tooltiptext in that yellow rectangle for each node in a treeview in C# . is that possible , anyone has an Idea ?
Thanx
UnitecSoft
We Will Either Find A Way Or Make One .
|
|
|
|
|
There are CodeProject articles and discussion threads on such topic. Use the search
facilities.
And yes it is possible, basically you would have to change the tooltiptext while hovering
the nodes.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Thanks Luc
Yes , sure you can change the tooltiptext of the tree , but there is no event to be triggered returning the node name or path when hovering over the tree . Nodes are not treated as objects in the tree , there is no Mousehover event for each node .
I did try to find articles on the same subject in codeproject , but could not find any , if you know any of these articles please tell me .
Thanks
UnitecSoft
We Will Either Find A Way Or Make One .
|
|
|
|
|
class Parent
{
}
class Child: Parent
{
string value="asdf";
}
/\ |_ E X E GG
|
|
|
|
|
Define a protected property or method overridable by the derived class. Example
class Parent
{
protected virtual string Value
{
get{;}
}
private string GetDerivedValue()
{
return Value;
}
}
class Child: Parent
{
string value="asdf";
protected override Value
{
get{return value;}
}
}
Visit my blog at http://dotnetforeveryone.blogspot.com/
|
|
|
|
|
To access the value there, you need to use the 'as' keyword, as in
Parent p = this as Parent;
If p != null, then the object was indeed of the parent type, and you can then use the p object to access public properties ( and, I assume, protected ones )
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
But wouldn't that violate OOP principles? (The parent knowing too much about the child.)
Wouldn't the "proper" way be for the parent to specify that the child must have a particular method?
class Parent
{
protected abstract string Value{get;} ;
public override string ToString()
{
return ( Value ) ;
}
}
class Child : Parent
{
string value="asdf";
protected override string Value
{
get { return ( this.value ) ; }
}
}
(I hope that's correct.)
|
|
|
|
|
yes, you're right. The 'proper' was is definately for the property to be defined in the base method. But, if that's not possible, and if there's no way around it, what I described is the way to achieve what was asked for.
Another way would be to define the property through an external interface, which is implimented by the derived class, and which is used in the 'as' call in the base class.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I don't even think that would work, Parent doesn't declare value. And the further problem would be if a child declares value as some unexpected type or not at all.
The base class must either include the declaration of value or specify an abstract method either directly or via an interface.
|
|
|
|
|
Have anyone used ngen.exe to generate native images of an assembly? I need to know if it has real utility...
Any sample will be welcome. Thanks in advance.
Visit my blog at http://dotnetforeveryone.blogspot.com/
|
|
|
|
|
Yes, I've done it before. It can speed up cold boots of your app significantly and warm boots somewhat. You can read more articles about it on MSDN. Here's one[^] for you.
|
|
|
|
|
How can I get the collection of field values from the below class.
So consider this instance of SentRecord
SentRecord sr=new SentRecord(1,new Subscriber(), DataTime.Now, false, "C:\img.jpg", 0);
Now how can I get all fields and values from sr. e.g.:
SentRecordID => 1
ContentCampaign => [ContentCampaign object]
Subscriber => [Subscriber object]
TimeSent => [DateTime object]
Confirmed => false
FileSent => "C:\img.jpg"
AdIDDelivered => 0
public class SentRecord : Model
{
public SentRecord(int contentID, Subscriber subscriber, DateTime timeSent, bool confirmed, string fileSent, int adIDDelivered)
{
this.ContentCampaign.ContentID = contentID;
this.Subscriber = subscriber;
this.TimeSent = timeSent;
this.Confirmed = confirmed;
this.FileSent = fileSent;
this.AdIDDelivered = adIDDelivered;
}
public int SentRecordID;
public ContentCampaign ContentCampaign;
public Subscriber Subscriber;
public DateTime TimeSent;
public bool Confirmed;
public string FileSent;
public int AdIDDelivered;
}
/\ |_ E X E GG
|
|
|
|
|
Look for these classes in MSDN.
1. System.Type
2. System.Reflection.FieldInfo.
Call GetField() method of your type and GetValue() method of FieldInfo of your members.
Farhan Noor Qureshi
|
|
|
|
|
I'm having trouble using GetValue()
Type t = this.MetaClass.modelObject.GetType();
this.MetaClass.TableName = t.FullName.Substring(t.FullName.LastIndexOf(".")+1);
FieldInfo []fields=t.GetFields();
this.MetaClass.fields =new string[fields.Length];
this.MetaClass.values = new string[fields.Length];
for (int i = 0; i < fields.Length; i++)
{
this.MetaClass.fields[i] = fields[i].Name;
}
I'm able to get all the field names, but I can't get the values of the fields. See the commented line.
Any ideas?
/\ |_ E X E GG
|
|
|
|
|
Hi
Try the following example that iterate over all the fields of an object and print the field name and value
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Test
{
public class CustomizeField
{
public CustomizeField(int x, int y)
{
m_x = x;
m_y = y;
}
public int m_x;
public int m_y;
public override string ToString()
{
return string.Format("X:{0}, Y:{1}", m_x, m_y);
}
};
public class SentRecord
{
public SentRecord(
DateTime timeSent,
bool confirmed,
string fileSent,
int adIDDelivered)
{
this.customizeField = new CustomizeField(10, 15);
this.TimeSent = timeSent;
this.Confirmed = confirmed;
this.FileSent = fileSent;
this.AdIDDelivered = adIDDelivered;
}
public CustomizeField customizeField;
public DateTime TimeSent;
public bool Confirmed;
public string FileSent;
public int AdIDDelivered;
}
class Program
{
static void Main(string[] args)
{
SentRecord sr = new SentRecord(DateTime.Now, true, @"c:\image.jpg", 10);
FieldInfo[] fields = sr.GetType().GetFields();
foreach (FieldInfo fi in fields)
{
Console.WriteLine(fi.Name + " " + fi.GetValue(sr).ToString());
}
}
}
}
|
|
|
|