|
Um, call PrintDocument.Print like you already did. If you want to know how to select a different printer, then you need to read the documentation in the .NET Framework SDK, like for the PrintDocument.PrinterSettings property, which has a printer called PrinterName . You can also use the PrinterSettings.InstalledPrinters static property to get a collection of printers that are installed. All of these properties and methods I've mentioned have plenty of sample source code that's not worth duplicating here.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
When i built My Solutions, mostly Assembly version automatically increased, but some case, Assembly Version lessoned.
I did not modify AssemblyInfo.cs file.
in Assemblyinfo.cs file,
version is [assembly: AssemblyVersion("1.0.*")]
Why version lessons?
Is there any way recovering version?
thanks in advance.
SeungPil Lee
|
|
|
|
|
The assembly version is generated according to the documentation for the AssemblyVersionAttribute in the .NET Framework SDK. From the constructor documentation:The format of the version string is: major. minor. build. revision.
When specifying a version, you have to at least specify major. If you specify major and minor, you can specify an asterisk (*) for build. This will cause build to be equal to the number of days since January 1, 2000 local time, and for revision to be equal to the number of seconds since midnight local time, divided by 2.
If you specify major, minor, and build, you can specify an asterisk for revision. This will cause revision to be equal to the number of seconds since midnight local time, divided by 2. So, as you should see, the version is generated using the current time. If your system clock is off or different computers are using different times zones (more common than you think, especially if someone has "Change automatically for daylight saving time" checked and another doesn't), then the version number will decrease.
Let me make a new case for you, though: disable automatic versioning! If you truly understand assembly strong names (which include the version) and manage larger solutions, you'd know why. Version numbers can quickly get out of control when using automatic versions. This is especially troublesome when you have a common build directory in a large group of developers. If assembly versions are changing all the time, you get FileNotFoundException s since the assembly version that the calling assembly was built against doesn't exist. You can - through .config files or publisher policy assemblies - redirect these versions easily enough (at least if there's no breaking changes) but it becomes a nightmare to manage those as well. Trust me - I'm a software architect and build master for a 60+ project solution with lots of dependencies.
Read Redirecting Assembly Versions[^] and Creating a Publisher Policy File[^] for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you for answer.
It's helpful to me.
I see what you said.
When I built assmebly. i changed my computer time to past time.
|
|
|
|
|
Dear All,
The scenario is, I am in the phase of developing a print module in C# that will extract data (text, images, ladder snippets) from the project file stored in the computer's memory.
Earlier, I was trying to create an RTF file and create a project report like document, but that idea was ruled out as the amount of data is very huge.
Thus I am not supposed to create a seperate file for printing that will either be previewed or printed with the customized dialogs, as it will be an overhead to the system also it may affect the speed of the system.
The prime objective is to do printing/previewing as fast as possible.
Now I think, have only one option, that is, to create the document using Graphic Object( in GDI+ ) at run-time, using PrintPageDocument.
Its not really eassy task for me, first of all creating a formatted file like a project report that has title , creaters name and date on first page, then rest of the pages having different sub-headings, data in simple tabular format , bitmap images and ladder snippets, is a big question then inserting ladder diagrams in to it is also unthinkable, as I don't know how to read ladder diagrams using C#.
I will appreciate your every single suggestion. Please guide.
|
|
|
|
|
There are actually plenty of open-source projects available that already do this you should take a look at (which begs the question, if so many free and commercial libraries exist - why are you creating your own...unless it's for educational purposes).
For example, take a look at the Report.NET[^] project on SourceForge.
There's also a good one here on CodeProject: Printing Reports in .NET[^].
As you've already guessed, this is a complicated task. I suggest plenty of research into the System.Drawing namespace members, as well as child namespaces members, as well as research into what other projects are already doing. Many of these projects spawn from people with a lot of experience with report libraries, including some that have written them for other languages / platforms.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I'm having a bit of a problem with Commerce server's Bizdesk.
Ive had to write some extra modules to add into bizdesk, which all work fine appart from one problem, the rest of bizdesk seems to work off cached data which only periodically is written back to the db, where as my modules interact directally with the db. So when you make a change on something and then open up one of my modules, the change has not occurred yet.
I know that restarting the website or the app domain flushes all the cached data and gives me the desired effect because i've done it manually, but i cant be there to do it every time, so I want to write some code on a web page that will restart the website or app domain.
Unfortunatly i have no idea how to do this
please help
|
|
|
|
|
This really belongs in the ASP.NET[^] forum, but since it's a simple question I'll answer anyway (just please use the appropriate forum next time). Read about the HttpRuntime.Close static method. It flushes all items in the cache.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've successfully created & installed a Windows sevice. (It's easy with c#!).
However there are two additions I need to make which I can't figure out:
a) how to provide a command-line input to it. I need to tell the service where to find and store certain files: in particular, when it is installed, the service needs to know where the user has chosen to install the other files of the application.
b) In the 'Services' section of the Computer Management Console, is it possible to set a property 'Allow Service to Interact with Desktop'. I would like to set this programmatically, but cannot find the corresponding property in the the System.ServiceProcess namespace.
Any ideas?
|
|
|
|
|
a) The best place is in the application's .config file. The file is named the same as the application plus ".config" as the extension (ex: MyApplication.exe.config). This goes into the same directory. It's a simple XML file and there's plenty of documentation in the .NET Framework SDK.
For an installer to change this file, though, isn't so easy. Using VS.NET's crappy Windows Installer project, you'd have to extend the Installer class that takes a parameter that is the filepath to the service. It's possible, but not easy.
Though it's not recommended for .NET application, you might consider the registry, which the Windows Installer projects can right to easily. Let your service executable reside in the Application Folder in the Windows Installer project (this corresponds to the INSTALLDIR property) and then add a key under HKEY_LOCAL_MACHINE\Software\YourCompanyName\YourProductName somewhere. Set the registry value as [INSTALLDIR] (notice the brackets, which is important). Then your service can read from that registry key using the Microsoft.Win32.Registry and RegistryKey classes.
b) There is no way through the .NET FCL assemblies to do this, other than writing to the registry yourself. Under HKEY_LOCAL_MACHINE\SYSTEM\Services\YourServiceName (not the display name, but the short name you've given it), write a binary key named "Type" with the value 288. This is a flagged key where 32, IIRC, is "Local system account" and 256 is "Allow service to interact with desktop".
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
That's great, thanks. Using the Registry has got me a long way forward, (The Installer variable is [TARGETDIR] and not [INSTALLDIR] by the way).
But for (b), I get an unauthorised access exception when programmatically writing to the key you suggest (the 'Services' key is in the 'CurrentControlSet' subkey, is that right?). This is odd because I am logged on as admin and can write to the key with RegEdit.
regPath = @"System\CurrentControlSet\Services\SpyFive";
key = Registry.LocalMachine.OpenSubKey(regPath);
if(key != null)
{
int type = (int)key.GetValue("Type");
key.SetValue("Type",type+256);
}
|
|
|
|
|
Of course, I need to 'OpenSubKey()' with write permission.
Anyway, could you confirn that the 'CurrentControlSet' is the correct key, and not 'ControlSet001' or 'ControlSet003'. What are these?
Having got the service working as I want it, I now cannot stop it programatically.
System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController("SpyFive");
try
{
sc.Stop();
}
catch
{
....
there is nothing caught, the service goes into 'stopping' mode and then nothing can be done with it (even from MMC). Any ideas?
|
|
|
|
|
An interesting effect is happening with my listbox control.
I first let .NET handle the control creation and handling code to create the listbox. I then call an external library that adds the appropriate list members to the list, and call an update on the listbox. NOTE THAT THIS WORKS FINE.
The trouble comes when I first call the external library when the list is EMPTY (so that update/refresh is called on the empty listbox control). THEN I add an element... so that one element now exists in the listbox. Refreshing again, now the listbox is STILL empty, when it should be displaying 1 item (stack trace reveals that the data exists in the table). NOW, I add a SECOND item, refresh, and they BOTH display!
I have a feeling the bug exists when I try to refresh an empty list, add an item and refresh again.
Is there a workaround? (Besides checking that a list is empty before refreshing it).
|
|
|
|
|
my program is very simple. When you press spacebar it shows next file in directory in webBroser control that fills my whole form. The problem is when I click on the web form or the menu or another window and then comeback and hit spacebar, it doesn't work. I was thinking it lost focus, but after I set focus back to the form it still didn't pick up the event.
Also, how would I set my Form Width to that of the webpage or picture being displayed in the webBroswer control?
Thanks
|
|
|
|
|
The WebBrowser control is an ActiveX control and dispatches messages in its own thread, i.e. .NET will not respond to notification messages for that ActiveX control. If you set the focus back to the Form by clicking in a TextBox or a Button or something, then it actually has the focus and will process the key down and up messages.
To combat this, set Form.KeyPreview to true . This will handle certain notification messages (which the .NET FCL assemblies translate to events) before the control with the focus. This will still not help when the WebBrowser control has the focus.
As far as your last question goes, this really isn't possible. The width of a page or an image displayed in the WebBrowser control doesn't set the size of the WebBrowser control. Rather, the WebBrowser control sets the size.
Now, script in the page can set the size of the WebBrowser control (depending on security settings). You can declare the IDocHostUIHandler and override the ResizeBorder in order for your Form (the hosting control) to resize itself if script resizes the Window. See the article Using MSHTML Advanced Hosting Interfaces[^] for more details on this.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Hi,
Does anyone know how to lock/unlock files in C#? I'm trying to lock an xml file while its open in the application, and unlock it when closed.
Thank you
Silver Bullet
|
|
|
|
|
See the FileStream class documentation in the .NET Framework. Specifically, the FileStream constructor has several overloads. Some of these require a FileShare enumeration where you can specify how to lock a file for sharing.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am using a Context Menu on a treeview and needed to get a reference to the treenode that was selected, it is all done in the MouseUp method
Here is my code
<br />
private void TreeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)<br />
{<br />
if (e.Button.ToString().ToUpper() == "RIGHT")<br />
{<br />
this.TreeView1.ContextMenu.Show(this.TreeView1,new Point(e.X,e.Y));<br />
this.TreeView1.SelectedNode = (this.TreeView1.GetNodeAt(e.X,e.Y));<br />
this.TreeView1.SelectedNode.BackColor = Color.FromName("HighLight");<br />
this.TreeView1.SelectedNode.ForeColor = Color.White;<br />
MessageBox.Show(this.TreeView1.SelectedNode.Text);<br />
<br />
--------------------------------------------------------------------------<br />
ADDED <br />
A better way to do this....<br />
<br />
this.TreeView1.ContextMenu.Show(this.TreeView1,new Point(e.X,e.Y));<br />
this.TreeView1.GetNodeAt(e.X,e.Y).BackColor = Color.FromName("HighLight");<br />
this.TreeView1.GetNodeAt(e.X,e.Y).ForeColor = Color.White;<br />
MessageBox.Show(this.TreeView1.GetNodeAt(e.X,e.Y).Text);<br />
<br />
} <br />
}<br />
--------------------------------------------------------------------------------<br />
<br />
I just ran into a situation where I had to get e reference to a TreeNode when a user right clicked a node.
You do not need to set the ContextMenu property of the treeview to get the ContextMenu to show up.
this.treeViewContextMenu.Show(this.TreeView1,new Point(e.X,e.Y));
Well I just thought that I would post this. Let me know what you think
William O'Malley
|
|
|
|
|
I'd say that if both work well, then choose the one you think works and looks best for your application. That's just my input.
Happy Programming and God Bless!
Internet::WWW::CodeProject::bneacetp
|
|
|
|
|
Sorry for the long decription...
Description:
I have a windows form which contains a ListView control. I'm automatically resizing the whole Form to fit the desktop. Basically, if the user dragged the form to the bottom of the desktop area, the ListView will shrink in size so that the whole window is still on the screen and none of the form extends past the Desktop bounds. To do this I have anchored the ListView control on all side and I change the Form's height during the LocationChanged Event, which in turn causes the ListView to change it's size due to the anchor. When the ListView shrinks the scrollbars are shown automatically which is what I want to occur. So everything works great up to this point.
Problem:
The problem occurs when I release the left mouse button after dragging the Form to the bottom of the desktop, the ListView Scrollbars disappear. It appears that the ListView is then resized past the bounds of the desktop, but I cannot be sure. When I click to drag the form the scroll bars reappear, but again disappear when I release the button.
Also, if I debug and step throught he code the scrollbars remain after the left mouse button is released....very odd
Any seems a similar problem ?
|
|
|
|
|
I *think* I know what it is, the LocationChanged Event is firing when I'm changing the size of the form from within the LocationChanged event handler, which is producing a recursion effect. Is there any way to stop the LocationChanged event from firing when I change the size of the form ?
|
|
|
|
|
Use state variables. If you programmatically change the location or size, set the state variable to some value, like true . In the LocationChanged event (and actually, if you're handler is in your Form class and is for that Form , override OnLocationChanged - calling base.OnLocationChanged - for better performance), if that state variable is set, don't execute your normal code but instead reset the state variable, like to false (if using a boolean).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
 I've tried this and I don't think its the answer:
protected override void OnLocationChanged(EventArgs e)
{
if(!SettingLocation)
{
if(frmFav!=null && frmFav.Visible)
{
timFavMover.Enabled=true;
MoveIdle=0;
}
IdleTime=0;
SetPlayListHeight();
}
base.OnLocationChanged (e);
}
private void SetPlayListHeight()
{
try
{
if(lvPlayList.Items.Count==0)
return;
SettingLocation=true;
ListViewItem LastItem = lvPlayList.Items[lvPlayList.Items.Count-1];
Rectangle ItemRect = LastItem.GetBounds(ItemBoundsPortion.Entire);
Screen CurScreen = Screen.PrimaryScreen;
Rectangle Desktop = CurScreen.WorkingArea;
if(this.Bottom > Desktop.Bottom)
{
if(this.Height <= PlayerRect.Height)
{
this.Height=PlayerRect.Height;
return;
}
while(this.Bottom > Desktop.Bottom)
{
int BottomDiff = this.Bottom-Desktop.Bottom;
if(BottomDiff==0)
break;
this.Height-= BottomDiff;
if(this.Height <= PlayerRect.Height)
break;
}
}
else if(!IsItemVisible(LastItem) && this.Bottom < Desktop.Bottom)
{
while(!IsItemVisible(LastItem) && this.Bottom < Desktop.Bottom)
{
int BottomDiff=Desktop.Bottom-this.Bottom;
if(BottomDiff==0)
break;
if(ItemRect.Height > BottomDiff)
this.Height+=BottomDiff;
else
this.Height+=ItemRect.Height;
}
}
}
catch(Exception Err)
{
MessageBox.Show("SetPlayListHeight Error: "+Err.Message);
}
finally
{
SettingLocation=false;
}
Correct me if I'm wrong but,
everytime I set the location or size of my form programatically a LocationChanged event is added to the queue, these messages then stack up until I release the mouse button and the form's message pump can process the messages in the queue.
I tried adding Application.DoEvents() in my finally block but that caused the form to not resize at all.
Any suggestions ?
|
|
|
|
|
I have another question. I'm trying to loop through all the textboxes on a
web application. The snippet is below
//foreach(WebControl ctr in Page.Controls)
foreach(Control ctr in Page.Controls)
{
if(ctr is TextBox)
{
TextBox t = (TextBox)ctr;
t.BackColor = Color.AliceBlue;
t.ReadOnly = false;
}
}
Would you please tell me why it does not work?
|
|
|
|
|