I am using Awesomium within C# and I am dragging variables out of the javascript within.
The code that is run:
if (GotCategories == false) {
if (WebBrowser.ExecuteJavascriptWithResult("finished").ToString() == "true" && WebBrowser.IsDocumentReady == true) {
int Level1Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories.length").ToString());
for (int i = 0; i < Level1Len - 1; i++) {
string Level1 = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].title");
string Level1ID = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].id");
TreeNode treenode = new TreeNode(Level1);
CatsTree.Nodes.Add(treenode);
int Level2Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub.length").ToString());
for (int j = 0; j < Level2Len; j++) {
string Level2 = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].title");
string Level2ID = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].id");
treenode = new TreeNode(Level2);
CatsTree.Nodes[i].Nodes.Add(treenode);
int Level3Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub.length").ToString());
for (int k = 0; k < Level3Len; k++) {
string Level3 = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].title");
string Level3ID = WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].id");
treenode = new TreeNode(Level3);
CatsTree.Nodes[i].Nodes[j].Nodes.Add(treenode);
if (WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].sub.length").IsInteger == true) {
string Level4Len = Convert.ToInt32(WebBrowser.ExecuteJavascriptWithResult("all_categories[" + i + "].sub[" + j + "].sub[" + k + "].sub.length").ToString());
A weird issue happens though..
If I run it how it is now Level4Len throws an exception error
Input string was not in a correct format.
Though.. Weirdly enough if I put a MessageBox.Show(Level4Len); I do not receive the error and.. as well as that the outputs that are shown are completely normal as they should be.
Weirdest thing I have seen..
Related
I recently make a program to calculate sum and avg of a given array. The program also want to print the histogram or stars pattern match with value of index.
Like this:
This is the code I wrote so far:
private void process_Click(object sender, EventArgs e)
{
string temp2 = null;
string temp3 = null;
float sum=0;
int countingNumber = 1;
string message;
int count = Convert.ToInt32(inputArray.Text);
int[] varray = new int[count];
for (int i = 0; i < count; i++)
//for (int j = 1; j <= count; j++)
{
varray[i] = Convert.ToInt32(Interaction.InputBox(message = "enter the value of array number " + countingNumber));
sum = sum+ varray[i];
temp += countingNumber + " "+varray[i] + Environment.NewLine;
temp2 += countingNumber + " " + varray[i] + " *" + Environment.NewLine;
box1.Text = Convert.ToString("Index Value" + Environment.NewLine + temp);
boxSum.Text = Convert.ToString(sum);
boxAvg.Text = Convert.ToString(sum/count);
countingNumber++;
}
for (int stars = 0; stars <= i; stars++)
{
temp3 = " ";
box2.Text = Convert.ToString("Element Value Histogram" + Environment.NewLine + temp2+temp3);
}
}
}
My code won't print the stars match with the value. Could anybody help me?
Try replacing this line:
temp2 += countingNumber + " " + varray[i] + " *" + Environment.NewLine;
with this line:
temp2 += countingNumber + " " + varray[i] + " " + new String('*', i) + Environment.NewLine;
need to do an arr[n,n] which will do a result as [0,0] = 0 [0,1] = 1 [0,2] = 3 [0,3] = 6 [1,1] = 0 [1,2] = 2 [1,3] = 5 [2,2] = 0 [2,3] = 3 [3,3] = 0
Trying to feed that arr with two for cycles. Anyway, can't figure out how to set conditions to generate it as I want it.
Any hint is welcome.
I tried to feed an array with two for loops, where I tried to sum values. So many errors occur when I start typing code.
E: Need to solve last if (else if (i > j && j == 0)), problem is out of bounds. Any idea? Don't want to shuffle everything. Need to calculate [1-4,0] value.
== [4,0] = 5, [3,0] = 9, [2,0] = 12, [1,0] = 14
Console.Write("Zadejte n: ");
int n = Convert.ToInt32(Console.ReadLine()) ;
int[,] array_prava = new int[n, n];
int[,] array_leva = new int[n, n];
int pulka = n / 2;
for (int i = 0; i < n; i++) // array_prava
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j];
Console.WriteLine("[" + i + "," + j + "] " + array_prava[i, j]);
}
else if (j > i)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j - 1] + i;
Console.WriteLine("11[" + i + "," + j + "] " + array_prava[i, j]);
}
else if (i > j && j == 0){
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j - 1] + i;
Console.WriteLine("22[" + i + "," + 0 + "] " + array_prava[i, 0]);
}
}
// Console.Write("----------------------------\n");
}
any hint is welcome
Got it.
for (int i = 0; i < n; i++) // array_prava
{
for (int j = 0; j < n; j++)
{
if (i == j)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j];
Console.WriteLine("[" + i + "," + j + "] " + array_prava[i, j]);
}
else if (j > i)
{
array_prava[i, j] = Math.Abs(j - i) + array_prava[i, j - 1] + i;
Console.WriteLine("11[" + i + "," + 0 + "] " + array_prava[i, 0]);
}
}
}
I'm working on a C# assignment in which i have to create a minimum spanning tree through the use of arrays. My code is made up of three arrays which track the nodes and whether or not they have been reached. I have to find the smallest total for all of the randomly generated links to be added up to visit all of the nodes. However, for some reason it is not quite working. The totals being produced are not correct and just wondered if anyone could help me work out why.I believe the error must be between the calculating of the total or how the minimum value is being decided or possible both however i am unsure.
So far i have tried changing the way it is calculated to involve a sum variable to store the values seperately. Ive attempted to add another if statement that would stop the min value from being anything above 98 ( the void value is 99) i have also tried to alter the earlier code to see if how i am testing the values in the SP array are the desired ones. still no results
int n = 5; //n = number of values
int m = 50; //m = max value in arra
int VoidValue = 99; // if i = j value = void value
int Total = 0; //Total value for spanning tree
int sum = 0; //Sum for total value for spanning tree
Random Rand = new Random(); //Create randomise value
int[,] c = new int[n + 1, n + 1]; //Cost array
int[] SP = new int[n + 1]; //Spanned array
int[,] AD = new int[n + 1, n + 1]; //Adjacency array
for (int i = 1; i <= n; i++)
{
SP[i] = 0;
for (int j = 1; j <= n; j++)
{
if (i == j)
{
c[i, j] = VoidValue; // give void spaces the value of 99
AD[i, j] = 0;
}
else
{
c[i, j] = Rand.Next(1, m); // Populate the array with randomised values
AD[i, j] = 0;
}
}
}
//Output all arrays to screen
Console.WriteLine("Cost Array: At the beginning");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
Console.Write("c[" + i + "," + j + "] = " + "- " + " ");
}
else
{
Console.Write("c[" + i + "," + j + "] = " + c[i, j].ToString("00") + " ");
}
}
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Spanned Array : At the beginning");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.Write("S[" + i + "] = " + SP[i].ToString("00") + " ");
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Adjacency Array : At the beginning");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
Console.Write("A[" + i + "," + j + "] = " + "- " + " ");
}
else
{
Console.Write("A[" + i + "," + j + "] = " + AD[i, j].ToString("00") + " ");
}
}
Console.WriteLine();
}
Console.ReadLine();
// Random Starting Point
int startPoint = Rand.Next(1, n + 1);
Console.WriteLine("Start at node " + startPoint);
SP[startPoint] = 1;
//Check the spanned array
Console.WriteLine("");
Console.WriteLine("Spanned Array : After the starting point has been chosen ");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.Write("S[" + i + "] = " + SP[i].ToString("00") + " ");
Console.WriteLine();
}
Console.ReadLine();
// Find minimum vallue link, repeatedly follow these links until spanned array is full
for (int p = 1; p < n; p++) // For every value of the spanned array
{
int MinValue = VoidValue, MinValuei = 0, MinValuej = 0; // declare variables
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++) // I and J are for both variables of the cost and adjacent array
if (i != j)
{
if (SP[i] == 1) //Spanned node
{
if (SP[j] == 0) //Unspanned node
{
if (c[i, j] < MinValue)
{
MinValue = c[i, j];
MinValuei = i;
MinValuej = j;
}
}
}
}
AD[MinValuei, MinValuej] = 1;
SP[MinValuej] = 1;
}
Console.WriteLine("");
Console.WriteLine("The min value is: " + MinValue);
Total = Total + MinValue;
Console.WriteLine("");
Console.WriteLine("The total is: " + Total);
}
//Finally output spanned and adjacent arrays
Console.WriteLine("");
Console.WriteLine("Spanned Array: After spanning tree");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
Console.Write("S[" + i + "] = " + SP[i].ToString("00") + " ");
Console.WriteLine();
}
Console.ReadLine();
Console.WriteLine("");
Console.WriteLine("Adjacency Array : After spanning tree");
Console.WriteLine("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (i == j)
{
Console.Write("A[" + i + "," + j + "] = " + "- " + " ");
}
else
{
Console.Write("A[" + i + "," + j + "] = " + AD[i, j].ToString("00") + " ");
}
}
Console.WriteLine();
}
Console.ReadLine();
//Output total value for the spanning tree
Console.WriteLine("");
Console.WriteLine("The total value for the spanning tree: " + Total);
Console.WriteLine("");
Console.ReadLine();
I can't figure out why the code don't work properly, when i click on save button show me Yokoso Log(1) then second save is showing Yokoso Log(1).txt(2).txt .....
//Create txt and write
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Yokoso Log\\Yokoso Log");
TextWriter txtwrite = new StreamWriter(logPath);
int count = 1;
Find:
if (File.Exists(logPath))
{
logPath = logPath + "(" + count.ToString() + ").txt";
count++;
goto Find;
}
else
{
File.Create(logPath);
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
txtwrite.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
txtwrite.WriteLine("");
txtwrite.WriteLine("____________________________________________________________________");
}
txtwrite.Close();
MessageBox.Show("Log create successfully (directory desktop).");
}
}
What you are trying to do is something like this:
var currentPath = logPath;
while (File.Exists(currentPath))
{
currentPath = logPath + "(" + count.ToString() + ").txt";
count++;
}
File.Create(currentPath);
...
here you are creating a file
TextWriter txtwrite = new StreamWriter(logPath);
then when you you check for the file sure enough there is a file
if (File.Exists(logPath))
this is what I think you mean to do
string logPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Yokoso Log\\Yokoso Log");
int count = 1;
while (File.Exists(logPath))
{
logPath = logPath + "(" + count.ToString() + ").txt";
count++;
}
using (TextWriter txtwrite = new StreamWriter(logPath))
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
txtwrite.Write("\t" + dataGridView1.Rows[i].Cells[j].Value.ToString() + "\t" + "|");
}
txtwrite.WriteLine("");
txtwrite.WriteLine("____________________________________________________________________");
}
}
MessageBox.Show("Log create successfully (directory desktop).");
How can i make the below code more efficient, with less lines.
Im adding some PictureBox elements to a two dimensinal array.
int a = 0;
int b = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " a = " + a);
Console.WriteLine("Iteration: " + i + " b = " + b);
pictureBoxArr[a, b] = new PictureBox();
b++;
}
int aa = 1;
int bb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aa = " + aa);
Console.WriteLine("Iteration: " + i + " bb = " + bb);
pictureBoxArr[aa, bb] = new PictureBox();
bb++;
}
int aaa = 2;
int bbb = 0;
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Iteration: " + i + " aaa = " + aaa);
Console.WriteLine("Iteration: " + i + " bbb = " + bbb);
pictureBoxArr[aaa, bbb] = new PictureBox();
bbb++;
}
I was thinking something like this - but im kinda stuck.
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; i++)
{
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
You almost had it.
If you want something more re-usable, you could set up a couple variables to hold your bounds.
int boundX = 10;
int boundY = 10;
for (int i = 0; i < boundX ; i++)
{
for (int j = 0; j < boundY ; j++)
{
pictureBoxArr[i, j] = new PictureBox();
}
}
Try:
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
Console.WriteLine("Iteration: " + i + " a = " + i);
Console.WriteLine("Iteration: " + j + " b = " + j);
pictureBoxArr[i, j] = new PictureBox();
}
}
Dude, you are very close