I have a List<string> that get's populated with URLs. What I'd like to do is convert the contents of the List to hyperlinks that the user can click on. I've seen a bunch of examples of how to do this, but most of them were to insert in to an email, or switch the word here to a hyperlink. I just don't know what I'm looking at, so it's a little confusing. Here's what I have:
List<string> lstUrls = new List<string>();
//PROGRAM GETS URLS FROM ELEMENTS IN HERE....
foreach (string s in lstUrls)
{
s = ""; //THIS DOESN'T WORK...
}
I don't want to change the content of the string - just to be able to display as a hyperlink. For example, one string value will be https://www.tyco-fire.com/TD_TFP/TFP/TFP172_02_2014.pdf; and how Stack Overflow displays it as a link, that's what I would like to accomplish.
I know I'm obviously botching the syntax. Any help is appreciated.
You can´t change the content of a List<T> while iterating it using foreach. But you can using for:
for(int i = 0; i < lstUrls.Count; i++)
{
var s = lstUrls[i];
lstUrls[i] = "" + s + "";
}
A bit easier to read was this:
lstUrls[i] = String.Format("{0}", s);
You could use linq for it:
lstUrls = lstUrls.Select(s => $"").ToList();
Or rather displaying the url in it:
lstUrls = lstUrls.Select(s => $"{s}").ToList();
Related
I'm trying to pull all the values from another program's DataGridBox. For that I'm using FlaUi. I made a code that does what I want. However, it is very slow. Is there a faster way to pull up all the values from another program's DataGridView using FlaUi?
my code:
var desktop = automation.GetDesktop();
var window = desktop.FindFirstDescendant(cf => cf.ByName("History: NEWLIFE")).AsWindow();
var table = window.FindFirstDescendant(cf => cf.ByName("DataGridView")).AsDataGridView();
int rowscount = (table.FindAllChildren(cf => cf.ByProcessId(30572)).Length) - 2;
// Remove the last row if we have the "add" row
for (int i = 0; i < rowscount; i++)
{
string string1 = "Row " + i;
string string2 = "Symbol Row " + i;
var RowX = table.FindFirstDescendant(cf => cf.ByName(string1));
var SymbolRowX = RowX.FindFirstDescendant(cf => cf.ByName(string2));
SCAN.Add("" + SymbolRowX.Patterns.LegacyIAccessible.Pattern.Value);
}
var message = string.Join(Environment.NewLine, SCAN);
MessageBox.Show(message);
Thank you in-advance
Searching for descendants is pretty slow as it will go thru all objects in the tree until it finds the desired control (or there are no controls left). It might be much faster to use the grid pattern to find the desired cells or get all rows at once and loop thru them.
Alternatively you could try caching as UIA uses inter process calls which are generally slow. So each Find method or value property does such a call. If you have a large grid, that can sum up pretty badly. For that exact case, using UIA Caching could make sense.
For that, you would get everything you need (all descendants of the table and the LegacyIAccessible pattern) in one go inside a cache request and then loop thru those elements in the code with CachedChildren and such.
A simple example for this can be found at the FlaUI wiki at https://github.com/FlaUI/FlaUI/wiki/Caching:
var grid = <FindGrid>.AsGrid();
var cacheRequest = new CacheRequest();
cacheRequest.TreeScope = TreeScope.Descendants;
cacheRequest.Add(Automation.PropertyLibrary.Element.Name);
using (cacheRequest.Activate())
{
var rows = _grid.Rows;
foreach (var row in rows)
{
foreach (var cell in row.CachedChildren)
{
Console.WriteLine(cell.Name);
}
}
}
Am close with this but it still failing. Just adds combobox1 item many times then changes. Combobox2 gets added correctly for each entry of cbo1.
Also adds the delimiter , at front of line in MyFile.txt
I load this with a split no problem, going back is the trouble
I'm like new real new C#.
StreamWriter OutFile = new StreamWriter("MyFile.txt",false);
foreach(object L in comboBox1.Items)
foreach(object M in comboBox2.Items)
{
string lineoftext1 = (L.ToString());
string lineoftext2 = (",");
string lineoftext3 = (M.ToString());
string joinedText;
joinedText = String.Join(lineoftext1, lineoftext2, lineoftext3);
//MessageBox.Show(joinedText);
OutFile.WriteLine(joinedText);
}
OutFile.Close();
OOOOH, i see the problem, it's in the way you are handling your strings, you don't use string.join that way, you can either join them explicitly like this:
joinedText = lineoftext1 + lineoftext2 + lineoftext3;
or you need to create something that the join method accepts, i would use a list, so the inside of your foreach loop would look like this
List<string> mystrings = new List<string>();
mystrings.add(L.ToString());
mystrings.add(M.ToString());
joinedText = String.Join(",", mystrings);
alternativly you could leave it how you have it, but change some values around
joinedText = String.Join(lineoftext2, lineoftext1, lineoftext3);
The first parameter passed to String.Join is the seperator. you were passing it the text.
If this doesn't help, then i would want to see your expected output as i mentioned in my comments above.
Edit: Try this if you want a 1-1 2-2 style
int i = 0;
foreach (object M in comboBox1.Items)
{
List<string> mystrings = new List<string>();
mystrings.Add(comboBox2.Items[i].ToString());
mystrings.Add(M.ToString());
OutFile.WriteLine(String.Join(",", mystrings));
i++;
}
just be aware, this will error if there are different numbers of items in each combobox. (you will get an outside array bounds error)
I am not sure , i think in your combobox1, same items are added when a post back is done, so it repeats back to combobox1 even any changes happens in combobox2. If this is your question, you can overcome this by adding your code inside, if (!IsPostBack), something like this,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//write you code here
}
}
Nikerym you got it man I just added combobox1 below
129 lines as was the original
Thanks M8
StreamWriter OutFile = new StreamWriter("MyFile.txt", false);
int i = 0;
foreach (object M in comboBox1.Items)
{
List<string> mystrings = new List<string>();
mystrings.Add(comboBox1.Items[i].ToString());
mystrings.Add(comboBox2.Items[i].ToString());
OutFile.WriteLine(String.Join(",", mystrings));
i++;
}
OutFile.Close();
I have a text file that has a string like:
"•5•Enter Title Name•Enter Description Here•30/04/2015•UNFINISHED•"
I am reading this text file in, how can I write it so the items between the • are put in seperate variables or strings. I have a general idea, using foreach loops with an if statement that checks for the specific character. Can anybody help me please? (code so far is below)
MessageBox.Show("Opening saved file: TaskFile.txt");
string path1 = (#"TaskFile.txt");
string lineOfText = File.ReadAllText(path1);
System.Diagnostics.Debug.WriteLine(lineOfText);
foreach (var sItem in lineOfText)
{
if(sItem == '•')
{
System.Diagnostics.Debug.WriteLine("test");
}
}
You can use Split method and use it like this
List<string> list = sItem.Split('.').ToList();
I agree with Ehsan on the Split method, but I think it should be like this:
var arrayOfStrings = lineOfText.Split('•');
foreach (var str in arrayOfStrings)
{
//do something.
}
I need to store the values into an array which I already implemented. Sadly, only the latest value is stored on looping. I want all the values to be stored, I tried to use ViewState, but this code below is not working or getting me what I wanted. Please help me.
foreach loop
{
successBenefitCodes = new [] {ben.BenefitCode.ToString()};
ViewState["successBenefitCodes"] = successBenefitCodes;
successBenefitCodes = ViewState["successBenefitCodes"] as string[];
}
You want something like this.
List<string> successBenefitCodes = new List<string>();
foreach(var ben in youritems)
{
successBenefitCodes.Add(ben.BenefitCode.ToString());
}
ViewState["successBenefitCodes"] = successBenefitCodes.ToArray();
i just want to ask help again. I've created a method to read values in gridview, i was able to get and read values from the gridview. The problem now, is how can i store the values inside an array and i want it to pass on the other page.
here's the code i've created
private void getrowvalues()
{
string combinedvalues;
foreach (GridViewRow row in gvOrderProducts.Rows)
{
string prodname = ((Label)row.FindControl("lblProductName")).Text;
string txtvalues = ((TextBox)row.FindControl("txtQuantity")).Text;
combinedvalues = prodname + "|" + txtvalues;
}
}
i want the result string combinedvalues to be put in an array or collection of strings which i can be access in other page. Is there a way to do it? Any inputs will be greatly appreciated.
thanks!!
Just saw KroaX answer which is the same, I leave mine for the example code.
private void getrowvalues()
{
string combinedvalues;
List<string> combinedValuesList = new List<string>();
foreach (GridViewRow row in gvOrderProducts.Rows)
{
string prodname = ((Label)row.FindControl("lblProductName")).Text;
string txtvalues = ((TextBox)row.FindControl("txtQuantity")).Text;
combinedvalues = prodname + "|" + txtvalues;
combinedValuesList.Add(combinedvalues);
}
// use combinedValuesList or combinedValuesList.ToArray()
}
Notepad code, untested...
To pass something from one page to another, you can store it in the session (sometimes it depends).
Session["combinedvalueList"] = combinedvalueList;
While in another page, you can access it.
if (Session["combinedvalueList"]!=null)
combinedValueList = Session["combinedvalueList"] as List<string>;
//Before Foreach
List<string> combinedvalueList = new List<string>();
//Inside Foreach
combinedvalueList.add(combinedvalues);
Please also see
List MSDN
There you can see samples and Methods of List class. It seems to me you are completely new to c# and programming in general ?