|
Hello guys and girls
I'm recently started on C# but as I know abit programming (Coming from Java) it should be easy to do this, but as I have experienced from Microsoft its never easy on customizing or subclassing their components (I've tried in Win32/C Old school, but thats many years ago)
The question is:
I want to draw a dropshadowborder on a panel and/or take it further to draw all components with a dropshadow when they are added to the panel
I started on subclassing Panel to override OnPaint, but as I cannot do the following code (atleast not yet) in C# I would like to have an explanation if you guys/girls are up for it or willing to help me out so I learn abit more this day
// Sample code working in Java
@Override
public void paint(Graphics g) {
if (shadow == null) {
BufferedImage buffer = new BufferedImage(getWidth(),
getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = buffer.createGraphics();
super.paint(g2);
shadow = factory.createShadow(buffer);
g2.dispose();
g.drawImage(shadow, distance_x, distance_y, null);
g.drawImage(buffer, 0, 0, null);
} else if (isVisible()) {
g.drawImage(shadow, distance_x, distance_y, null);
super.paint(g);
}
}
// End of sample snippet
My snippet in C#:
//Snippet from C#
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Image buffer = new Bitmap(Width, Height);
Graphics g2 = Graphics.FromImage(buffer);
g2.FillRectangle(new SolidBrush(Color.Black), new Rectangle(5, 5, Width, Height));
e.Graphics.DrawImage(buffer, 0, 0);
}
I know the code convertion isnt exact the same but it was to point out my problem
I hope you can see what I want to do, and I hope someone is willing to spend some minutes answering me with some theory.
My Best Regards and Wishes
David Bundgaard
|
|
|
|
|
The control has a method that causes it to render itself to a bitmap. It's called DrawToBitmap. Use that to create a copy of your control in a bitmap, then you can manipulate it.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Hi, If I use a using command that uses a oleDbCommand object, do I need to close the using statement before I return a result?
eg
using (OleDbCommand Command = GetCommandObj("DB1"))
{
Command.CommandText = "<sql statement>";
return Command.ExecuteScalar().ToString();
}
will the above code dispose of the OleDbCommand and release the connection?
or do I need to do somethiong like:
string result = string.Empty;
using (OleDbCommand Command = GetCommandObj("DB1"))
{
Command.CommandText = "<sql statement>";
result = Command.ExecuteScalar().ToString();
}
return result;
also, should I include something like
Command.Connection.Dispose(); within the using statement to release the connection?
Thanks,
Chas
|
|
|
|
|
Yeah, I think that's in the documentation.
|
|
|
|
|
Thanks for that, 'Yeah' to what part of my question?
|
|
|
|
|
Neither actually.
Anyway, others have given you the full answer.
I'll just add that I generally don't create and delete Connection and Command objects like that. I create one of each at the beginning of the program, use them repeatedly throughout the run (opening and closing the connection each time), and then only delete them at the end.
Plus I prefer connection.CreateCommand for instantiating Command objects. But that's just me.
|
|
|
|
|
using (OleDbCommand Command = GetCommandObj("DB1"))
{
Command.CommandText = "<sql statement>";
return Command.ExecuteScalar().ToString();
}
Is enough, but it only destroys your command and not you connection.
If you want to destroy also the connection the use something like this:
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
OleDbCommand command = new OleDbCommand(queryString, connection);
return command.ExecuteScalar();
}
There's also the possibility to use a try finally statement something like this:
OleDbConnection connection = null;
OleDbCommand command = null;
try
{
connection = new OleDbConnection(connectionString))
connection.Open();
command = new OleDbCommand(queryString, connection);
return command.ExecuteScalar();
}
finally
{
if(command != null)
command.Dispose();
if(connection != null)
connection.Dispose();
}
Greetings
Covean
|
|
|
|
|
Covean wrote:
There's also the possibility to use a try finally statement something like this:
OleDbConnection connection = null;
OleDbCommand command = null;
try
{
connection = new OleDbConnection(connectionString))
connection.Open();
command = new OleDbCommand(queryString, connection);
return command.ExecuteScalar();
}
finally
{
if(command != null)
command.Dispose();
if(connection != null)
connection.Dispose();
}
Return some value under the try catch should not be a good practices.
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
It's important to note that using statements are syntatic sugar for turning:
using (OleDbCommand Command = GetCommandObj("DB1"))
{
Command.CommandText = "<sql statement>";
return Command.ExecuteScalar().ToString();
} Into:
OleDbCommand Command;
try
{
Command = GetCommandObj("DB1");
Command.CommandText = "<sql statement>";
return Command.ExecuteScalar().ToString();
}
finally
{
Command.Dispose();
}
So, based on how finally blocks work, it will always call the dispose whether it returns or throws an exception. So, yes, the initial example you gave is correct.
|
|
|
|
|
And, yes, Covean is right about closing the connection (I missed that part). The sooner the better.
|
|
|
|
|
Return some value under the try catch should not be a good practices.
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
|
Please Ignore this post
Well.. It depends on how you are using the following code snippets...
public class ReturnExample
{
private String normalExecution()
{
String result = "";
try
{
result = "Entered try block.";
return result;
}
catch(Exception e)
{
result = result + "Entered Catch block.";
return result;
}
finally
{
result = result + "Entered finally block.";
}
}
public static void main(String[] args)
{
ReturnExample example = new ReturnExample();
String result = example.normalExecution();
System.out.println(result);
}
}
Here you will find the reaturing value is updated;
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Well.. It depends on how you are using it. look at the following code snippets...
public class ReturnExample
{
private String normalExecution()
{
String result = "";
try
{
result = "Entered try block.";
return result;
}
catch(Exception e)
{
result = result + "Entered Catch block.";
return result;
}
finally
{
result = result + "Entered finally block.";
}
}
public static void main(String[] args)
{
ReturnExample example = new ReturnExample();
String result = example.normalExecution();
System.out.println(result);
}
}
Here you will find the reaturing value is updated;
Thanks
Md. Marufuzzaman
Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
|
|
|
|
|
Oh, yeah, there is that; but it's easily avoided by not writing that way.
I suppose a better position is simply to avoid return in catch and finally blocks, but allow them in try blocks as long as there are no returns outside the try (the catch must rethrow).
In fact it may be better as an argument for the Single-Return Brigade.
|
|
|
|
|
First you said.
Here you will find the reaturing value is updated;
result = "Entered try block."!
So what's wrong with this?
At next I said try and finally and not try and catch.
And whats the difference between try finally or using?
Both do the same!
Now to your statement "its not a good practice".
Whats wrong with the practice to cleanup everything
you allocated in an function?
Now one question:
Ever seen the limitations of the garbage-collector?
So it can be a good practice to collect the generation
zero (objects with a short lifetime cycle) in a
finally statement, if your objects eats up a large amounts of
memory, or the gc kills your performance!
At last one word to those crying out never catch something till the
exception reaches the gui layer.
This is wrong in general. Its right that you should not catch
everything and send it to nirvana but how about this? (pseudocode)
ConnectionObject con = null;
TransActionObject trans = null;
try
{
con = new ConnectionToSomeDatabase(...);
trans = new TransActionObject(con, ...);
trans.Commit();
trans = null;
}
catch
{
if(trans != null)
trans.Rollback();
throw;
}
finally
{
if(con != null)
con.Close();
}
How to implement something like this?
Whats the point not to use the given possibilities?
If somebody now says, its slow, then you are right
if you use this in a very fast loop, but how about
start a transaction on a database? There is not
comparision!
So just my 2 cent on this.
And yes I will continue make intensive use of this kind of
statements!
Greetings
Covean
|
|
|
|
|
Hi all. Does anyone have any experience deploying a .nunit file to a network share?
I'm trying to share everything involved with running a particular test suite such that anyone on the team can simply access the share, open my nunit file in their NUnit-gui and run to see my status. The test suite that is to be shared is not dependent upon executing any custom code. Every one of the tests is structured similarly to this:
[TestFixture()]
public class CoverageTests
{
[Test()]
public void ReportStatusOfTask1()
{
Assert.IsTrue(false, "Task 1 has not begun yet");
}
[Test()]
public void ReportStatusOfTask2()
{
Assert.IsTrue(true, "Task 2 is ready for review");
}
}
"I need build Skynet. Plz send code"
|
|
|
|
|
Hello Dear Friends,
I've an application which shows the video from the webcam.
I want to create a C# application which can capture the part of the first application that shows the video in it. Can anybody suggest the way to do this. One way to do this is as described below
1) From the first running application that shows the video in it we can get handle of the window and from that window handle we can get the left,top, right,bottom of the area of the application that shows the video. I've HWND of the first running application but how do I get the left,top,height,width of the area which shows the video.
Is there any other way to achieve this or suggestion for this.
Thanks in Advance,
ritz1234
|
|
|
|
|
you need the handle of the control that shows the video or calculate the correct region if you know the correct location of the "videocontrol"..
greetz
|
|
|
|
|
Hi rootjumper,
Thanks for the response.
The problem is that it is the SKYPE which shows the video while we do the video call.And I don't think it use any control for the video so how to get the handle of the region or the location of the video region?
Thanks
ritz1234
|
|
|
|
|
Hi,
I am developing a C# app in which i want to use PIV Middleware but VS didn't accept its dll when i tried to make a reference. I don't have much experience in this type of programming, can some one guide me and refer to some sample codes or articles.
Thanks in Advance
shezi
|
|
|
|
|
Hello,
Could you please explain me how to put icons to the buttons of Windows application? And also I am tryting to put icon to application itself..
All the best
|
|
|
|
|
Message Closed
modified 23-Nov-14 7:17am.
|
|
|
|
|
thank you for the information, one more question what about putting icons instead of buttons?
|
|
|
|
|
I have a ListView control with a column(just for temp. test) and 2 Groups.
Now, the LV getting populated with 10 items(0..9) and associated with first group in ListView. Now when all done, I'm iterating the Items collection. And found it same as appearing in ListView.
http://img40.imageshack.us/i/40225142.png/[^]
private void Form1_Load(object sender, EventArgs e)
{
for (int a = 0; a < 10; a++)
{
ListViewItem lvi = new ListViewItem(a.ToString());
listView1.Items.Add(lvi);
listView1.Groups[0].Items.Add(lvi);
}
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
for (int a = 0; a < listView1.Items.Count; a++)
{
richTextBox1.Text += listView1.Items[a].Text + "\r\n";
}
}
now, When I set its sort to Descending...it appears like this
http://img39.imageshack.us/i/43854370.png/[^]
In the above screenshot, the indexes are accurate but its showing '9' at the end.
So I thought to add/insert items manually without using inbuilt sorting. And I did but problem persists. Its showing in the sequence the items added/inserted. However the indexes are correct. Any idea guys ?
I also tried ListView.RedrawItems() but nothing seems to works.
Edit : Found something...but View is already set to Details
http://www.codeproject.com/Messages/2509769/Bug-in-ListView-Items-Insert-int-ListViewItem.aspx[^]
Finally Edit 2 :
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=115345[^]
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
modified on Tuesday, November 10, 2009 8:00 AM
|
|
|
|