|
I was drawing the text outside the Paint event because the Panel is created in a different class while the function that draws the text is found in another class in the application I am writing (Not the example shown as a test). But I think I should do it in a Paint event. I want to install a Paint event for the Panel in the class within which the function that draws the text is implemented. Yet to do it but I hope that will work. Thanks.
|
|
|
|
|
One way to handle this is to create a list of items you want to paint (ordered by the order you want to paint them), and have each one use it's own paint method. Then you just iterate over this list in the paint event and call the appropriate draw. I would typically do something like this:
public abstract class PaintBase
{
public void Draw(Graphics graphics)
{
OnDraw(Graphics graphics);
}
protected abstract OnDraw(Graphics graphics);
} Then, you derive your classes from this:
public class PanelText : PaintBase
{
public Font DrawFont { get; set; }
public string Text { get; set; }
protected override OnDraw(Graphics graphics)
{
graphics.DrawString(Text, DrawFont, Brushes.Black, 50, 50);
}
} Then, in your form, create a list of type PaintBase and add what you need to it, as in:
private List<PaintBase> paintables = new List<PaintBase>();
private void AddPaintables()
{
PanelText panelText = new PanelText();
panelText.DrawFont = panel.Font;
panelText.Text = "Problem drawing text on panel";
paintables.Add(panelText);
} Finally, in your paint handler, all you would do is iterate like this:
foreach (PaintBase item in paintables)
{
item.Draw(e.Graphics);
} This is a very rough implementation - it's missing a lot of error handling, validation, etc, but on the whole, you should be able to get the idea. Note that I've just knocked this up in the message editor, so I apologise if the syntax isn't 100%; it should serve to get you started though.
|
|
|
|
|
Very cool, Pete, this to me exemplifies one of the cases of "gems submerging in the forums," where the post, imho, deserves elevation to some "archived Valhalla," without requiring the author to write a Tip/Trick or article.
As you know, I have been commenting on issues like this in the "Buggs and Suggs" forum.
best, Bill
"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman
|
|
|
|
|
Hi
Dan_K ,
To draw object under any control, you must implement the event "OnPaint".
You can use this code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
panel.Paint += new PaintEventHandler(panel_Paint);
}
private void void panel_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString("Problem drawing text on Panel", panel.Font, Brushes.Black, 50, 50);
}
}
}
|
|
|
|
|
Hi all
Here is my conundrum: I have a helper class/function that says if a date is a working day.
It does this by reading in the holidays from the database and checking the date against weekends, and then holidays.
It holds the holidays in a dictionary keyed by date for quick lookup and caching.
It is passed an interface to a holiday repository in the ctor and everything is injected via Castle Windsor ctor injection from a root entity.
It needs to be an in-memory cache and readily accessible as it is used a lot.
So far so good.
How would you go about making this a static extension method to DateTime?
(Since you can’t inject to static classes…)
The basic problem is, can you pass stuff into an extension method using CW? Or am I stuck with injection all the way and no statics, and therefore no extension methods?
I came up with a hack that uses a collection in the static helper class, and gets set with the holiday cache beforehand, but it just seemed a bit klunky...
Thanks in advance for any ideas...
|
|
|
|
|
Uh, am I missing something obvious?
public static bool IsWorkingDay(this DateTime dt){
return false;
}
permits
bool shouldIBeWorking = DateTime.Now.IsWorkingDay();
(Is it possible to have extension properties? I don't think so, right?)
This is completely independent of dependency injection and as long as the extension method is visible in the place you're trying to use it (i.e. in the same assembly, or a referenced assembly, and you have a using statement for the class that defines it) it should work anywhere.
|
|
|
|
|
I'll try again with an example:
Here is the current naive implementation:
public static class DateExtension
{
static readonly Dictionary<DateTime, Holiday> Dict;
static DateExtension()
{
Dict = new Dictionary<DateTime, Holiday>();
}
public static void SetHoliday(Holiday holidayToAdd)
{
Dict.Add(holidayToAdd.Date, holidayToAdd);
}
public static bool IsWorkingDay(this DateTime dateToCheck)
{
if (dateToCheck.DayOfWeek == DayOfWeek.Saturday ||
dateToCheck.DayOfWeek == DayOfWeek.Sunday)
{
return false;
}
return !Dict.ContainsKey(dateToCheck);
}
}
public class Holiday
{
public virtual DateTime Date { get; set; }
public virtual string Description { get; set; }
}
To use this, you have to stuff the DateExtension with the holidays first
Holiday hol = new Holiday {Date = new DateTime(2012, 1, 2), Description = "NYD 2012"};
DateExtension.SetHoliday(hol);
So you would use it like this:
DateTime dt = new DateTime(2012, 1, 2);
Debug.Assert(!dt.IsWorkingDay());
dt = dt.AddDays(1);
Debug.Assert(dt.IsWorkingDay());
Now, the question is about, how can I nicely get CW to instantiate the holidays into the static extension, without doing something like this:
var holRep = _container.Resolve<IHolidayRepository>();
DateExtension.SetAllHolidays(holRep.All());
In other words, can anyone think of a way to do this automatically within CW by simply registering the static class DateExtension in CW and have it inject the repository for me?
Thx
|
|
|
|
|
Ah, okay, now I see the problem.
The easiest way is to register in reverse, i.e. pass a container to the extension class:
static class DateExtensions {
static IUnityContainer container;
static HolidayHandler handler;
public static void RegisterHolidayHandler(IUnityContainer container){
container.Register<HolidayHandler, HolidayHandler>();
this.container = container;
}
public static bool IsHoliday(){
if(handler == null) handler = container.Resolve<HolidayHandler>();
}
class HolidayHandler {
internal List<Holidays> holidays;
public HolidayHandler(EntityContext context){
holidays = context.GetHolidays();
}
}
}
If you want some syntactic tidyness then of course RegisterHolidayHandler can itself be an extension method.
If you don't want to have a dependency on the dependency injection framework here (and I don't think you do, really), you need to give the extension class a property (of type IHolidayHandler or something) and explicitly assign an instance of a subclass which is populated by IoC. This is essentially 'loading the holidays' but with the mechanics of it abstracted and hidden away. That is pretty much what you say you don't want to do.
I can't think of a clever way without having to do, at least,
DateExtensions.HolidayRepository = _container.Resolve<IHolidayRepository>();
|
|
|
|
|
Thanks BobJanova.
Isn't passing around a reference to the container an anti-pattern?
Anyway, I'm specifically trying to avoid directly calling resolve against any repositories, as I want all the plumbing to be automatic through ctor/field injection. Which you can't (apparently) do with statics, and CW.
Hmmm....
Rgds
|
|
|
|
|
Yes, I suspect it is, which is why I suggest doing it the other way.
I'm pretty sure you can't do property/field injection on a static because there is no point at which an instance of it is created by the container and passed to something else. Obviously you can't do constructor parameter injection.
Personally I don't like dependency injection much and consider the whole field something of an anti-pattern so perhaps I'm not the ideal person to be trying to help you
|
|
|
|
|
I want to develope a RTF editor like word.I means it must hava WYSWG and Have a page view (like a Print View).
|
|
|
|
|
|
hi my friends
i need to help for( programming code c++ or c#) mapping for noc(network on chip)
by algorithm pso(particle swarm optimization)
i cant speak or write english welly
help me pleas
|
|
|
|
|
I am using the sniffer mentioned in the article
A Network Sniffer in C#
It only captures inward traffic... Please please guide me how can i modify it to capture outward traffic....
Thanks
|
|
|
|
|
I suggest you wait for the author of the article to respond, he is most likely to be able to help.
In any case, doing this will be too big a question to be answered usefully in a message here.
|
|
|
|
|
Hi, I would like to initialize a generic using a string.
Type t = Type.GetType("double")
List<t> MyList = new List<t>();
It doesn't work.
Is it possible to do it?
thanks for your time
|
|
|
|
|
List<t> MyList = new List<t>();
Nope. The whole generics stuff is dealt with by the compiler, so the types (such as your t) have to be known at compile time.
|
|
|
|
|
no way of a work around?
I have a string input and need to initialize a generic. It is a big constrain apparently
|
|
|
|
|
You can't get List<T> to compile when T is unknown.
There are things one can do with reflection, they are complex and/or tedious. Maybe you should explain what you really want to achieve, at some higher level.
|
|
|
|
|
I need to initialize a generic class like:
class myClass < T, G > where T: car
where G: moto
{
...
}
car and moto are base classes, with lots of derived classes each. Since I use an excel file to initialize the class myClass I will enter in excel T and G as string from a List (after data validation), and I would like to initialize the correct class. Not easy to use a switch statement since I have more than 20 derived class for T and 15 derived class for G
|
|
|
|
|
That seems like a completely different question. How about this:
1. if you need a specialized car, use a CarFactory method that returns a Car of the correct derived type; you can do this either with a switch, or with reflection; search for the Factory pattern, and Activator.CreateInstance()
2. if you need a collection of them, collect the base type, hence List<Car> ; it will gladly hold any object that is a Car or a Car derivative.
PS: wherever it said base class, you could substitute interface.
|
|
|
|
|
Thank for reply.
2 main problems.
1) myClass uses specialization of car an moto at the same time. The problem is the conjuncted combination of specializations. 20 car and 15 moto... myClass can be inizialized in a lot of combinations.
2) the input is from excel. I should use something that convert string to class. Factory for specialization of car only is ok. But I need combination of specialization..
Thnaks
|
|
|
|
|
- IMoto interface
- ICar interface exposing a Moto property of type IMoto
- Moto factory returning an implementation of IMoto, the concrete implementation determined by switch statement
- car factory returning an implementation of ICar, the concrete implementation determined by switch statement and setting the Moto property using the Moto factory
e.g.
public class CarFactory
{
public ICar CreateCar(string carID, string motoID)
{
ICar result;
switch(carID)
{
case "jaguar":
result = new Jaguar();
break;
case "porche":
result = new porche();
break;
}
result.Moto = MotoFactory.Create(MotoID);
return result;
}
}
public class MotoFactory
{
public IMoto Create(string motoID)
{
IMoto result;
switch(motoID)
{
case "uber":
result = new UberMoto();
break;
case "pants":
result = new PantsMoto();
break;
}
return result;
}
}
Pedis ex oris
Quidquid latine dictum sit, altum sonatur
|
|
|
|
|
Oky, qquestion:
how to use the syntax with a class
myClass<t,g>: where t: car where g: moto
Can not do:
myClass< CarFactory.Create("jaguar"), MotoFactory.Create("uber")> o = new ...
myClas
|
|
|
|
|
aside from
myClass<ICar,IMoto> you can't without some nasty reflection (detailed in other replies)
My question would be why you need to use generics for this.
With the interface driven approach the ICar has an Imoto in a strategy pattern. your myClass could be simply written to work with ICar instances regardless of the concrete types.
this would require the ICar interface to be well designed but since your myClass clas is designed to work with any combination of car and moto then it should be easy to determine the common functionality to place on the ICar and IMoto interfaces
why pass concrete types when a common interface will suffice?
Pedis ex oris
Quidquid latine dictum sit, altum sonatur
|
|
|
|