|
I may be being very thick here, so stay with me...
...I derive a class (MyListControl) from ListControl, add my databinding etc. No problem.
How do I go from here to get a CheckedListBox and a ListBox which derives from MyListControl and not ListControl.
When you paste the blindingly simple and obvious answer I will most probably take Christian Grauss' suggestion of buying a large book on VB and smashing myself in the face with it.
Regards
Angel
*********************************************
|
|
|
|
|
You can't change the ancestors of existing classes (Without rewriting them), but with a little extra work, you could encapsulate them into a host control.
public class GenericListBox<T> : Control
where T : ListControl
{
public GenericListBox(data...)
{
T innerControl;
if (typeof(T) == typeof(MyListBox))
innerControl = new MyListBox(data);
else if (typeof(T) == typeof(MyCheckedListBox))
innerControl = new MyCheckedListBox(data);
else
throw new ArgumentException("Some descriptive error message");
innerControl.Dock = DockStyles.Fill;
this.Controls.Add(innerControl);
}
}
...
GenericListBox<MyListBox> ctl = new GenericListBox<MyListBox>(someData);
Of course, you'd have to provide wrapper methods for many functions, or just expose the innerControl object as a ListControl and wrap any methods from below that on the inheritance chain.
Honestly though, I don't think this is the best way to do it. I'd suggest making a factory for it, and just passing an enum with the type of control to create:
public static ListControl CreateListControl(MyControlTypes ctlType, DataObject data)
{
if (ctlType == MyControlTypes.NormalListBox)
return new MyListBox(data);
else
return new MyCheckedListBox(data);
}
(MyControlTypes would be an enum, of course)
Of course, I might be misunderstanding you completely, but maybe this will spark an idea.
|
|
|
|
|
Your first suggestion is nearer to what I want. I was hoping not to use encapsulation, but this is a good start for the generic knowledge I'm lacking. Thanks for taking the time.
Regards
Angel
*********************************************
|
|
|
|
|
Hi,
Hello friends, i need your help..my problem is that i want to export my sql server database to MS access file in a specified location say in a folder..i am using .net 2.0 c# as the language...can anybody help me...
Regards
Alex.
|
|
|
|
|
It's hard to say how to help, when you give no indication of what you have done so far, and where you are stuck.
So... What have you done so far? Where are you stuck?
|
|
|
|
|
Hi,
I have a problem with the conversion of a string of type "07:30" into a time. I want to take only the time, without the date. I tried
DateTime openTime = DateTime.Now.ToShortTimeString(open);
,where open = "07:30".
Please help me with this problem.
|
|
|
|
|
Hi,
a DateTime always contains date and time.
TimeOfDay gives time since midnight.
Hence:
DateTime dt=DateTime.Parse(myInputString);
TimeSpan time=dt.TimeOfDay;
|
|
|
|
|
But if my string is like this "07:30", what can I do? Because I tried that method and my result is 00:00:00.

|
|
|
|
|
When I run the code snippet
DateTime dt=DateTime.Parse("07:30");
TimeSpan time=dt.TimeOfDay;
log(time.ToString());
I get "07:30:00" as expected.
BTW if you're not satisfied by Parse, there also is DateTime.ParseExact().
|
|
|
|
|
Cool. I've always padded it out with current day/month/year. Learn something new everyday here....
Regards
Angel
*********************************************
|
|
|
|
|
try
<br />
System.DateTime nowTime = System.DateTime.Now;<br />
<br />
System.DateTime openTime = System.DateTime.Parse(nowTime .ToShortDateString() + " 07:30:00");<br />
<br />
TimeSpan open = nowTime - openTime;<br />
<br />
<br />
if (nowTime > openTime)
Console.WriteLine("We've been open for {0:0} minutes", open.TotalMinutes);<br />
else<br />
Console.WriteLine("We will open in {0:0} minutes", -open.TotalMinutes);<br />
<br />
There must be better methods though...?
Regards
Angel
*********************************************
|
|
|
|
|
The problem is that I will take the string from a Xml file and I cant use it like that. I really need help.
|
|
|
|
|
what exactly are you trying to do? can you post more code?
Regards
Angel
*********************************************
|
|
|
|
|
I read a Xml file and from it I will have a string, for example the string name is open="07:30".
I tried to read it like this
DateTime oTime = DateTime.Parse(open);
TimeSpan openTime = oTime.TimeOfDay;
where openTime is of type TimeSpan.
But the result is 00:00:00 and it doesnt work.
|
|
|
|
|
You still haven't posted any code....
I hope this isn't homework I am helping with......
So you have xml file eg:-
<br />
<myxml><br />
<times open="07:30" close="09:30" /><br />
</myxml><br />
And you want to know how long until it opens. Timespan is the difference between two times. If you want to know how long it is until the shop opens, then you need two things. The current time, and the time it opens.
I am assuming you now have "07:30" stored in a string. I am also assuming that you want to know this information about 'today', and not any day in the future.
C# DateTime class, as has already been mentioned, must have both a date and time portion.
To get the current time :-
System.DateTime currentTime = System.DateTime.Now;
As I am writing this, this will give you:
02/05/2007 15:26:34
OK. That is the time now. You now want to know the time (today) when your shop/office etc opens.
So, take the current datetime (currentTime) and replace the time portion with your time.
System.DateTime openTime = System.DateTime.Parse(currentTime.ToShortDateString() + open + ":00" );
The extra ":00" is because a valid time (I think) needs hours:minutes:seconds and yours ("07:00") doesn't have this.
So you now have currentTime and openTime.
If you get the timespan of these, and use the TotalMinutes member, you get the number of minutes until the shop opens, or the number of minutes it has been open if this number is negative.
TimeSpan open = nowTime - openTime;<br />
int MinutesUntilWeOpen = open.TotalMinutes;
Is this homework or a real requirement? Post what you have tried up until now - it makes understanding your requirement a lot easier.
Regards
Angel
*********************************************
|
|
|
|
|
Thanks for trying to help me, but I dont want to make that. I want only to show the hour I read it from the Xml file. That's the problem, because I tried it and it shows something like 00:00, and not 07:30, like it should.
|
|
|
|
|
OK - lets try again. Show me the whole function that you have at the moment. Show me your input, and what you expect as output.
Regards
Angel
*********************************************
|
|
|
|
|
this is the function which reads the Xml File and is part of a class Cities.
public class Cities : List<city>
{
public void OpenCityList(string filename)
{
DataSet cityDS = new DataSet();
try
{
this.Clear();
cityDS.ReadXml(filename);
DataRowCollection cities = cityDS.Tables[0].Rows;
foreach (DataRow city in cities)
{
this.Add(new City(Convert.ToInt32(city["X"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Y"], CultureInfo.CurrentCulture), Convert.ToInt32(city["Demand"], CultureInfo.CurrentCulture), Convert.ToString(city["Open"], CultureInfo.CurrentCulture)));
}
}
finally
{
cityDS.Dispose();
}
}
private TimeSpan startHour;
public TimeSpan StartHour
{
set
{
startHour = value;
}
get
{
return startHour;
}
}
public void ShowCityHour()
{
foreach (City cityhour in this)
{
startHour = cityhour.OpenTime;
}
}
}
And in another class, named City I have :
public class City
{
private int demand;
private TimeSpan openTime;
public City(int x, int y, int citydemand, string open)// I am interested about the string open
{
Location = new Point(x, y);
demand = citydemand;
DateTime oTime = DateTime.Parse(open);
TimeSpan openTime = oTime.TimeOfDay;
}
public TimeSpan OpenTime
{
get
{
return openTime;
}
set
{
openTime = value;
}
}
}
It is something like that. I take the data from the Xml file and I store it in the string open, and I want to convert it to time, because after that I will have to sort the whole list in order of the hours.
|
|
|
|
|
Anka_Ame wrote: foreach (City cityhour in this)
{
startHour = cityhour.OpenTime;
}
What does the variable startHour (and the property StartHour) mean in a list of cities ??
and why is a foreach loop used to set it (to whatever happens to be the last city.OpenTime) ?? in a method called Show... where nothing gets shown
|
|
|
|
|
that's not my problem. I just want to read the value from a Xml file and to convert it to time, in a string without date.
|
|
|
|
|
You can't have a time without a date. You keep using TimeSpan. That shows the difference between two times / dates - unless I am mistaken. Anybody?
You have two options - convert all times to 'today' + your time. You can then sort them.
or
Or convert your timestrnig to an integer, throw all the ints into a collection and sort them.
string myString = "07:30"
int myTime = int.Parse( myString.Substring(0,2) ) * 60;
myTime += int.Parse( myString.Substring(3,2) ) ;
This will give you the time in minutes, as aninterger. (from memory, so could be typo's etc);
Regards
Angel
*********************************************
|
|
|
|
|
Use TimeSpan.Parse(string s)
Hope this helps...
|
|
|
|
|
Guys,
Last week I asked here on CP for some aid in converting something like "aabb" to "aAbB". You had to read a textfile, convert it and show it in a textbox.
We participated with about 10 or 15 people and the result was that my application wasn't the fastest (and definitely not the slowest), but I did got a compliment for having the shortest code. Most participants used a lot of delegates and events with different threads etc.
Thanks to all those that helped me .
V.
I found a living worth working for, but haven't found work worth living for.
|
|
|
|
|
V. wrote: converting something like "aabb" to "aAbB". You had to read a textfile, convert it and show it in a textbox.
V. wrote: Most participants used a lot of delegates and events
I don't remember your post last week, but from the description you gave just now, I don't see where delegates and events could come into a solution like that.
|
|
|
|
|
lol, he could convert a file of 8MB in 2,9 seconds. (including reading in and putting into the textbox), he used several different classes and he added different conversion methods for ASCII, UTF etc...)
My conversion, including reading in and putting into textbox was not even 20 lines .
|
|
|
|