c# WPF scroll view Prints based on scroll location - c#

I have built an app on WPF C# that holds invoices and I have created a usercontrol that is assigned to a scrollview.
Everything works perfect until I notice this really anoying printing bug.
So the user control is in the size of an A4. If the scroll view is scrolled to the top at the time of printing :
**PrintDialog pd = new PrintDialog();
pd.PrintVisual(scrollView, "print this off");**
No issues occur. However, if the scroll view is in middle or bottom it prints with an offset.
I have tried to remedy it by moving the scrollview to the top before printing and it does so but the prints come out wrong...
**scrollView.ScrollToHome();
PrintDialog pd = new PrintDialog();
pd.PrintVisual(scrollView, "print this off");**
and yet if I Hit the print button again it will print correctly. I have no idea why it prints before moving it to top as thats the only explanation I have for it...
PLEASE HELP

Call UpdateLayout() after scrolling home:
scrollView.ScrollToHome();
scrollView.UpdateLayout();
(from the docs of the UpdateLayout() function: "Ensures that all visual child elements of this element are properly updated for layout.")

this worked like a charm and simpler than implementing threading. Both good answers
I could not repro the problem with a very minimal sample WPF app consisting of just a ScrollViewer and a Panel. A shot in the dark: try to call scrollView.UpdateLayout() after your call to scrollView.ScrollToHome(). The docs of the UpdateLayout() function say, "Ensures that all visual child elements of this element are properly updated for layout.", and that sounds like what we want. – dlatikay

I have same problem before when I change layout of a Visual right before print action. If I print immediately, I will get wrong result. I guess that the Visual don't have enough time to render properly new layout before you print. I fixed like this:
using System.Threading.Tasks;
async void Print() // Use async
{
PrintDialog pd = new PrintDialog();
scrollView.ScrollToHome();
await Task.Delay(TimeSpan.FromSeconds(1.0)); // Wait 1 second scrollView being properly rendered.
pd.PrintVisual(scrollView, "print this off");
}

Related

C#/WinForms tooltip stops displaying

I'm facing the following issue during setting up tooltips on a WinForms / C# desktop application (using .NET Framework version 4.5).
Application has hundreds of form elements where I would like to display a tooltip.
Current implementation as the following:
I have one toolTip object on my main form
Language file has been loaded and saved in an array
There is a method which suppose to assign the tooltip texts to the different elements accordingly by calling the SetToolTip method on the toolTip object.
E.g.
toolTip.SetToolTip(backBtn, LocalizationFile[0]);
toolTip.SetToolTip(myTextBox, LocalizationFile[1]);
It works fine, tooltips are displayed correctly.
As soon as I reach ca. 30 calls it stops working.
Calling ~30 times SetToolTip method to setup the required tooltips, cause to completely stop displaying the tooltips.
The previously worked tooltip texts are not getting display anymore.
There is no exception or any error message.
Can you please explain me why the toolTip object just stops displaying the texts after calling the SetToolTip method several times? Is there any workaround out there to apply in such cases?
EDIT-1
Following workaround works, but I'm still unsure what is the original problem.
I have created a method to call SetToolTip on the toolTip object, after calling the method, I re-create the toolTip instance using the "new" operator. That solves the issue. However as I want to disable anytime the tooltips on my application I also store all the references in a List. On that list I can iterate over to enable / disable all toolTip references according to what the user wants.
Basically there is a button to toggle the tooltips.
Do you have any idea, why this actually solves the original issue?
List<ToolTip> storeToolTipReferences = new List<ToolTip>(); //store tooltip references
//method begin called to set the tooltip on a control, store the reference of the tooltip and create a new instance
private void SetMyToolTip(Control ctrl, string toolTipText)
{
toolTip.SetToolTip(ctrl, toolTipText);
storeToolTipReferences.Add(toolTip);
toolTip = new ToolTip();
}
//called as the application loads or the user changes the language
private void SetAppLanguage(string[] LocalizationText)
{
storeToolTipReferences.ForEach(e => e.RemoveAll());
SetMyToolTip(ctrl1, LocalizationText[1]);
SetMyToolTip(ctrl2, LocalizationText[2]);
SetMyToolTip(ctrl3, LocalizationText[3]);
.....
}
//logic for tooltip enable/disable in my application
storeToolTipReferences.ForEach(t => t.Active = tooltip_control.Checked);
Thank you!

Windows Form not recognized C#

I am trying to add a new form to look back at printed labels. Simple form to keep track of the Job# printed. However when I try and code the button click to showdialog for the form, nothing happens. It does not recognize that I created the form at all.
I added this EditProductsForm above the JobRecord just to see if there was something wrong with the Button Click. This works fine. On my screen the EditProductsForm is light blue and is shown in the prediction.
EditProductsForm editProductsForm = new EditProductsForm();
editProductsForm.Show();
The JobRecord is not show in prediction and has the red line underneath showing an error?!
JobRecord jobRecord = new JobRecord();
jobrecord.Showdialog();
Any help as to what might have been changed?
You haven't imported the proper namespaces where those class file resides like
using <namespace_name_where_form_class_defined>;

how to create drop down panel with two steps

I would like to create a drop down panel that when you collapse it, it will close only half and when you expand, it will expand and show the other items as shown in below image.
I already tried to use SplitConatainer and by hide the panel manage my work but my problem is when I collapse it the other items not rearrange. please refer to below image.
button code:
if (splitContainer1.Panel2Collapsed)
splitContainer1.Panel2Collapsed = false;
else
splitContainer1.Panel2Collapsed = true;
Also I tried to set size of panel but it is not rendering and effecting.
would you mind help me in introducing some previous work that have ability of customization. Or help me to start create such component please?
I solve my problem by add a parameter to project "ExpandCollapsePanel" in GitHub.
So Just by adding a value to MaximumCollapseHeight, you can control panel.
you can get the source code from here.
enter link description here

Off screen element teststack white

I need to automate 3rd party WPF application. I use
TestStack/White. This application has menu bar that is presented by images. After some actions menu is changing. There presents new images. When I want to click on new image:
Window mainWindow = application.GetWindow("Main window", InitializeOption.NoCache);
Image newTask = mainWindow.Get<Image>(SearchCriteria.ByControlType(ControlType.Image).AndIndex(2));
newTask.Click();
I get exception:
TestStack.White.AutomationException: Cannot perform action on Image.
AutomationId:, Name:, ControlType:image, FrameworkId:WPF, element is
offscreen.
I use Microsoft Inspect for research elements.
When I start tests, Inspect show me that image is offscreen. But if I do these actions manually, it works perfectly and in Inspect this image is not offscreen.
How can I refresh these elements or clear cache of window?
There are ReInitialize and ReloadIfCached methods on Window object. Try those to see if something changes.
Are you sure AndIndex(2) is correct element in that particular situation?
Try using GetMultiple and iterate the collection to see what images you actually have and which are not Offscreen.
WPF automation with White is pretty hard. Try Telerik Testing Framework and White could be supporting framework. It is much more easier that way.
It can be a problem with focus, try to use this before getting image:
mainWindow.Focus(DisplayState.Maximized);
Not an exact answer but the final solution I've got after all these TestStack.White not found elements, table rows, and so on. I started to move it to FlaUI. If something does not work or unstable with White then most likely I can get more stable and fast-executable FlaUI solution.
Fortunatly, such migration can be done with little steps. For example I already have TestStack.White.Application app, then I replace White portion of code with FlaUI like this:
var flApp = FlaUI.Core.Application.Attach(app.Process.Id);
using (var automation = new UIA3Automation())
{
// .. new UI element processing
}
I don’t think that caching is the problem here. You are getting the mainWindow with InitializeOption.NoCache. In Non-cache mode the controls are found on demand. So I presume that the cache is refreshed automatically. (https://github.com/TestStack/White/blob/master/src/TestStack.White/Factory/InitializeOption.cs)
Perhaps the index of the element you want to click is not 2.
Have you tried adding an explicit wait? It sounds like you have only tried adding an implicit wait.(https://github.com/TestStack/TestStack.docs/blob/master/_source/White/Advanced%20Topics/Waiting.md)

Print a chart (toolkit)

I would like to be able to print the chart I make with silverlight-toolkit.
The application is written in Silverlight 4.
However I have no idea how to get this done.
Does anybody know how this works or has a tutorial somewhere?
I have been searching at Google to see if I could find something, but I didn't find anything that worked for me.
Thanks in advance.
EDIT:
I'm useing the following code after I click on the print button but nothing happends:
Chart chartToPrint; // The element to be printed
PrintDocument doc = new PrintDocument(); // Create the PrintDocument object that will do the printing
doc.PrintPage += (s, args) =>
{
// Set the chart that needs to be printed.
// As soon as this is set, printing starts
args.PageVisual = chartToPrint;
}
Edit:
Things I have tried:
http://gergelyorosz.com/2010/05/printing-in-silverlight-printing-charts-and-auto-scaling/
http://kb.yworks.com/article507.html
http://www.visiblox.com/blog/2010/05/advanced-printing-in-silverlight-printing-charts-and-auto-scaling
http://msdn.microsoft.com/en-us/library/system.drawing.printing.pagesettings.landscape(v=vs.110).aspx
printing an image in landscape orientation?
http://msdn.microsoft.com/en-us/magazine/hh148152.aspx
Have a look at this site:http://kb.yworks.com/article507.html . Hope this helps you!

Categories

Resources