|
Why constructors don't return values ??
|
|
|
|
|
Because constructors are called when a new class is created, and therefore what is returned to the caller is a new instance of the class.
"A people that values its privileges above its principles soon loses both." -- Dwight D. Eisenhower
|
|
|
|
|
As an extra to the previous answer, if you want to inicate that something in the constructor did not work as expected, throw an exception, that or provide an extra initialisation function for use after the object was constructed.
"Je pense, donc je mange." - Rene Descartes 1689 - Just before his mother put his tea on the table.
Shameless Plug - Distributed Database Transactions in .NET using COM+
|
|
|
|
|
i draw some rectangles and lines into the the panel object. but how can i save the content in the panel object to a bitmap file ?
thanks....:
|
|
|
|
|
If you want to keep what you've drawn to your panel, then I guess you'll have to take a different approach altogether.
Am I right that you're calling myPanel.CreateGraphics() and then use this Graphics object for painting? In this case, the actions are not recorded anywhere and your panel will be blank after the next paint event it receives (try hiding your panel behind another window and then restoring it...).
I think you should use a Bitmap from the start and paint onto this Bitmap using a Graphics object retrieved by Graphics.FromImage() .
In the paint event handler of your panel you paint the bitmap.
That way you can also call myBitmap.Save(...) to save the contents of the bitmap.
Regards,
mav
|
|
|
|
|
What 'mav' said will work, but there's several problems with his approach. If you draw on a Graphics object that you obtained from Control.CreateGraphics , your elements will be drawn. But if the form needs repainting (perhaps it was covered up by another form), they won't be redrawn. You need to perform your drawing in an override of OnPaint (which is passed a PaintEventArgs that has a Graphics property that you should use for painting).
In order to be able to save that same code to a Bitmap class and then to a file, you'll want to modularize your code like so:
private void PaintRectangles(Graphics g)
{
g.DrawRectangle(...);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
PaintRectangles(e.Graphics);
}
private void saveBtn_Click(object sender, EventArgs e)
{
using (Bitmap bmp = new Bitmap(panel1.Size.Width,
panel1.Size.Height))
{
using (Graphics g = Graphics.FromImage(bmp))
{
PaintRectangles(g);
}
bmp.Save("filename.png", ImageFormat.Png);
}
} Now you've encapsulated your rectangle drawing code into a single method that can draw onto any Graphics object, including the form or panel itself (this is only an example, mind you) or a Bitmap . The using statements above make sure that both the Bitmap and Graphics objects are disposed (very important, otherwise memory may not be freed when necessary and unmanaged objects will linger, causing memory leaks) even if an exception is thrown.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I'm improving the GUI of my demo program. There are some controls in the UI, like textbox and numericUpDown etc. I just want to highlight the text (fullly select the text) when I tabbing them.
now, I just using the Control's Enter event handler for each control, and then do selection inside its function.
I felt quite inconvenient, especially, when using numericUpDown, I need to do selection by using this command :
numericUpDown1.Select(0, numericUpDown1.Value.ToString().Length);
so, Is there any easy way to do this?
thanks
|
|
|
|
|
Some of the controls such as TextBox have .SelectAll() method but some like numericUpDown don't. But if you want that behaivor for all of your controls you could go with mass selection of them all and then handling their's .Enter event.
<br />
Control[] GetAllControls() <br />
{<br />
ArrayList list = new ArrayList();<br />
GetAllControls(Controls, list);<br />
return (Control[])list.ToArray(typeof(Control));<br />
}<br />
void GetAllControls(Control.ControlCollection controls, ArrayList list)<br />
{<br />
foreach(Control control in controls) {<br />
if(control.HasChildren) GetAllControls(control.Controls, list);<br />
list.Add(control);<br />
}<br />
}<br />
<br />
void Form_Load(object sender, System.EventArgs e)<br />
{<br />
foreach (Control c in GetAllControls())<br />
c.Enter += new EventHandler(c_Enter);<br />
}<br />
<br />
void c_Enter(object sender, EventArgs e)<br />
{<br />
if (c is TextBox)<br />
((TextBox)c).Select(0, c.Text.Length);<br />
}<br />
To make it easier you could detect Base classes (for TextBox it is BaseTextBox)... if someone has better solution please correct me...
|
|
|
|
|
hi,
im developing an application that needs to POST (multipart) unicode texts to the server.
i tried to send unicode directly to the server and it throws a "ProtocolViolation" exception.
now, i have to convert every char to integer and then to string.
any easier way to do it?
thanks
|
|
|
|
|
You might want to take a look at the System.Text.Encoding class, which can encode and decode strings and byte[] arrays to/from different encodings (even particular code pages, which don't apply to Unicode). Make sure you also set the HttpWebRequest.ContentEncoding to the proper type, or for multipart-MIME the particular Content-Type (and charset) for a MIME part. In either case, the Encoding class will help.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
What is the way to create fast links from highlighted words in a richTextBox to certain parts in text of this or other's richTextBoxes?
I want to have an available description of some highlighted items in richTextBoxes when the user clicks on them.
|
|
|
|
|
There's a LinkClicked event available which you'll have to handle.
By default, standard links (beginning with http:// or ftp:// and a few others) are displayed as links and fire the event when clicked.
If you want other parts (esp. not starting with <protocol>://) then you'll have to use interop to set the correct character attribute (CFE_LINK) to the text range you want to act as a link.
You might use this article on MSDN as a basis for extending your RichTextBox to add support for something like SetSelectionLink() .
Regards,
mav
|
|
|
|
|
....................................<br />
richTextBox1.LinkClicked += new LinkClickedEventHandler(Link_Clicked);<br />
................................................<br />
protected void Link_Clicked(object sender, LinkClickedEventArgs e)<br />
{<br />
.................................................<br />
}
Could someone tells me how: when click on an item in the richTexBox1 to display a specific area of the richTextBox1 ?
Thank you mav.northwind for your response.
Could I have a more specific answer?
|
|
|
|
|
Use Select() to set the Cursor to the location you want to display and then call ScrollToCaret().
mav
|
|
|
|
|
I could use another richTextBox2 which supposed to be a large text since it contains all the text and should be invisible all the time and the only I see will be the richTextBox1.
Also to put a Keyword at the start point of the certain text area in the richTextBox2 and another at the end point of that area. Then:
int start = richTextBox2.Text.IndexOf(myStartKey) ;<br />
int end = richTextBox2.Text.IndexOf(myEndKey) ;<br />
richTextBox2.SelectionStart = start ;<br />
richTextBox2.SelectionLength = end-start+1 ;<br />
richTextBox1.Text = richTextBox1.SelectedText ;
But if I do not use www or ftp could I have an example of what I can use to make items have an appearance and behavior like them ?
|
|
|
|
|
Please stop shouting.
As I stated before, to make part of your RichText to have link appearance and behaviour (blue foreground, underlined, fire LinkClicked event), this part of text has to have the right flags set (CFE_LINK) in the underlying RichEdit control.
These flags (and several others) cannot be set from .NET alone, that's why I've added a section to the MSDN article on how to make only part of your text Bold (or italic or Size 12 or a link) without affecting other properties.
Please read the article, it's all explained there.
The only thing you have to do basically is to duplicate the SetSelectionBold() function from RichTextBoxEx , for example, and change CFE_BOLD to CFE_LINK .
Then add your keyword to the RichTextBoxEx, select it and call your newly defined SetSelectionLink() function.
Voila - only the selection is underlined and will fire LinkClicked without http:// or ftp://.
mav
|
|
|
|
|
|
I'm trying to automate the kickoff of my company's test automation suite. To do this, I've written a Windows service (using C#/.NET) that checks for the presence of a signal file and then kicks off our test automation (using Process.Start()). The problem is that the test automation starts invisibly - the process is there, but no GUI, and since this is GUI testing, we need the GUI to be visible. This seems like a pretty straightforward job - start a GUI app in a visible and interactive state using a service - but I can't figure it out so far. Any suggestions are greatly appreciated.
Thanks.
|
|
|
|
|
If you look at the service in the control panel you should find and option that allows the service to interact with the desktop.
It should be under the logon tab.
This might be what you need.
Chris
|
|
|
|
|
An alternative to what Chris said is to run the service as a particular user with local login rights and an associated desktop session (if they don't, a new should be initialized when the service logs in as that user), like your user account. In this case, however, the service will only run in the specified user's desktop session (which could be in the background of another current desktop session for an interactive user).
In most cases, what Chris said is best but I just wanted to present this alternative.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks very much to both you and Chris. Chris' solution would work except that I need to have the service access a network share folder. The Local System account can't access the network resource. I tried using my own account to run the service, but my service started the test application in the background - I could see the process, but no desktop interaction. What you say makes it sound like the test application should be interacting with my desktop session, since the service is running under my account and I'm currently logged in. Can you think of any reason why it isn't?
|
|
|
|
|
You could use impersonation. See the documentation for the WindowsIdentity.Impersonate method in the .NET Framework SDK for an example.
This won't prompt for credentials, however. That's your code's job but simply requires a dialog with a couple TextBox es and Button s (or however you want it to look).
Another option - though a hack - is to set the password for the local SYSTEM account on both machines to the same password. This has worked for several other people in workgroup setups before.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
hi there,
i would like to receive information about a cd/dvd in my cd-rom drive. i already use the freedb and volumeinfo article's code to receive the audio track names and other details (thx a lot for the code!).
what i need is the following:
- i want to get the disk size in bytes. this works with a system.management.managementobject, but _not_ for audio cd's, it returns 0 (it works for mixed cd's and dvd's e.g.).
- what i need even more is the date when the cd/dvd was pressed/burned. this must work somehow! nero shows it! also windows shows a date for each track...
could anyone help me with that please?
greetings, have a nice week,
civc
|
|
|
|
|
|
You have to be alot more specific about what your asking and what your doing. Instance of what?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|