Saving Button location in XML file in Windows Form - c#

My problem is Saving Button location in XML file. My Form have button that are draggable on edit form, so when i am finished editing the form i want to save Button location in XML file, so then i can read the location from the XML file and display the button on the modified position.
Any Suggestions?

Unless it's otherwise a requirement for your application or you need to store additional complex data, you don't really need to create an XML file. You could use a plain text file like this:
var fileContents = string.Format("{0}\r\n{1}\r\n", button.Top, button.Left);
File.WriteAllText("ButtonCoordinates.dat");
Then to read and apply back the coordinates:
var fileContents = File.ReadAllLines("ButtonCoordinates.dat");
button.Top = double.Parse(fileContents[0]);
button.Left = double.Parse(fileContents[1]);
Of course you need to add error checking, file path management et al, but you get the idea.

Related

SendKeys to a windows file dialog

I want to send the string ABC to the input field of a windows file dialog. With this code line I can set the focus to the correct element. I see a blinken cursor.
var filedialogOverlay = drv.SwitchTo().ActiveElement();
But the following code doesn't write the string into the element.
Thread.Sleep(1000);
filedialogOverlay.SendKeys("ABC");
EDIT:
The file upload prompt is shown by a website which I want to test. Because of black box testing I can't see the source code. Is there a tool to analyse the GUI?
When I right click the input element I get the following choices.
You can use the SendKeys.SendWait of Windows Form
//Input the file path into the filename field:
SendKeys.SendWait(longfilepath);
//Input "Enter" key
SendKeys.SendWait(#"{Enter}");
https://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.sendwait(v=vs.110).aspx
If you need to upload file, try to send path to file directly to appropriate input field:
drv.FindElement(By.XPath("//input[#type='file']")).SendKeys("ABC");
P.S. If there are more than one input fields for file upload located on page, you might need more specific XPath, like "//input[#id='some_specific_file_upload']"...

save and save as in Windows Form App by C#

I'm using this code for making save as of image in c#
SaveFileDialog svf = new SaveFileDialog();
svf.Filter = "JPEG files (*.jpeg)|*.jpeg";
if (DialogResult.OK == svf.ShowDialog())
{
this.imgbox.Image.Save(svf.FileName,ImageFormat.Jpeg);
}
I need to make save now for image without change name or location ( apply save not save as as the above code ) how i can use this code for save now ?
Assuming WinForms, add a field or property to the appropriate control to hold the full path to the file currently opened, and call it currentFileName or something.
When opening a file, assign the path to the variable. Now when the user hits the Save menu, you can use the already stored path to overwrite the opened file. You can add a Save As menu that pops up the SaveFileDialog, and afterwards assigns the result to the variable again - if not cancelled.
When the user invokes the New or Close menu, make sure to clear the stored path.

How to create a save button that overwrites previously selected file

How would you make a Save button that, unlike a SaveAs button, will save over the previously selected file without opening a dialog? I have no trouble utilizing SaveAs to open a Save Dialog and create a file but automatically saving to a previously set text file has stumped me.
did you try something like that :
System.IO.File.AppendAllText(previously file path, the new value);

pre-visualize an image

I'd like to pre-visualize an image in a image box before save it in a directory.
How can i do this, i use a checkbox to see if the user wants to pre-visualize or not because i dont find another way to do this without a checkbox.
I use file upload to upload the image.
string serverFileName = "";
serverFileName = Path.GetFileName(Upload.PostedFile.FileName);
Upload.PostedFile.SaveAs(MapPath("~/fotosPerfil/") + serverFileName);
i use this piece of code to save the image.
I think this post is exactly what you need to implement. Check out Ivan's solution.
Yes, indeed you can read the path selected by the user and display the image in an <img> tag, all client-side prior to uploading.

Dynamically append number to PDF or make submit button change its URL based on that number

I'm serving up PDFs from a SQL db and presenting them in the browser. I'm trying to figure out a way to embed a number in the PDF dynamically so that the recordID for that PDFs SQL record is available to me when the user submits the XML form data. The user hits the submit button on the form and the form submits its XML data to my submission page. If there is some way of changing the submission URL on the fly then I could do a query string to pass my self the recordID. I'm not generating the PDF in code, its being created by hand and then uploaded to my site.
Edit
User is given a link someServer.com/pdfLink.aspx?formID=5 they go there and that pages pulls a PDF from the DB and displays it. This pulls up acrobat in browser full size so my aspx page isn't in control of submitting the completed form, Acrobat is. The user fills out the form and hits the submit button in the form. This submit button was set up at form design time to point to another page someSite.com/pdfSubmit.aspx The submit button posts the XML data to that page and I can process it. I need the recordID in the query string for the someSite.com/pdfSubmit.aspx page. To do this I would need to modify the PDF to either add the recordID and query string to the submit button's submit URL, or embed it in the PDF else ware. The big question is how do I modify the PDF just before I display it via someServer.com/pdfLink.aspx?formID=5 to do either of these two options.
Embedding a number in PDF is not exactly kosher, but there are some things that you can do that will honor the spec.
The current PDF spec says that "The last line of the file shall contain only the end-of-file marker
%%EOF
but there is some wiggle room - the implementation details say that it doesn't technically have to be the last line of the file, but only has to appear in the last 1K and, generally speaking, if you don't muck with things too much, most compliant readers won't even blink. If I had to do this, I would be inclined to add a newline (if there isn't one), then a % (which is a PDF comment), a marker to let me know it's mine, and finally the number. So something like:
// assume we already know it ends with %%EOF
void AppendNumberToPdf(Stream stm, int number, bool addNewline)
{
stm.Seek(0, SeekOrigin.End); // go to EOF
StreamWriter writer = new StreamWriter(stm, new ASCIIEncoding(), 1024);
writer.WriteLine(string.Format("{0}% {1} {2}", (addNewLine ? "\n" : ""), kMyMarkerString, number));
writer.Flush();
}
kMyMarkerString should be something like "MyApplicationDocumentIdentifier:" or some such thing that will make it easy to identify your tracks.
The querystring is read-only so you cannot dynamically change it at runtime. However can you:
Add the recordID to the form at the time the submit page is initially rendered
Can you process the submit form and then do a Response.Redirect or Server.Transfer to the correct page with the recordid parameter added to the querystring
While trying #plinth's suggestion I realized I had to change from XML submission (since his data was on the PDF directly. So I changed the form to submit as XDP which has XML data + embedded PDF. When I did this and viewed the raw XDP that the form submitted I ran across this.
<?xml version="1.0" encoding="UTF-8" ?>
<?xfa generator="XFA2_4" APIVersion="3.0.8262.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2010-05-04T15:15:00Z" uuid="6d0944c8-1573-442c-9c85-11e372bd38c3">
<xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<xfa:data>
<form1>
<TextField1>TestMe</TextField1>
</form1>
</xfa:data>
</xfa:datasets>
<pdf href="ViewPDF.aspx?formID=10" xmlns="http://ns.adobe.com/xdp/pdf/" />
</xdp:xdp>
Notice the 2nd to last line. It automatically includes the PDF's url which had the formID value that I needed. So all I had to do was get the XDP instead of pure XML post from the form and it gives me everything I needed.

Categories