Can't show all nodes of TreeView - c#

I have the following Code down here, but when I try to add a child to parent it causes an error when parentID is bigger than 4.
public void LoadNodes()
{
ConnectionShorten("TreeViewTable");
int H = MyNodes.Tables["TreeViewTable"].Rows.Count;
for (int i = 0; i < H; i++)
{
int PID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("ParentID");
string Name = MyNodes.Tables["TreeViewTable"].Rows[i].Field<string>("RootName");
int Level = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("Level");
int UID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("UID");
switch (Level)
{
case 0:
treeView1.Nodes.Add(Name.ToString());
break;
case 1:
treeView1.Nodes[0].Nodes.Add(Name.ToString());
break;
case 2:
switch (PID)
{
case 1:
treeView1.Nodes[0].Nodes[0].Nodes.Add(Name.ToString());
break;
case 2:
treeView1.Nodes[0].Nodes[1].Nodes.Add(Name.ToString());
break;
case 3:
treeView1.Nodes[0].Nodes[2].Nodes.Add(Name.ToString());
break;
case 4:
treeView1.Nodes[0].Nodes[3].Nodes.Add(Name.ToString());
break;
case 5:
treeView1.Nodes[0].Nodes[4].Nodes.Add(Name.ToString());
break;
case 6:
treeView1.Nodes[0].Nodes[5].Nodes.Add(Name.ToString());
break;
case 7:
treeView1.Nodes[0].Nodes[6].Nodes.Add(Name.ToString());
break;
case 8:
treeView1.Nodes[0].Nodes[7].Nodes.Add(Name.ToString());
break;
case 9:
treeView1.Nodes[0].Nodes[8].Nodes.Add(Name.ToString());
break;
case 10:
treeView1.Nodes[0].Nodes[9].Nodes.Add(Name.ToString());
break;
case 11:
treeView1.Nodes[0].Nodes[10].Nodes.Add(Name.ToString());
break;
default:
break;
}
break;
case 3:
break;
default:
break;
}
}
}
It does load 1 main node and 10 parents, then when it comes to child it says a negative index, but I'm sure the parent does exist!
Also how can I make more levels with no difficulty like this, because I think it's very difficult to make levels > 3.

You can close it, I just found that problem and solved it.
It was because there is a jump on ID on the DB. So I try to add child before I even add the parent. my program is read by ID so thats it ... sorry for this.

Related

If-statement into the switch-method with boolean

Problem: How to write this if-codeblock into a switch case? I have only a problem with the boolean.
int Note = 0;
Console.WriteLine("What is your note?: ");
Note = Convert.ToInt32(Console.ReadLine());
if ((Note < 1) || (Note > 6))
{
Console.WriteLine("Your input is wrong!");
}
else
{
if (Note <= 4)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
}
I tried to write this codeblock into a switch-method.
int Note = 0;
Console.WriteLine("What ist your note?: ");
Note = Convert.ToInt32(Console.ReadLine());
switch (Note)
{
case Note < 1 || Note > 6:
Console.WriteLine("Your input is wrong!");
break;
case Note <= 4:
Console.WriteLine("Passed");
break;
}
Error Message: I can't convert the string into int.
C#7+ supports range-based switching:
switch (Note)
{
case int n when n< 1 || Note > 6:
Console.WriteLine("Your input is wrong!");
break;
case int n when n <= 4:
Console.WriteLine("Passed");
break;
}
You can do it like that if you really need a switch
switch (Note)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Passed");
break;
case 5:
case 6:
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Your input is wrong!");
break;
}
This is not where switch statements are for. You should keep using if.
I will proof that very easily, using a switch, as you asked:
switch (Note)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Passed");
break;
case 5:
case 6:
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Your input is wrong!");
break;
}
This is just way too verbatim. You don't want to write down every value. if is more appropriate.

Switch statement using text box within forms

I am building a form that takes an input number from a text box and then will take the number that was input, and display the roman numeral equivalent in another text box.
My Form:
private void convertButton_Click(object sender, EventArgs e)
{
int numberInput;
switch (numberInput)
This is where I keep getting an error code. The "switch (numberInput)" is seen as and unassigned local variable. How do I assign it so that it will be able to access all of the case integers?
{
case 1:
outputTextBox.Text = "I";
break;
case 2:
outputTextBox.Text = "II";
break;
case 3:
outputTextBox.Text = "III";
break;
case 4:
outputTextBox.Text = "IV";
break;
case 5:
outputTextBox.Text = "V";
break;
case 6:
outputTextBox.Text = "VI";
break;
case 7:
outputTextBox.Text = "VII";
break;
case 8:
outputTextBox.Text = "VIII";
break;
case 9:
outputTextBox.Text = "IX";
break;
case 10:
outputTextBox.Text = "X";
break;
default:
MessageBox.Show("Please enter and number between 1 and 10. Thank you!");
break;
}
Cause your variable is not assigned yet int numberInput; and so the error. You said input is coming from a TextBox, in that case do like below assuming textbox1 is your TextBox control instance name
int numberInput = Convert.ToInt32(this.textbox1.Text.Trim());
Convert.ToInt32 may throw an exception if the parsing is unsuccessful. Another method is to Int.Parse:
int numberInput = int.Parse(textbox1.Text.Trim());
or better yet
int numberInput;
if(int.TryParse(textbox1.Text.Trim(), out numberInput))
{
switch (numberInput)
...
}

Write random text into Textbox

I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
Console.;
break;
case 1:
Console.WriteLine.TextBox("Yes");
break;
case 2:
Console.WriteLine.TextBox("No");
break;
case 3:
Console.WriteLine.TextBox("Maybe");
break;
case 4:
Console.WriteLine.TextBox("You could say that");
break;
case 5:
Console.WriteLine.TextBox("Most certain");
break;
case 6:
Console.WriteLine.TextBox("Dont even try");
break;
case 7:
Console.WriteLine.TextBox("Full steam ahead");
break;
}
}
Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...
Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
theTextBox.Text = "";
break;
case 1:
theTextBox.Text = "Yes";
break;
case 2:
theTextBox.Text = "No";
break;
case 3:
theTextBox.Text = "Maybe";
break;
case 4:
theTextBox.Text = "You could say that";
break;
case 5:
theTextBox.Text = "Most certain";
break;
case 6:
theTextBox.Text = "Dont even try";
break;
case 7:
theTextBox.Text = "Full steam ahead";
break;
}
}

'DING' On TabStop in C#/WPF

I currently have a WPF window with numerous textboxes/buttons. Currently when trying to use the TAB key to navigate between the objects you hear a 'DING' and the focus is not changed to the next object in the TabIndex.
Here is what the window I have looks like with the TabIndex numbers displayed.
All the objects have TabStop set to True.
Not the prettiest solution but it works.
On Form Initialization call
this.KeyUp += new System.Windows.Forms.KeyEventHandler(KeyEvent);
Then utilize this function to grab the TAB key and process the focus.
private void KeyEvent(object sender, KeyEventArgs e) //Keyup Event
{
if (e.KeyCode == Keys.Tab)
{
++iFocusCount;
}
else if (e.KeyCode == Keys.Tab && e.KeyCode == Keys.Shift)
{
--iFocusCount;
}
switch (iFocusCount)
{
case 0:
contactBox.Focus();
break;
case 1:
incidentBox.Focus();
break;
case 2:
actionsListBox.Focus();
break;
case 3:
profilesListBox.Focus();
break;
case 4:
currentLatchBox.Focus();
break;
case 5:
daysBox.Focus();
break;
case 6:
calculateDateButton.Focus();
break;
case 7:
copyButton.Focus();
break;
case 8:
notesTextBox.Focus();
break;
case 9:
keycodeBox.Focus();
break;
case 10:
xnaBox.Focus();
break;
case 11:
generateTamButton.Focus();
break;
case 12:
generateNotesButton.Focus();
break;
case 13:
sendEmailButton.Focus();
break;
case 14:
saveButton.Focus();
break;
case 15:
clearLabel.Focus();
break;
case 16:
iFocusCount = 0;
contactBox.Focus();
break;
}
}
This still produces the "DING" but the focus changes which is what I wanted in the first place.

image not displaying

I hope you can help me! I am trying to make a game similiar to candyland. I want the die to spin when the user clicks the button. A random number is chosen and based on that number, the dice displays the image for that number. That works! Then, I want our user to be able to move forward on our board- based on the spot that they're on, it adds whatever they spinned and the image on that spot becomes visible. When in debug mode, everything works perfectly but for some reason, the pawn never moves! Can you please tell me why. I am attaching my code below. Thank you so much!
protected void btnSpin_Click(object sender, EventArgs e)
{
Random randomNumber = new Random();
int x = randomNumber.Next(1, 6);
switch (x)
{
case 1:
//imgDie.ImageUrl = "~/Images/dice1.jpg";
Session["Die"] = "~/Images/dice1.jpg";
break;
case 2:
Session["Die"] = "~/Images/dice2.jpg";
break;
case 3:
Session["Die"] = "~/Images/dice3.jpg";
break;
case 4:
Session["Die"] = "~/Images/dice4.jpg";
break;
case 5:
Session["Die"] = "~/Images/dice5.jpg";
break;
case 6:
Session["Die"] = "~/Images/dice6.jpg";
break;
}
imgDie.ImageUrl = (string)Session["Die"];
place = place + x;
switch (place)
{
case 2:
img2.Visible = true;
img2.ImageUrl = (string)Session["Imagesrc"];
break;
case 3:
img3.Visible = true;
img3.ImageUrl = (string)Session["Imagesrc"];
break;
case 4:
img4.Visible = true;
img4.ImageUrl = (string)Session["Imagesrc"];
break;
case 5:
img5.Visible = true;
img5.ImageUrl = (string)Session["Imagesrc"];
break;
case 6:
img6.Visible = true;
img6.ImageUrl = (string)Session["Imagesrc"];
break;
case 7:
img7.Visible = true;
img7.ImageUrl = (string)Session["Imagesrc"];
break;
case 8:
img8.ImageUrl = (string)Session["Imagesrc"];
img8.Visible = true;
break;
my guess is your 'place' variable is a member field and it's being reinitialized with each page construction. chnge your place variable to be viewstate or session state like your other stuff.

Categories