|
If you only have the string of the class and the type is not known at compile time, then you should use reflection and the Activator.CreateInstance[^] method.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
You can use reflection, as Greeeg suggested. But, as I use to say, if you need reflection then you are doing something pretty advanced, or something wrong.
Unless the classes can be anything, I would just use a switch to select the class to be created.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Using VS2008, in a Windows.Form application...
I'm trying to fade an image onto the screen. I first load the image I want use, and then create another image that is nothing more than the same color as the form background. To fade on/off the image, I fade on the actual image, and then fade on the plain background image. Given the following code...
float m_alphaStep = 0.100000000f;
int m_X = 0;
int m_Y = 0;
public void Fade(Bitmap bitmap)
{
float width = (float)m_bitmap.Width;
float height = (float)m_bitmap.Height;
Rectangle destRect = new Rectangle(0, 0, m_bitmap.Width, m_bitmap.Height);
float alpha = 0.000000000f;
try
{
for (alpha = 0.000000000f; alpha < 1.000000000f; alpha += m_alphaStep)
{
m_colorMatrix[3, 3] = alpha;
m_attributes.SetColorMatrix(m_colorMatrix);
m_graphics.DrawImage(bitmap,
destRect,
(float)m_X,
(float)m_Y,
width,
height,
GraphicsUnit.Pixel,
m_attributes);
}
m_colorMatrix[3, 3] = 1.0f;
m_attributes.SetColorMatrix(m_colorMatrix);
m_graphics.DrawImage(bitmap,
destRect,
(float)m_X,
(float)m_Y,
width,
height,
GraphicsUnit.Pixel,
m_attributes);
}
catch (Exception ex)
{
if (ex != null) { }
}
}
public void PaintItem()
{
Fade(m_bitmap);
Thread.Sleep(1000);
Fade(m_offBitmap);
}
Problems:
0) The indicated call to m_graphics.DrawImage() won't compile, despite my using what intellisense claims is a valid overload.
1) The for loop determines the new alpha channel value. The nature of float values causes the alpha value to eventually become some bizarre value (usually by the 7th iteration), which forces me to only do the loop while it's less than 1.0, and then perform the final DrawImage() call after the loop has exited.
2) Performance quite frankly sucks. I can see the image redraw itself from top to bottom. Is there a better (more efficient) way to fade an image on/off the screen?
3) Immediately upon starting the application, I begin displaying the image. If I turn on double-buffering for the form, the form doesn't get displayed until the Fade() routine completes. If I don't turn on double buffering, it works as expected.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: The indicated call to m_graphics.DrawImage() won't compile, despite my using what intellisense claims is a valid overload.
I think that you are reading the intellisense wrong. There is no such overload.
John Simmons / outlaw programmer wrote: The for loop determines the new alpha channel value. The nature of float values causes the alpha value to eventually become some bizarre value
That's not surprising at all. Floating point values are not intended to be exact. You should use an integer value for the loop, and calculate the alpha value from that.
John Simmons / outlaw programmer wrote: Performance quite frankly sucks.
Try to use an overload where you specify the coordinates as integers instead of floats, so that you don't risk getting any scaling when you draw the image.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: There is no such overload.
Well, I coulda sworn I saw that overload. Looking again, it's not there.
Guffa wrote: That's not surprising at all. Floating point values are not intended to be exact. You should use an integer value for the loop, and calculate the alpha value from that.
Well Sparky, I tried that and I still get the wierded out values (and I kinda figured I'd get them - I'm not new at this floating point stuff).
Guffa wrote: ry to use an overload where you specify the coordinates as integers instead of floats, so that you don't risk getting any scaling when you draw the image.
I changed to the appropriate DrawImage( ) function, but no joy. I even tried Invalidate(with image rectangle) , but that doesn't seem to help at all.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: I tried that and I still get the wierded out values (and I kinda figured I'd get them - I'm not new at this floating point stuff).
What kind of "wierded out" values do you get?
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
It's really not important since it was bound to occur anyway. But since you asked, when 0.1 is added to 0.6, it comes out as 0.788888888, and then when 0.1 is added to that, it comes out to 8.00000001. Typical floating point stuff.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: 0) The indicated call to m_graphics.DrawImage() won't compile, despite my using what intellisense claims is a valid overload.
It's Point[] , not Point for the second parameter.
So new Point[] { new Point(m_X, m_Y) } should do the trick.
John Simmons / outlaw programmer wrote: 1) The for loop determines the new alpha channel value. The nature of float values causes the alpha value to eventually become some bizarre value (usually by the 7th iteration), which forces me to only do the loop while it's less than 1.0, and then perform the final DrawImage() call after the loop has exited.
As Guffa said, better use a range like 0-255 (or whatever you wish) and calculate the alpha by n/255.
John Simmons / outlaw programmer wrote: 2) Performance quite frankly sucks. I can see the image redraw itself from top to bottom. Is there a better (more efficient) way to fade an image on/off the screen?
Depending on how many iterations you do per second, that's many pixels that need to be modified.
Can't you put the image onto a PictureBox? You could then use the UpdateLayeredWindow interop to change the alpha of the control, which should be faster than your method.
Or just draw the image once on the form and set the Form.Opacity property in a loop.
Both methods will work only on Windows 2000 and upwards, though.
Of course the fastest method would be to use DirectX pixel shaders, but that'd be definitely overkill for your application.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Greeeg wrote: Can't you put the image onto a PictureBox? You could then use the UpdateLayeredWindow interop to change the alpha of the control, which should be faster than your method.
I guess I could, but you ask that like I knew it was available.
Greeeg wrote: Or just draw the image once on the form and set the Form.Opacity property in a loop.
Can't use Form.Opacity because then the entire window would come and go.
Greeeg wrote: Both methods will work only on Windows 2000 and upwards, though.
That's okay, I'm not concerned with people that are still using Win9x.
Greeeg wrote: Of course the fastest method would be to use DirectX pixel shaders, but that'd be definitely overkill for your application.
I would tend to agree.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Hi, I get a weird error:
The assembly with display name 'mscorlib.XmlSerializers' failed to load in the 'LoadFrom'
This is what I did
public class ErrorObject
{
[XmlElement("DateTimeError")]
public DateTime DateTimeError { get; set; }
[XmlElement("Message")]
public string Message { get; set; }
[XmlElement("InnerMessage")]
public string InnerMessage { get; set; }
[XmlElement("ErrorPriority")]
public ErrorLevel ErrorPriority { get; set; }
[XmlElement("Dump")]
public object Dump { get; set; }
public ErrorObject(DateTime DateTimeError, string Message, ErrorLevel ErrorPriority)
{
this.DateTimeError = DateTimeError;
this.Message = Message;
this.ErrorPriority = ErrorPriority;
}
}
public enum ErrorLevel { Low, Normal, High, Critical };
and
public void AddMessage(ErrorObject errorObject)
{
List<ErrorObject> errorObjects = new List<ErrorObject>();
errorObjects.Add(errorObject);
XmlSerializer serializer = new XmlSerializer(errorObjects.GetType());
TextWriter textWriter = new StreamWriter(LogFile);
serializer.Serialize(textWriter, errorObjects);
textWriter.Close();
}
No idea what's wrong.
VS2008 is being used.
thx in advance
|
|
|
|
|
What's the complete error message?
Is the ErrorObject class in a different module than the
AddMessage() code?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I found it. It's been the debugger.
I use 64bit and while it searches for the right version it throws an error.
Weired..
Thanks anyway
|
|
|
|
|
Hi.
I have a problem getting this thing to work, been reading everything i got my hands on but still can´t get it to work...
I have a UI with a textbox that gets updated via a method. This method updates the textbox thread safley by checking invokeRequired and so on.
I make an instace of a class in the main thread that has this backgroundworker in it. When i start the backgroundworker in the class im not able to update my textbox in the UI? Can i only do this from statuschanged event from the thread?
Im more used to embedded programming...
Hope i have managed to explain my problem good enough, this one is really getting on my nerves
Regards
/Johan
|
|
|
|
|
Hey,
I'll write a small example:
private delegate void delegateUpdateTextInForm(string txt);
public void updateTextInForm(string txt)
{
if(form.InvokeRequired)
{
delegateUpdateTextInForm dFunc = new delegateUpdateTextInForm(updateTextInForm);
form.Invoke(dFunc , new object[] { txt });
}
else
{
txtBox.Text = txt;
}
}
hope it helps!
|
|
|
|
|
I prefer to have a field to hold the delegate rather than instantiating one for each call.
And I don't think you need to instantiate an object array, though maybe it was necessary in .net 1
|
|
|
|
|
hi
i add a new reportViewer to my form and add a table to it in Report Designer.
then write this code to fill my report by code :
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=northwind;Integrated Security=True");
SqlCommand cmdSelect = new SqlCommand("select * from customers", con);
SqlDataReader dr;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
con.Open();
dr = cmdSelect.ExecuteReader();
dt.Load(dr);
con.Close();
Microsoft.Reporting.WinForms.ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource("Customers",dt);
this.reportViewer2.LocalReport.DataSources.Add(rds);
this.reportViewer2.LocalReport.ReportEmbeddedResource = "ReportingServices.Report1.rdlc";
this.reportViewer2.RefreshReport();
but the following error message has shown me :
The table ‘table1’ is in the report body but the report has no data set. Data regions are not allowed in reports without datasets.
i don't know how to access table1 properties by code and set DataSetName.
thanks
|
|
|
|
|
Hi,
Sounds like the table name referenced in report designer is different than what is in the dataset. Check in the report designer what is the name of the table holding customer data (most likely it's table1) and either change it to Customers or change the name in the call to ReportDataSource constructor.
Mika
|
|
|
|
|
Thanks Mika
i changed name of table1 to Customers, but still the following message has shown me :
The table ‘Customers’ is in the report body but the report has no data set. Data regions are not allowed in reports without datasets.
|
|
|
|
|
Are you sure that you have set the data source correctly in report designer (from the menu Report / Data Sources...)?
Mika
|
|
|
|
|
As you can see in my first post, i don't set any datasource in report designer, i want set all properties by code :
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=northwind;Integrated Security=True");
SqlCommand cmdSelect = new SqlCommand("select * from customers", con);
SqlDataReader dr;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
con.Open();
dr = cmdSelect.ExecuteReader();
dt.Load(dr);
con.Close();
Microsoft.Reporting.WinForms.ReportDataSource rds = new Microsoft.Reporting.WinForms.ReportDataSource("Customers",dt);
this.reportViewer2.LocalReport.DataSources.Add(rds); this.reportViewer2.LocalReport.ReportEmbeddedResource= "ReportingServices.Report1.rdlc";
this.reportViewer2.RefreshReport();
|
|
|
|
|
Ok, you must define in the report definition (using designer) where the data is coming from. One way is to define a dataset and add that to report definition. After that you can add relevant fields to the report. At run time you can change the actual data by defining a new datasource, but that datasource must still have the fields (and tables) used in report definition.
Basically this is like data binding in UI. Try to define the datasource in designer, add a few fields in the report and then open the report in XML editor instead of designer. Because the report is only a XML definition, you can take a look at this and get a clear view what is actually happening when you design a report.
This article could also be useful to you: Creating Client Report Definition (.rdlc) Files[^]
Hope this helps,
Mika
|
|
|
|
|
Thanks Mika again
i know this, but my main problem is that how to access DataField's properties via code ?
|
|
|
|
|
I take it you're referring datafields in the report. Sorry to say but as far as I know there's no easy programmatic way (such as in UI or Crystal Reports).
The only way I know is to open the rdlc in xml format using for example XmlDocument class and read relevant elements from it and possibly modify them. Basically you can create the whole report on-the-fly by actually creating the xml formatted rdlc. For small reports it's quite easy but if the report definition is large, it can be quite painfull.
Mika
|
|
|
|
|
Hello,
This is my first post here I'm currently a high-school student learning C#.
I've made a small space-invaders/"blitz" video game.
The game is very simple, there's a group of aliens in the top of the screen, moving, and the player controls a space-ship that can shoot at the aliens.
After reading a few articles about threading I've decided that the easiest way for me to implement the moving of the aliens with the shootings and the space ship is to thread every operation, so every operation will have it's own timer (Actually a while loop with a Thread.Sleep())
I did that, but I'm getting an "InvalidOperationException - Object currently in use" from the methods that are trying to draw into the Graphics of the form, sometimes after firing a shot.
This is after using an AutoResetEvent, I've made sure that drawing to the graphics of the form occures in turns. But I'm still getting the error.
So I was wondering if anyone has a suggestion for me, even if it's without threading. I'm pretty sure I've used the ResetEvent on all the drawings. What causes the "object in use" other than drawing to it?
Thanks and sorry for the long post - I tend to write a lot. I hope I was clear - English isn't my mother language.
Uri.
|
|
|
|
|