Xamarin studio UIImageView - c#

I can load a picture to a UImageView with I click a button, but not from a timer. why is that ? and how can I fix it ?
here is my code :
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
Patter_Function();
switch_Function();
}
void switch_Function()
{
image1 = new UIImage();
image1 = UIImage.FromBundle("heart");
image2 = new UIImage();
image2 = UIImage.FromFile( #"White_Sharingan.png");
image3 = new UIImage();
image3 = UIImage.FromFile("snow.png");
image4 = new UIImage();
//image4.//FromFile("Images/waterfront.jpg");
switch (Patter_1[switch_Counter].Move0) {
case 0 :
Box_1.Image = image1;
break;
case 1 :
Box_1.Image = image1;
break;
case 2 :
Box_2.Image = image1;
break;
case 3:
Box_3.Image = image1;
break;
case 4:
Box_4.Image = image1;
break;
case 5:
Box_5.Image = image1;
break;
case 6:
Box_6.Image = image1;
break;
case 7:
Box_7.Image = image1;
break;
case 8:
Box12.Image = image1;
break;
case 9:
Box_9.Image = image1;
break;
case 10:
Box_10.Image = image1;
break;
case 11:
Box_11.Image = image1;
break;
case 12:
Box8.Image = image1;
break;
case 13:
Box_13.Image = image1;
break;
case 14:
Box_14.Image = image1;
break;
case 15:
Box_15.Image = image1;
break;
case 16:
Box_16.Image = image1;
break;
case 17:
Box_17.Image = image1;
break;
case 18:
Box_18.Image = image1;
break;
case 19:
Box_19.Image = image1;
break;
case 20:
Box_20.Image = image1;
break;}
switch (Patter_1[switch_Counter].Move1)
{
case 0:
Box_1.Image = image2;
break;
case 1:
Box_2.Image = image2;
break;
case 2:
Box_2.Image = image2;
break;
case 3:
Box_3.Image = image2;
break;
case 4:
Box_4.Image = image2;
break;
case 5:
Box_5.Image = image2;
break;
case 6:
Box_6.Image = image2;
break;
case 7:
Box_7.Image = image2;
break;
case 8:
Box12.Image = image2;
break;
case 9:
Box_9.Image = image2;
break;
case 10:
Box_10.Image = image2;
break;
case 11:
Box_11.Image = image2;
break;
case 12:
Box8.Image = image2;
break;
case 13:
Box_13.Image = image2;
break;
case 14:
Box_14.Image = image2;
break;
case 15:
Box_15.Image = image2;
break;
case 16:
Box_16.Image = image2;
break;
case 17:
Box_17.Image = image2;
break;
case 18:
Box_18.Image = image2;
break;
case 19:
Box_19.Image = image2;
break;
case 20:
Box_20.Image = image2;
break;
}
switch (Patter_1[switch_Counter].Move2)
{
case 0:
Box_1.Image = image3;
break;
case 1:
Box_2.Image = image3;
break;
case 2:
Box_2.Image = image3;
break;
case 3:
Box_3.Image = image3;
break;
case 4:
Box_4.Image = image3;
break;
case 5:
Box_5.Image = image3;
break;
case 6:
Box_6.Image = image3;
break;
case 7:
Box_7.Image = image3;
break;
case 8:
Box12.Image = image3;
break;
case 9:
Box_9.Image = image3;
break;
case 10:
Box_10.Image = image3;
break;
case 11:
Box_11.Image = image3;
break;
case 12:
Box8.Image = image3;
break;
case 13:
Box_13.Image = image3;
break;
case 14:
Box_14.Image = image3;
break;
case 15:
Box_15.Image = image3;
break;
case 16:
Box_16.Image = image3;
break;
case 17:
Box_17.Image = image3;
break;
case 18:
Box_18.Image = image3;
break;
case 19:
Box_19.Image = image3;
break;
case 20:
Box_20.Image = image3;
break;
}
switch (Patter_1[switch_Counter].Move3)
{
case 0:
Box_1.Image = image4;
break;
case 1:
Box_2.Image = image4;
break;
case 2:
Box_2.Image = image4;
break;
case 3:
Box_3.Image = image4;
break;
case 4:
Box_4.Image = image4;
break;
case 5:
Box_5.Image = image4;
break;
case 6:
Box_6.Image = image4;
break;
case 7:
Box_7.Image = image4;
break;
case 8:
Box12.Image = image4;
break;
case 9:
Box_9.Image = image4;
break;
case 10:
Box_10.Image = image4;
break;
case 11:
Box_11.Image = image4;
break;
case 12:
Box8.Image = image4;
break;
case 13:
Box_13.Image = image4;
break;
case 14:
Box_14.Image = image4;
break;
case 15:
Box_15.Image = image4;
break;
case 16:
Box_16.Image = image4;
break;
case 17:
Box_17.Image = image4;
break;
case 18:
Box_18.Image = image4;
break;
case 19:
Box_19.Image = image4;
break;
case 20:
Box_20.Image = image4;
break;
}
switch_Counter++;
if (switch_Counter >4 )
{
switch_Counter = 0;
}
}
I used the debugger, so I know the code is getting to the line
but it is not doing anything when it get there
the bebugger says:
UIKit.UIKitThreadAccessException: UIKit Consistency error: you are calling a UIKit method that can only be invoke from the UI Thread.
Is there a why around that ?

Any operation you involve in modifying UI view, you will need to do it under UI/Main thread. So put the code inside the lambda of:
InvokeOnMainThread ( () => {
// manipulate UI controls
});

Related

Memory game doesn't open all cards upon starting

I'm making a memory game in C#. It has 10 pairs of cards, total 20. It's supposed to work like this: When pressing the Start button, the game shuffles randomly the cards and reveals all cards. After 3 seconds it flips them. Then, the player has to find all pairs.
I'm having a problem with shuffling. I wrote this code. It's being executed when I press the Start button:
foreach (String icon in icons)
{
int x1 = r.Next(1, 21);
int x2 = r.Next(1, 21);
if (!num.Contains(x1))
{
num.Add(x1);
}
else
{
do
{
x1 = r.Next(1, 21);
} while (!num.Contains(x1));
num.Add(x1);
}
if (!num.Contains(x2))
{
num.Add(x2);
}
else
{
do
{
x2 = r.Next(1, 21);
} while (!num.Contains(x2));
num.Add(x2);
}
switch (x1)
{
case 1:
pictureBox1.Image = Image.FromFile(icon);
break;
case 2:
pictureBox2.Image = Image.FromFile(icon);
break;
case 3:
pictureBox3.Image = Image.FromFile(icon);
break;
case 4:
pictureBox4.Image = Image.FromFile(icon);
break;
case 5:
pictureBox5.Image = Image.FromFile(icon);
break;
case 6:
pictureBox6.Image = Image.FromFile(icon);
break;
case 7:
pictureBox7.Image = Image.FromFile(icon);
break;
case 8:
pictureBox8.Image = Image.FromFile(icon);
break;
case 9:
pictureBox9.Image = Image.FromFile(icon);
break;
case 10:
pictureBox10.Image = Image.FromFile(icon);
break;
case 11:
pictureBox11.Image = Image.FromFile(icon);
break;
case 12:
pictureBox12.Image = Image.FromFile(icon);
break;
case 13:
pictureBox13.Image = Image.FromFile(icon);
break;
case 14:
pictureBox14.Image = Image.FromFile(icon);
break;
case 15:
pictureBox15.Image = Image.FromFile(icon);
break;
case 16:
pictureBox16.Image = Image.FromFile(icon);
break;
case 17:
pictureBox17.Image = Image.FromFile(icon);
break;
case 18:
pictureBox18.Image = Image.FromFile(icon);
break;
case 19:
pictureBox19.Image = Image.FromFile(icon);
break;
case 20:
pictureBox20.Image = Image.FromFile(icon);
break;
}
switch (x2)
{
case 1:
pictureBox1.Image = Image.FromFile(icon);
break;
case 2:
pictureBox2.Image = Image.FromFile(icon);
break;
case 3:
pictureBox3.Image = Image.FromFile(icon);
break;
case 4:
pictureBox4.Image = Image.FromFile(icon);
break;
case 5:
pictureBox5.Image = Image.FromFile(icon);
break;
case 6:
pictureBox6.Image = Image.FromFile(icon);
break;
case 7:
pictureBox7.Image = Image.FromFile(icon);
break;
case 8:
pictureBox8.Image = Image.FromFile(icon);
break;
case 9:
pictureBox9.Image = Image.FromFile(icon);
break;
case 10:
pictureBox10.Image = Image.FromFile(icon);
break;
case 11:
pictureBox11.Image = Image.FromFile(icon);
break;
case 12:
pictureBox12.Image = Image.FromFile(icon);
break;
case 13:
pictureBox13.Image = Image.FromFile(icon);
break;
case 14:
pictureBox14.Image = Image.FromFile(icon);
break;
case 15:
pictureBox15.Image = Image.FromFile(icon);
break;
case 16:
pictureBox16.Image = Image.FromFile(icon);
break;
case 17:
pictureBox17.Image = Image.FromFile(icon);
break;
case 18:
pictureBox18.Image = Image.FromFile(icon);
break;
case 19:
pictureBox19.Image = Image.FromFile(icon);
break;
case 20:
pictureBox20.Image = Image.FromFile(icon);
break;
}
There is also an auxiliary list "num" and a list "icons", which contains the names of the icons' files.
List<int> num = new List<int>();
List<String> icons = new List<String>() { "agelada.png", "elefantas.png", "gata.png", "gatopardos.png", "kamilopardali.png", "liontari.png", "lykos.png", "skylos.png", "tigris.png", "zebra.png" };
This code is supposed to work like this:
~It generates two random numbers, from 1 to 20 (the game has 20 cards), and stores them into x1 and x2 respectively.
~For each icon, member of the "icons" list: If x1 isn't found in the list num, it gets added in it. If x1 is found, a new number is being generated, until generating one that isn't in "num". Then it gets stored in "num". Same for x2. This process repeats for all the member of the list "icons".
~Then, depending of the randomly generated numbers, the icon gets inserted into the respective pictureBoxes, for example, if the numbers are 6 and 17, the picture is entered into pictureBox6 and pictureBox17.
The goal of this process is to make sure that all 10 pictures are displayed exactly twice, to make pairs.
However, I have a problem. When I click on Start, not all cards are flipped (The question mark is the "back side" of the cards). It looks like this:
Picture
It's supposed to show animal pictures in all cards.
Any ideas?
In addition to the recommendations around managing the PictureBox controls in arrays, I would recommend you change the line (not shown) where you create the Random object to something like this:
Random r = new Random(10);
Setting the seed like this while you're debugging will let you predict the output, and as you step through code from one run to the next run, it's easier to follow what's happening.
That said, if you stepped through this code:
do
{
x1 = r.Next(1, 21);
} while (!num.Contains(x1));
num.Add(x1);
If you stopped the code on adding the value to your array, you would have seen the value already exists in the array, which is the opposite of the intended action. That means your loop is exiting at the wrong time.
In this case, it's because your check is the opposite logic of what it should be, you want to keep running through this loop while the value is in the array. In other words, changing both loops to look like this:
do
{
x1 = r.Next(1, 21);
} while (num.Contains(x1));
num.Add(x1);
You will end up filling up your array with 20 unique values.

Passing a variable from a method to a button click event

I want to be able to pass upperEncodedMsg into the text property of msgLabel at the bottom of my code.
namespace ProgrammingAssignmentDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Encode()
{
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToCharArray();
for (int i = 0; i < encodeArray.Length; i++)
{
char letter = (encodeArray[i]);
switch (letter)
{
case 'a':
case 'A':
encodeArray[i] = 't';
break;
case 'b':
case 'B':
encodeArray[i] = 'u';
break;
case 'c':
case 'C':
encodeArray[i] = 'v';
break;
case 'd':
case 'D':
encodeArray[i] = 'w';
break;
case 'e':
case 'E':
encodeArray[i] = 'x';
break;
case 'f':
case 'F':
encodeArray[i] = 'y';
break;
case 'g':
case 'G':
encodeArray[i] = 'z';
break;
case 'h':
case 'H':
encodeArray[i] = 'a';
break;
case 'i':
case 'I':
encodeArray[i] = 'b';
break;
case 'j':
case 'J':
encodeArray[i] = 'c';
break;
case 'k':
case 'K':
encodeArray[i] = 'd';
break;
case 'l':
case 'L':
encodeArray[i] = 'e';
break;
case 'm':
case 'M':
encodeArray[i] = 'f';
break;
case 'n':
case 'N':
encodeArray[i] = 'g';
break;
case 'o':
case 'O':
encodeArray[i] = 'h';
break;
case 'p':
case 'P':
encodeArray[i] = 'i';
break;
case 'q':
case 'Q':
encodeArray[i] = 'j';
break;
case 'r':
case 'R':
encodeArray[i] = 'k';
break;
case 's':
case 'S':
encodeArray[i] = 'l';
break;
case 't':
case 'T':
encodeArray[i] = 'm';
break;
case 'u':
case 'U':
encodeArray[i] = 'n';
break;
case 'v':
case 'V':
encodeArray[i] = 'o';
break;
case 'w':
case 'W':
encodeArray[i] = 'p';
break;
case 'x':
case 'X':
encodeArray[i] = 'q';
break;
case 'y':
case 'Y':
encodeArray[i] = 'r';
break;
case 'z':
case 'Z':
encodeArray[i] = 's';
break;
}
}
}
static string upperEncoded(char[] encodeArray, string upperEncodedMsg)
{
string encodedMsg = new string(encodeArray);
upperEncodedMsg = encodedMsg.ToUpper();
return upperEncodedMsg;
}
private void clearBtn_Click(object sender, EventArgs e)
{
messageTxt.Text = string.Empty;
msgLabel.Text = string.Empty;
processedMessageLabel.Text = "Processed Message: ";
}
private void encodeBtn_Click(object sender, EventArgs e)
{
Encode();
if (messageTxt.TextLength == 0)
{
MessageBox.Show("There is no message to Encode");
}
else
{
processedMessageLabel.Text = "Encoded Message: ";
msgLabel.Visible = true;
}
msgLabel.Text = upperEncodedMsg;
}
}
}
I tried to keep your structure in tact, there are a few things that could be done in less characters and in less complicated ways, but as you are learning the language practice is the only thing that can improve your skills :) So well done.
namespace ProgrammingAssignmentDecoder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string upperEncodedMsg = null;
public void Encode()
{
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToCharArray();
for (int i = 0; i < encodeArray.Length; i++)
{
char letter = (encodeArray[i]);
switch (letter)
{
case 'a':
case 'A':
encodeArray[i] = 't';
break;
case 'b':
case 'B':
encodeArray[i] = 'u';
break;
case 'c':
case 'C':
encodeArray[i] = 'v';
break;
case 'd':
case 'D':
encodeArray[i] = 'w';
break;
case 'e':
case 'E':
encodeArray[i] = 'x';
break;
case 'f':
case 'F':
encodeArray[i] = 'y';
break;
case 'g':
case 'G':
encodeArray[i] = 'z';
break;
case 'h':
case 'H':
encodeArray[i] = 'a';
break;
case 'i':
case 'I':
encodeArray[i] = 'b';
break;
case 'j':
case 'J':
encodeArray[i] = 'c';
break;
case 'k':
case 'K':
encodeArray[i] = 'd';
break;
case 'l':
case 'L':
encodeArray[i] = 'e';
break;
case 'm':
case 'M':
encodeArray[i] = 'f';
break;
case 'n':
case 'N':
encodeArray[i] = 'g';
break;
case 'o':
case 'O':
encodeArray[i] = 'h';
break;
case 'p':
case 'P':
encodeArray[i] = 'i';
break;
case 'q':
case 'Q':
encodeArray[i] = 'j';
break;
case 'r':
case 'R':
encodeArray[i] = 'k';
break;
case 's':
case 'S':
encodeArray[i] = 'l';
break;
case 't':
case 'T':
encodeArray[i] = 'm';
break;
case 'u':
case 'U':
encodeArray[i] = 'n';
break;
case 'v':
case 'V':
encodeArray[i] = 'o';
break;
case 'w':
case 'W':
encodeArray[i] = 'p';
break;
case 'x':
case 'X':
encodeArray[i] = 'q';
break;
case 'y':
case 'Y':
encodeArray[i] = 'r';
break;
case 'z':
case 'Z':
encodeArray[i] = 's';
break;
}
}
foreach (char eachChar in encodeArray) {
upperEncodedMsg += eachChar;
}
}
public void upperEncoded()
{
if (upperEncodedMsg != null)
{ upperEncodedMsg = upperEncodedMsg.ToUpper(); }
}
private void clearBtn_Click(object sender, EventArgs e)
{
messageTxt.Text = string.Empty;
msgLabel.Text = string.Empty;
processedMessageLabel.Text = "Processed Message: ";
}
private void encodeBtn_Click(object sender, EventArgs e)
{
if (messageTxt.TextLength == 0)
{
MessageBox.Show("There is no message to Encode");
}
else
{
Encode();
upperEncoded();
processedMessageLabel.Text = "Encoded Message: ";
msgLabel.Visible = true;
}
msgLabel.Text = upperEncodedMsg;
}
}
}
I'd go down the route of creating a mapping using a Dictionary<char, char>(see here for details on Dictionary) and simply lookup the key value in the dictionary for each char in encodeArray. It may also be better to use a StringBuilder rather than just a string to store your encoded message.
Create Mapping
//Add Dictionary at the top of the class along with upperEncodedMsg string.
private Dictionary<char, char> charMapping;
private StringBuilder upperEncodedMsg;
//Create mappings. Use uppercase values here and you won't need to use your 'upperEncoded' method. This bit can be done in your Form1 constructor.
public Form1()
{
InitializeComponent();
charMapping = new Dictionary<char, char>();
charMapping.Add('A', 'T');
charMapping.Add('B', 'U');
//more mappings...
}
You can then remove the big switch statement you have and replace it with the following:
public void Encode()
{
upperEncodedMsg = new StringBuilder();
string message = Convert.ToString(messageTxt.Text);
char[] encodeArray = message.ToUpper().ToCharArray();
for(int i = 0; i < encodeArray.Length; i++)
{
//Use the mappings created earlier to get the associated char.
char outputLetter;
charMapping.TryGetValue(encodeArray[i], out outputLetter);
//Append letter to your upperEncodedMsg StringBuilder.
upperEncodedMsg.Append(outputLetter);
}
}
In your button click, at the bottom you can then add:
private void encodeBtn_Click(object sender, EventArgs e)
{
//your code...
msgLabel.Text = upperEncodedMsg.ToString();
}
The main benefit of doing it this way is that your mappings can be made available in other parts of your code. It also makes it easier to maintain and your mappings are loaded as soon as the form is, rather than waiting to call your encode method. A lookup of a Dictionary is also likely to be quicker than a large switch statement.

Continuous input to switch case until i press exit c#

I am trying to create a program where the application should never exit until I press option '6'. Below is my sample program: Right now, if I press any option, it executes the corresponding method and console window closes (exits the application) after it is done executing. I want the console to wait for the next option to be entered.
Console.WriteLine(#"Select one of the following option:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
return;
case 2:
DoDrivers(reqObj);
return;
case 3:
DoOS(reqObj);
return;
case 4:
DoPackages(reqObj);
return;
case 5:
DoAll(reqObj);
return;
case 6:
Environment.Exit(0);
return;
default:
DoAll(reqObj);
return;
}
You can just place your code into a loop, and change the return to break statements:
while(true) // Loop forever
{
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
break; // Break, don't return
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
break;
}
}
If you wish to prompt for input each iteration, you can move the prompt into the loop, as well.
using System;
namespace LoopUntilSelectionTester
{
class Program
{
static void Main()
{
string key;
while((key = Console.ReadKey().KeyChar.ToString()) != "6")
{
int keyValue;
int.TryParse(key, out keyValue);
ProcessInput(keyValue);
}
}
private static void ProcessInput(int keyValue)
{
switch (keyValue)
{
case 1:
DoApps(reqObj);
break;
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
}
}
}
}
First, what I'd do is make it into a method:
public void AskForInput()
{
Console.WriteLine(#"Select one of the following options:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
return;
case 2:
DoDrivers(reqObj);
return;
case 3:
DoOS(reqObj);
return;
case 4:
DoPackages(reqObj);
return;
case 5:
DoAll(reqObj);
return;
case 6:
Environment.Exit(0);
return;
default:
DoAll(reqObj);
return;
}
}
and then just use it like this:
for (;;) AskForInput();
Just wrap the whole thing in a while statement.
while (true)
{
Console.WriteLine(#"Select one of the following option:
1-Write Apps
2-Write Drivers
3-Write OS
4-Write Packages
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
switch (selectionKey)
{
case 1:
DoApps(reqObj);
break;
case 2:
DoDrivers(reqObj);
break;
case 3:
DoOS(reqObj);
break;
case 4:
DoPackages(reqObj);
break;
case 5:
DoAll(reqObj);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
break;
}
}
This worked for me:
Console.WriteLine(#"Select one of the following option:
1-Write Applications XML
2-Write Drivers XML
3-Write Operating Systems XML
4-Write Packages XML
5-All the above
6-Exit");
string strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
while (true)
{
switch (selectionKey)
{
case 1:
DoApplications(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 2:
DoDrivers(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 3:
DoOperatingSystems(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 4:
DoPackages(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 5:
DoAll(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
case 6:
Environment.Exit(0);
break;
default:
DoAll(reqObj);
strReadKey = Console.ReadKey().KeyChar.ToString();
int.TryParse(strReadKey, out selectionKey);
break;
}
}

Populate PieChart based on SQL Server stored procedure

I have the following stored procedure that I have to get data from:
EXECUTE [dbo].[StationHealthStatusSummary2]
#LineId varchar(100), // 5,9,10
#MeasurementDt datetime, //2012/06/06
#Ntotal Int output,
#N0 int output,
#N1 int output,
#N2 int output,
#N3 int output,
#N4 int output,
#N5 int output,
#N6 int output,
#N7 int output,
#N8 int output,
#N9 int output,
#N10 int output,
#N11 int output,
#N12 int output,
#N13 int output,
#N14 int output,
#N15 int output,
#N16 int output
GO
Now I can send parameters to LineID and Measurement date as follows:
SqlConnection sql = new SqlConnection(#"Data Source=(local)\SQLEXPRESS;Initial Catalog=iComs;Persist Security Info=True;User ID=sa;Password=Password);
SqlCommand getData = new SqlCommand("StationHealthStatusSummary2", sql);
SqlDataAdapter da = new SqlDataAdapter(getData);
getData.CommandType = CommandType.StoredProcedure;
getData.Parameters.Add(new SqlParameter("#LineId", Lines));
getData.Parameters.Add(new SqlParameter("#MeasurementDt", date1));
SqlParameter ParamaterNtotal = new SqlParameter();
ParamaterNtotal.ParameterName = "#Ntotal";
ParamaterNtotal.SqlDbType = SqlDbType.Int;
ParamaterNtotal.Direction = ParameterDirection.Output;
getData.Parameters.Add(ParamaterNtotal);
sql.Open();
getData.ExecuteNonQuery();
Now I can get the value of NTotal and assign it to a Teechart (PieSlice),
but how do I get the value for #N0..#N16?
I've got some code that might give you some idea of what I'm trying to accomplish.
int NTotal = int.Parse(getData.Parameters["#Ntotal"].Value.ToString());
if (GetVariantVariableI(getData.Parameters[0].Value) = 0)
{
for (c = 1; c <= 18; c++)
{
Nvl = GetVariantVariableI(getData.Parameters[2+c].Value);
switch(c)
{
case 1:
NTotal = Nvl;
break;
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
cstatus = c-2;
NPieValues[cstatus] = Nvl;
break;
}
string SliceName;
int NUsed;
NUsed = 0;
for(i=0;i<=16;i++)
{
NUsed = NUsed + NPieValues[i];
}
if (NUsed < NTotal)
{
Chart1.Series[0].Add(NTotal - NUsed);
slice1.Title = "Not Connected";
slice1.Add();
slice1.Color = System.Drawing.Color.Silver;
}
if (NUsed > NTotal)
{
NPieValues[7] = NPieValues[7]-(NUsed-NTotal);
}
for (i=0;i<=16;i++)
{
if (NPieValues[i]>0)
{
switch(i)
{
case 0: SliceName ="Green";
break;
case 1: SliceName ="Yellow";
break;
case 2: SliceName ="Orange";
break;
case 3: SliceName ="Red";
break;
case 4: SliceName ="Broken Rail";
break;
case 5: SliceName ="Buckling Rail";
break;
case 6: SliceName ="Maintenance required";
break;
case 7: SliceName ="Station(s) Off";
break;
case 8: SliceName ="Rail Differential kN";
break;
case 9: SliceName ="Left Rail Sensor Faulty";
break;
case 10: SliceName ="Right Rail Sensor Faulty";
break;
case 11: SliceName ="Temperature Rail Sensor Faulty";
break;
case 12: SliceName ="Calibration Required";
break;
case 13: SliceName ="Station Vandalised";
break;
case 14: SliceName ="Station uninstalled";
break;
case 15: SliceName ="Gauges removed for Maintenance";
break;
case 16: SliceName ="No GSM Coverage";
break;
default:
SliceName ="?";
}
switch(i)
{
case 0: clr = "System.Drawing.Color.Green";
break;
case 1:clr = "System.Drawing.Color.Yellow";
break;
case 2:clr = "System.Drawing.Color.Orrange";
break;
case 3:clr = "System.Drawing.Color.Red";
break;
case 4:
case 5:
case 8:clr = "System.Drawing.Color.Purple";
break;
case 6:clr = "System.Drawing.Color.Black";
break;
case 7:clr = "System.Drawing.Color.Gray";
break;;
case 9:clr = "System.Drawing.ColorTranslator.FromHtml('#E0671F')";
break;
case 10:clr = "System.Drawing.ColorTranslator.FromHtml('#BA4EC2')";
break;
case 11:clr = "System.Drawing.ColorTranslator.FromHtml('#FF8000')";
break;
case 12:clr = "System.Drawing.ColorTranslator.FromHtml('#BF4093')";
break;
case 13:clr = "System.Drawing.Color.SkyBlue";
break;
case 14:clr = "System.Drawing.Color.Aqua";
break;
case 15:clr = "System.Drawing.ColorTranslator.FromHtml('#BFBFFF')";
break;
case 16:clr = "System.Drawing.Color.MedGray";
break;
default : clr = "System.Drawing.Color.White";
break;
}
slice1.Add(NPieValues[i],SliceName,clr);
}
}
}
}
}
Now after it got all those values it has to populate the piechart.
Please, any help will be highly appreciated and please tell me if I'm being too vague.
Thanks
You have added #Ntotal as an Output parameter, why didn't you add all the others?
SqlParameter n = new SqlParameter();
n.ParameterName = "#N0";
n.SqlDbType = SqlDbType.Int;
n.Direction = ParameterDirection.Output;
getData.Parameters.Add(n);
Then you can retrieve the output values the same way you do with #Ntotal. Also, you can retrieve the values by name instead of looping through them, if you prefer.
Thanks. I Figured it out...
after i send the parameters as follows:
SqlParameter Paramater0 = new SqlParameter();
Paramater0.ParameterName = "#N0";
Paramater0.SqlDbType = SqlDbType.Int;
Paramater0.Direction = ParameterDirection.Output;
getData.Parameters.Add(Paramater0);
I then get the value and declare it to a var as follows:
int N0 = int.Parse(getData.Parameters["#N0"].Value.ToString());
and finally declare that int to a slice on the chart as follows:
slice1.Add(N0, "Green", System.Drawing.Color.Green);
Thanks for your help #mgnoonan :)

C#: How can I combine a switch with an if-statement?

I need to combine a switch with an if-statement.
How can I do that? I want to do something like this:
switch (periodtype)
{
if(starttime>endtime)
{
;
}
else
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
What's wrong with:
if(starttime<=endtime)
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
if(starttime>endtime)
{
// Stuff
}
else
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
Like that?
if(starttime>endtime)
{
;
}
else
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
if(starttime <= endtime)
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}
Hmm way too late with answer
isnt this easier:
if (starttime <= endtime)
{
switch (periodtype)
{
case 0: nextRunTime = nextRunTime.AddHours(period); break;
case 1: nextRunTime = nextRunTime.AddMinutes(period); break;
case 2: nextRunTime = nextRunTime.AddSeconds(period); break;
}
}

Categories

Resources