how do i select the node's index? - wpf

I have a xml populated combobox. If builderemail (from parsing of text using streamreader) is equals to the any one value found in the xml file, the combobox will select the index. how do i go about selecting it?
if (line.StartsWith("Builder_Email:"))
{
bool IsNodeExists = false;
string[] fields = line.Split('\t');
string builderemail = fields[3];
XmlDocument emailparse = new XmlDocument();
emailparse.Load(#"C:\GUI\buildermanageremail.xml");
XmlNodeList emailnode = emailparse.GetElementsByTagName("value");
if (string.IsNullOrEmpty(builderemail))
comboBox1.SelectedIndex = -1;
else
foreach (XmlNode node in emailnode)
{
if (builderemail == node.InnerText)
{
// how do i get the combobox selection right?
// need some code here
IsNodeExists = true;
break;
}
}
if(!IsNodeExists)
{
//create main node
XmlNode abc = emailparse.CreateNode(XmlNodeType.Element, "builder", null);
//create the first child node
XmlNode value = emailparse.CreateElement("value");
//set the value
value.InnerText = builderemail;
// add childes to father
//node.AppendChild(id);
abc.AppendChild(value);
// find the node we want to add the new node to
XmlNodeList l = emailparse.GetElementsByTagName("builderemail");
// append the new node
l[0].AppendChild(abc);
// save the file
emailparse.Save(#"C:\GUI\buildermanageremail.xml");
//then we populate the new updated xml file into the drop down list:
PopulateDDLFromXMLFile();
int count = emailparse.SelectNodes("email/builderemail/builder").Count;
count--;
comboBox1.SelectedIndex = count;
}
}
the place to look at is here:
foreach (XmlNode node in emailnode)
{
if (builderemail == node.InnerText)
{
// how do i get the combobox selection right?
// need some code here
IsNodeExists = true;
break;
}
}

I believe this code does everything that you want your code to do. This code is certainly not perfect, and may not even work, but if you compare it to yours you should find that it employs probably half a dozen practices that you're not following in your code and probably should be. If you cut out all the assertions, you'll find that it's only 10 lines of code (not counting the refactored method).
if (line.StartsWith("Builder_email:"))
{
Debug.Assert(
line.Where(x => x == '\t').Count() > 2),
"Can't parse input line.");
string builderEmail = line.Split('\t')[3];
Debug.Assert(
builderEmail != null && builderEmail == builderEmail.Trim(),
"Input data is bad.");
string filename = #"C:\GUI\buildermanageremail.xml"
Debug.Assert(
File.Exists(filename),
"Expected XML file does not exist.");
XmlDocument emailXml = new XmlDocument();
emailXml.Load(filename);
// In your real application, you know the name of the document element, so you
// should replace * with it in the XPath below.
string xpath = string.Format(
"/*/builderemail/builder/value[.='{0}']",
builderEmail);
if (emailXml.SelectSingleNode(xpath) == null)
{
CreateBuilderEmailElement(emailXml, builderEmail);
emailXml.Save(filename);
// I've changed the name of this method, which is problematic for several
// reasons - not least of which is that in my world, at least, "DDL" means
// "Data Definition Language."
//
// This also assumes that you've created an overload of the method that
// takes an XmlDocument argument.
PopulateEmailComboBox(emailXml);
}
// I'm assuming that the builderEmail is the actual text value stored in the
// combo box items, in which case all you need to do is find the item with that
// value and set SelectedItem, which will automatically set SelectedIndex. Also,
// if the value isn't found, setting SelectedItem to null automatically sets
// SelectedIndex to -1.
builderEmailComboBox.SelectedItem = builderEmailComboBox.Items
.Where(x => x.ToString() == builderEmail)
.FirstOrNull();
}
And here's the method for creating the builderemail element - which, by the way, should be named builderEmail if you have any say over it:
// this is refactored out as its own function, and made internal so that you
// can unit test it.
internal void CreateBuilderEmailElement(XmlDocument emailXml, string builderEmail)
{
XmlElement builder = emailXml.CreateNode("builder");
XmlElement value = emailXml.CreateNode("value");
builder.AppendChild(valueElm);
value.InnerText = builderEmail;
// again, you know the name of the document element in your application,
// so replace the * below with it.
Debug.Assert(
emailXml.SelectSingleNode("/*/builderemail") != null,
"No builderemail element found under the document element.");
emailXml.SelectSingleNode("/*/builderemail").AppendChild(builder);
}
Also, is there a reason that your XML has a separate value element under builderEmail, instead of builderEmail just containing the value?

Related

Update Text of FlowDocument in the CodeBehind

I need to alter the text of a FlowDocument without changing the existing formatting and am having trouble doing so.
My thought was to do a foreach of Blocks in the document. Then for any Paragraph do a foreach of the Inlines like this;
foreach (var x in par.Inlines)
{
if (x.GetType() == typeof(Run))
{
Run r = (Run)x;
r.Text = r.Text.Replace("#", "$");
}
}
Problem is that this returns the following error message;
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
What is the correct way of doing this?
The usual solution is to call ToList() on the collection and iterate through the new collection returned by ToList().
var runs =
flowdoc.Blocks.OfType<Paragraph>()
.SelectMany(par => par.Inlines).OfType<Run>()
.ToList();
foreach (var r in runs)
{
r.Text = r.Text.Replace("#", "$");
}
Your error comes from trying to use the foreach loop to enumerate through a collection while also modifying the collection. Use a for loop.
For changing text in a flow document, try a TextPointer + TextRange, here's an example (this one changes the text background but you can change text just as easily).
private void ClearTextHighlight(FlowDocument haystack)
{
TextPointer text = haystack.ContentStart;
TextPointer tpnext = text.GetNextContextPosition(LogicalDirection.Forward);
while (tpnext != null){
TextRange txt = new TextRange(text, tpnext);
//access text via txt.Text
//apply changes like:
var backgroundProp = txt.GetPropertyValue(TextElement.BackgroundProperty) as SolidColorBrush;
if(backgroundProp != null && backgroundProp.Equals(Setting_HighlightColor)){
//change is here
txt.ApplyPropertyValue(TextElement.BackgroundProperty, Setting_DefaultColor);
}
text = tpnext;
tpnext = text.GetNextContextPosition(LogicalDirection.Forward);
}
}

Copy list to Flowdocument messes up 1st listitem

I'm writing an extension that allows a user to merge multiple notes into a single note and provides some features like adding periods onto the end of the original notes. I'm writing the code that copies the parts of one flowdocument to another and inserts the periods as it goes.
I am having problems copying lists to the new document. For some reason the first listitem always ends up in the paragraph PREceeding the list instead of in the list.
My code:
foreach (Block b in tempDoc.Blocks)
{
thisBlock++;
if (b is List)
{
pkhCommon.WPF.Helpers.AddBlock(b, mergedDocument);
}
else
{
Paragraph p = b as Paragraph;
foreach (Inline inl in p.Inlines)
{
if (!(inl is LineBreak))
pkhCommon.WPF.Helpers.AddInline(inl, mergedDocument);
}
if (thisElement != lastElement || thisBlock != lastBlock)
if ((bool)cb_AddPeriods.IsChecked)
pkhCommon.WPF.Helpers.AddInline(new Run(". "), mergedDocument);
else
pkhCommon.WPF.Helpers.AddInline(new Run(" "), mergedDocument);
}
}
Below is the function to merge the blocks. The AddIline function works the same way.
public static void AddBlock(Block from, FlowDocument to)
{
if (from != null)
{
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
TextRange range = new TextRange(from.ContentStart, from.ContentEnd);
System.Windows.Markup.XamlWriter.Save(range, stream);
range.Save(stream, DataFormats.XamlPackage);
TextRange textRange2 = new TextRange(to.ContentEnd, to.ContentEnd);
textRange2.Load(stream, DataFormats.XamlPackage);
}
}
}
I can't understand why the flowdocument is deciding to put the listitem into the PREceeding paragraph.
Adding a block to the FlowDocument's block collection should put it at the end.
This works for for me.
Document.Blocks.Add(blockToAdd);
You are doing the save/load to clone the block right? Can you just try adding it this way at the end instead of inserting in the text range?
var blockToAdd = XamlReader.Load(stream) as Block;
Document.Blocks.Add(blockToAdd);
Part of your issue is that after saving, the stream position is at the end of the stream, so there is nothing to Load. There is probably a better way to fix this but I am out of time to help. Setting position to 0 feels wrong. This has no parse exception.
var from = new System.Windows.Documents.List(new ListItem(new Paragraph(new Run("Blah"))));
using (var stream = new MemoryStream())
{
System.Windows.Markup.XamlWriter.Save(from, stream);
stream.Position = 0;
Block b = System.Windows.Markup.XamlReader.Load(stream) as Block;
}

how do i get imagedownloadservice to work with multilist component in codenameone?

I have created a multi list in GUI Designer. I am setting the model as below
#Override
protected boolean initListModelMultiIssueList(List cmp) {
fetchIssues(cmp);
if (issueVector != null ) {
cmp.setModel(new DefaultListModel(issueVector));
System.out.println(cmp);
}
return true;
}
void fetchIssues( List c){
//fetch issues based on the searchquery hash
//first thing is to create the query from the hash
System.out.println("Starting to fetch results");
try{
java.util.List<ServiceRequest> serviceRequests = ServiceRequest.getServiceRequests(formQuery(searchQuery),true);
//we need to now populate the issueVector
//with the data
System.out.println(serviceRequests.toString());
if (issueVector != null ) {
issueVector.clear();
} else {
issueVector = new Vector();
}
int index = 0;
for (ServiceRequest serviceRequest : serviceRequests) {
Hashtable hIssue = new Hashtable();
hIssue.put("id",serviceRequest.getHref());
//System.out.println(hIssue);
ImageDownloadService.createImageToStorage(serviceRequest.getRequestPictureURL().toString(),
c, index, "icon",
"service-icon-"+ index ,null);
//hIssue.put("icon", serviceRequest.getRequestPictureURL().toString());
//System.out.println(hIssue);
//reverse geocode the location
Double x = new Double(0.0);
x=new Double(serviceRequest.getRequestLocationLatitude());
Double y = new Double(serviceRequest.getRequestLocationLongitude());
String location=reverseGeocode(x, y);
hIssue.put("location", location);
//System.out.println(hIssue);
Service service = serviceRequest.loadService();
hIssue.put("service", serviceRequest.loadService().getName().toString());
hIssue.put("reportedOn",serviceRequest.getCreatedAt().toString());
//System.out.println("Final hIssue" + hIssue.toString());
issueVector.add(hIssue);
index=index+1;
System.out.println(issueVector);
}
}catch (Exception e){
System.out.println("Error loading search results");
System.out.println(e);
}
}
The icon in the multi list GUI design has been set to the appropriate property. ImageDownloadService does download the image files but then it does not display in the list as expected. What am I doing wrong?
Its possible that the image is downloaded before the entry is available. Although its hard to tell with the code and without a clear explanation of the symptoms.
You need to first create the model and set it to the list (ideally with a blank placeholder image so the list doesn't "jump"). Then you need to loop over the list and invoke the image download service, otherwise it might return before the data is in the list and fail! This can happen if the image is already in cache so its very likely to fail fast in that case.

How to move by code the BindingSource to a specific record

Using datagridview bound to BindingSource control bound to a LINQ to SQL class, I wonder how to position the bindingSource to a specific record, that is, when I type a Product name in a textbox, the bindingsource should move to that specific product. Here is my code:
In my form FrmFind:
NorthwindDataContext dc;
private void FrmFind_Load(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
var qry = (from p in dc.Products
select p).ToList();
FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
int index = productBindingSource.Find("ProductName", tb.Text);
if (index >= 0)
{
productBindingSource.Position = index;
}
}
In the program class:
public class FindAbleBindingList<T> : BindingList<T>
{
public FindAbleBindingList()
: base()
{
}
public FindAbleBindingList(List<T> list)
: base(list)
{
}
protected override int FindCore(PropertyDescriptor property, object key)
{
for (int i = 0; i < Count; i++)
{
T item = this[i];
//if (property.GetValue(item).Equals(key))
if (property.GetValue(item).ToString().StartsWith(key.ToString()))
{
return i;
}
}
return -1; // Not found
}
}
How can I implement the find method to make it work?
You can combine the BindingSource.Find() method with the Position property.
For example, if you have something like this in your TextBox changed event handler:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
int index = bs.Find("Product", tb.Text);
if (index >= 0)
{
bs.Position = index;
}
}
This of course will depend on a lot of things like the particular implementation of the Find method the data source for the binding source has.
In a question you asked a little while ago I gave you an implementation for Find which worked with full matches. Below is a slightly different implementation that will look at the start of the property being inspected:
protected override int FindCore(PropertyDescriptor property, object key)
{
// Simple iteration:
for (int i = 0; i < Count; i++)
{
T item = this[i];
if (property.GetValue(item).ToString().StartsWith(key.ToString()))
{
return i;
}
}
return -1; // Not found
}
Do note that the above method is case sensitive - you can change StartsWith to be case insensitive if you need.
One key thing to note about the way .Net works is that the actual type of an object is not sufficient all the time - the declared type is what consuming code knows about.
This is the reason why you get a NotSupported exception when calling the Find method, even though your BindingList implementation has a Find method - the code that receives this binding list doesn't know about the Find.
The reason for that is in these lines of code:
dc = new NorthwindDataContext();
var qry = (from p in dc.Products
select p).ToList();
FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
When you set the data source for the binding source you include the extension method OrderBy - Checking this shows that it returns IOrderedEnumerable, an interface described here on MSDN. Note that this interface has no Find method, so even though the underlying FindableBindingList<T> supports Find the binding source doesn't know about it.
There are several solutions (the best is in my opinion to extend your FindableBindingList to also support sorting and sort the list) but the quickest for your current code is to sort earlier like so:
dc = new NorthwindDataContext();
var qry = (from p in dc.Products
select p).OrderBy(p => p.ProductName).ToList();
FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
productBindingSource.DataSource = list;
In WinForms there are no entirely out of the box solutions for the things you are trying to do - they all need a little bit of custom code that you need to put together to match just your own requirements.
I took a different approach. I figured, programmatically, every record must be checked until a match is found, so I just iterated using the MoveNext method until I found a match. Unsure if the starting position would be the First record or not, so I used the MoveFirst method to ensure that is was.
There is one assumption, and that is that what you are searching for is unique in that column. In my case, I was looking to match an Identity integer.
int seekID;
this.EntityTableBindingSource.MoveFirst();
if (seekID > 0)
{
foreach (EntityTable sd in EntityTableBindingSource)
{
if (sd.ID != seekID)
{
this.t_EntityTableBindingSource.MoveNext();
}
else
{
break;
}
}
}
I didn't really care for either answer provided. Here is what I came up with for my problem:
// Create a list of items in the BindingSource and use labda to find your row:
var QuickAccessCode = customerListBindingSource.List.OfType<CustomerList>()
.ToList().Find(f => f.QuickAccessCode == txtQAC.Text);
// Then use indexOf to find the object in your bindingSource:
var pos = customerListBindingSource.IndexOf(QuickAccessCode);
if (pos < 0)
{
MessageBox.Show("Could not find " + txtQAC.Text);
}
else
{
mainFrm.customerListBindingSource.Position = pos;
}

System.Printing.PrintQueue QueueStatus not updating

Is there a way to update the print queue status information contained in the PrintQueue object?
I've tried calling Refresh on the PrintQueue object but that doesn't really do anything. For instance, I've turned off the printer and the Control Panel correctly shows the printer as "Offline", however the QueueStatus property, as well as the IsOffline property don't reflect that - no matter how many times I call Refresh on both the PrintServer and the PrintQueue in question.
I've seen examples of how to get status information using WMI queries but I wonder - since these properties are available on the PrintQueue object - whether there is any way to use those.
After try to print your PrintDocument (System.Drawing.Printing), try to check status of printjobs.
First step:
Initialize your printDocument.
Second step:
Get your printer Name From System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast<string>();
And copy it into your printerDocument.PrinterSettings.PrinterName
Third step:
Try to print and dispose.
printerDocument.Print();
printerDocument.Dispose();
Last step: Run the check in a Task (do NOT block UI thread).
Task.Run(()=>{
if (!IsPrinterOk(printerDocument.PrinterSettings.PrinterName,checkTimeInMillisec))
{
// failed printing, do something...
}
});
Here is the implementation:
private bool IsPrinterOk(string name,int checkTimeInMillisec)
{
System.Collections.IList value = null;
do
{
//checkTimeInMillisec should be between 2000 and 5000
System.Threading.Thread.Sleep(checkTimeInMillisec);
using (System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PrintJob WHERE Name like '%" + name + "%'"))
{
value = null;
if (searcher.Get().Count == 0) // Number of pending document.
return true; // return because we haven't got any pending document.
else
{
foreach (System.Management.ManagementObject printer in searcher.Get())
{
value = printer.Properties.Cast<System.Management.PropertyData>().Where(p => p.Name.Equals("Status")).Select(p => p.Value).ToList();
break;
}
}
}
}
while (value.Contains("Printing") || value.Contains("UNKNOWN") || value.Contains("OK"));
return value.Contains("Error") ? false : true;
}
Good luck.

Resources