'DING' On TabStop in C#/WPF - c#

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.

Related

What are some reasons that a PictureBox or Label might not display their respective image or text in the situation described below?

As you can see in the screenshots below, I have made a PictureBox with a Label(that is smaller than the PictureBox) overlapping it. I have set the PictureBox to containing the green rectangle image that is visible in the first screenshot, and I have set the Label to having a "LimeGreen" BackColor and a "ControlText" text color(which is just black).
Both objects' "Visible" properties are set to "false" by default, but over the course of the program, these properties are both changed to "true," which I know for certain from debugging. The second screenshot is taken from after these properties are changed to "true" in the program.
Another property of these objects that is changed over the course of the program is the main content: for the Label, the text changes from being nonexistent to saying "75 %", and the PictureBox's image changes to a rectangle 75% filled by green. I know that the image and text are set correctly from debugging.
However, as is visible in the second screenshot, what I intended to happen(and what I think should happen as I have determined from debugging) does not take place. Neither the image nor the text appear on-screen at any point during the runtime of the program. However, what you can see in the second screenshot is the "FixedSingle" border that I put on the Label. This was only visible during the span of time that the code indicated that the Label and PictureBox should be visible, so I assume that the Label and PictureBox are actually set to Visible here, but for some reason, their text and image are not appearing. The image that I set the PictureBox to display by default doesn't ever display either.
My first thought after discovering that the Visible property and the Image and Text assignments were not faulty was that an object might be overlapping the Label and PictureBox. However, I discovered that this issue did not vanish when I set both objects to be in front of the object they both share space with.
I have not found this issue with other PictureBox's or Label's, all of which I got functioning using a near-identical process to what I am using in this case. The only notable difference between the cases is that I am using System.Threading.Thread.Sleep in the nonfunctioning PictureBox and Label. Does this function somehow mess with image and text loading? Might there be a reason other than Visible being false, the Text and Image being set incorrectly, and objects overlapping that the image and text would not load in this case?
Here are the screenshots:
(yes, the placeholder images are silly. The border I talk about above is the black rectangle in the middle.)
Here is the code that seems to be giving me trouble:
switch (char1ChosenSkill.numberOfTargets) //this switch block can be ignored; for now it always goes to case 1
{
case 1:
Side2HPBoxList[BasicProcess.side2.IndexOf(targetList[0])].Visible = true; //sets the PictureBox in question's Visible to true
Thread.Sleep(500); //waits half a second(I think)
targetList[0] = char1ChosenSkill.effect(targetList[0]); //this can be ignored since it does not effect the PictureBox or Label
Side2HPBoxList[BasicProcess.side2.IndexOf(targetList[0])].Image = UpdateHpBar(targetList[0], BasicProcess.side2); //runs a function that I will post and document below this block of code
Thread.Sleep(2000); //waits 2 seconds(I think)
Side2HPBoxList[BasicProcess.side2.IndexOf(targetList[0])].Visible = false; //sets the PictureBox in question's Visible back to false
Menu2Background.Visible = (char2 != null); //this can be ignored since it does not effect the PictureBox or Label
break;
}
The UpdateHpBar function:
private Image UpdateHpBar(BasicCharacter target, List<BasicCharacter> side)
{
PictureBox HPBox = side2HPbox1; //since I got an error claiming that HPBox isn't assigned before, I set it to a default value before changing it(note that the default value is the PictureBox in question)
//ints generated for a switch block later on
int NewHP = target.HP.value * 100;
int MaxHP = target.HP.max;
//this block assigns HPBox to a PictureBox based on UpdateHpBar's arguments
foreach (BasicCharacter item in side)
{
if (item == target)
{
switch (side.IndexOf(item))
{
case 0:
HPBox = side2HPbox1;
break;
case 1:
HPBox = side2HPbox2;
break;
case 2:
HPBox = side2HPbox3;
break;
case 3:
HPBox = side2HPbox4;
break;
default:
HPBox = side2HPbox1;
break;
}
}
}
int percHP = (NewHP / MaxHP); //calculates a new int based on the ints generated above
//this switch block assigns an Image to HPBox depending on the value of percHP
switch (percHP)
{
case 0:
HPBox.Image = Properties.Resources.EnemyHPBar0;
break;
case 1:
HPBox.Image = Properties.Resources.EnemyHPBar1;
break;
case 2:
HPBox.Image = Properties.Resources.EnemyHPBar2;
break;
case 3:
HPBox.Image = Properties.Resources.EnemyHPBar3;
break;
case 4:
HPBox.Image = Properties.Resources.EnemyHPBar4;
break;
case 5:
HPBox.Image = Properties.Resources.EnemyHPBar5;
break;
case 6:
HPBox.Image = Properties.Resources.EnemyHPBar6;
break;
case 7:
HPBox.Image = Properties.Resources.EnemyHPBar7;
break;
case 8:
HPBox.Image = Properties.Resources.EnemyHPBar8;
break;
case 9:
HPBox.Image = Properties.Resources.EnemyHPBar9;
break;
case 10:
HPBox.Image = Properties.Resources.EnemyHPBar10;
break;
case 11:
HPBox.Image = Properties.Resources.EnemyHPBar11;
break;
case 12:
HPBox.Image = Properties.Resources.EnemyHPBar12;
break;
case 13:
HPBox.Image = Properties.Resources.EnemyHPBar13;
break;
case 14:
HPBox.Image = Properties.Resources.EnemyHPBar14;
break;
case 15:
HPBox.Image = Properties.Resources.EnemyHPBar15;
break;
case 16:
HPBox.Image = Properties.Resources.EnemyHPBar16;
break;
case 17:
HPBox.Image = Properties.Resources.EnemyHPBar17;
break;
case 18:
HPBox.Image = Properties.Resources.EnemyHPBar18;
break;
case 19:
HPBox.Image = Properties.Resources.EnemyHPBar19;
break;
case 20:
HPBox.Image = Properties.Resources.EnemyHPBar20;
break;
case 21:
HPBox.Image = Properties.Resources.EnemyHPBar21;
break;
case 22:
HPBox.Image = Properties.Resources.EnemyHPBar22;
break;
case 23:
HPBox.Image = Properties.Resources.EnemyHPBar23;
break;
case 24:
HPBox.Image = Properties.Resources.EnemyHPBar24;
break;
case 25:
HPBox.Image = Properties.Resources.EnemyHPBar25;
break;
case 26:
HPBox.Image = Properties.Resources.EnemyHPBar26;
break;
case 27:
HPBox.Image = Properties.Resources.EnemyHPBar27;
break;
case 28:
HPBox.Image = Properties.Resources.EnemyHPBar28;
break;
case 29:
HPBox.Image = Properties.Resources.EnemyHPBar29;
break;
case 30:
HPBox.Image = Properties.Resources.EnemyHPBar30;
break;
case 31:
HPBox.Image = Properties.Resources.EnemyHPBar31;
break;
case 32:
HPBox.Image = Properties.Resources.EnemyHPBar32;
break;
case 33:
HPBox.Image = Properties.Resources.EnemyHPBar33;
break;
case 34:
HPBox.Image = Properties.Resources.EnemyHPBar34;
break;
case 35:
HPBox.Image = Properties.Resources.EnemyHPBar35;
break;
case 36:
HPBox.Image = Properties.Resources.EnemyHPBar36;
break;
case 37:
HPBox.Image = Properties.Resources.EnemyHPBar37;
break;
case 38:
HPBox.Image = Properties.Resources.EnemyHPBar38;
break;
case 39:
HPBox.Image = Properties.Resources.EnemyHPBar39;
break;
case 40:
HPBox.Image = Properties.Resources.EnemyHPBar40;
break;
case 41:
HPBox.Image = Properties.Resources.EnemyHPBar41;
break;
case 42:
HPBox.Image = Properties.Resources.EnemyHPBar42;
break;
case 43:
HPBox.Image = Properties.Resources.EnemyHPBar43;
break;
case 44:
HPBox.Image = Properties.Resources.EnemyHPBar44;
break;
case 45:
HPBox.Image = Properties.Resources.EnemyHPBar45;
break;
case 46:
HPBox.Image = Properties.Resources.EnemyHPBar46;
break;
case 47:
HPBox.Image = Properties.Resources.EnemyHPBar47;
break;
case 48:
HPBox.Image = Properties.Resources.EnemyHPBar48;
break;
case 49:
HPBox.Image = Properties.Resources.EnemyHPBar49;
break;
case 50:
HPBox.Image = Properties.Resources.EnemyHPBar50;
break;
case 51:
HPBox.Image = Properties.Resources.EnemyHPBar51;
break;
case 52:
HPBox.Image = Properties.Resources.EnemyHPBar52;
break;
case 53:
HPBox.Image = Properties.Resources.EnemyHPBar53;
break;
case 54:
HPBox.Image = Properties.Resources.EnemyHPBar54;
break;
case 55:
HPBox.Image = Properties.Resources.EnemyHPBar55;
break;
case 56:
HPBox.Image = Properties.Resources.EnemyHPBar56;
break;
case 57:
HPBox.Image = Properties.Resources.EnemyHPBar57;
break;
case 58:
HPBox.Image = Properties.Resources.EnemyHPBar58;
break;
case 59:
HPBox.Image = Properties.Resources.EnemyHPBar59;
break;
case 60:
HPBox.Image = Properties.Resources.EnemyHPBar60;
break;
case 61:
HPBox.Image = Properties.Resources.EnemyHPBar61;
break;
case 62:
HPBox.Image = Properties.Resources.EnemyHPBar62;
break;
case 63:
HPBox.Image = Properties.Resources.EnemyHPBar63;
break;
case 64:
HPBox.Image = Properties.Resources.EnemyHPBar64;
break;
case 65:
HPBox.Image = Properties.Resources.EnemyHPBar65;
break;
case 66:
HPBox.Image = Properties.Resources.EnemyHPBar66;
break;
case 67:
HPBox.Image = Properties.Resources.EnemyHPBar67;
break;
case 68:
HPBox.Image = Properties.Resources.EnemyHPBar68;
break;
case 69:
HPBox.Image = Properties.Resources.EnemyHPBar69;
break;
case 70:
HPBox.Image = Properties.Resources.EnemyHPBar70;
break;
case 71:
HPBox.Image = Properties.Resources.EnemyHPBar71;
break;
case 72:
HPBox.Image = Properties.Resources.EnemyHPBar72;
break;
case 73:
HPBox.Image = Properties.Resources.EnemyHPBar73;
break;
case 74:
HPBox.Image = Properties.Resources.EnemyHPBar74;
break;
case 75:
HPBox.Image = Properties.Resources.EnemyHPBar75;
break;
case 76:
HPBox.Image = Properties.Resources.EnemyHPBar76;
break;
case 77:
HPBox.Image = Properties.Resources.EnemyHPBar77;
break;
case 78:
HPBox.Image = Properties.Resources.EnemyHPBar78;
break;
case 79:
HPBox.Image = Properties.Resources.EnemyHPBar79;
break;
case 80:
HPBox.Image = Properties.Resources.EnemyHPBar80;
break;
case 81:
HPBox.Image = Properties.Resources.EnemyHPBar81;
break;
case 82:
HPBox.Image = Properties.Resources.EnemyHPBar82;
break;
case 83:
HPBox.Image = Properties.Resources.EnemyHPBar83;
break;
case 84:
HPBox.Image = Properties.Resources.EnemyHPBar84;
break;
case 85:
HPBox.Image = Properties.Resources.EnemyHPBar85;
break;
case 86:
HPBox.Image = Properties.Resources.EnemyHPBar86;
break;
case 87:
HPBox.Image = Properties.Resources.EnemyHPBar87;
break;
case 88:
HPBox.Image = Properties.Resources.EnemyHPBar88;
break;
case 89:
HPBox.Image = Properties.Resources.EnemyHPBar89;
break;
case 90:
HPBox.Image = Properties.Resources.EnemyHPBar90;
break;
case 91:
HPBox.Image = Properties.Resources.EnemyHPBar91;
break;
case 92:
HPBox.Image = Properties.Resources.EnemyHPBar92;
break;
case 93:
HPBox.Image = Properties.Resources.EnemyHPBar93;
break;
case 94:
HPBox.Image = Properties.Resources.EnemyHPBar94;
break;
case 95:
HPBox.Image = Properties.Resources.EnemyHPBar95;
break;
case 96:
HPBox.Image = Properties.Resources.EnemyHPBar96;
break;
case 97:
HPBox.Image = Properties.Resources.EnemyHPBar97;
break;
case 98:
HPBox.Image = Properties.Resources.EnemyHPBar98;
break;
case 99:
HPBox.Image = Properties.Resources.EnemyHPBar99;
break;
case 100:
HPBox.Image = Properties.Resources.EnemyHPBar100;
break;
default:
HPBox.Image = Properties.Resources.EnemyHPBar100;
break;
}
return HPBox.Image; //returns the image assigned to HPBox(this means in the first code block in this post, this function is used to assign an image to the PictureBox in question, in this case "Properties.Resources.EnemyHPBar75")
}
Also note that I have a visibility change function set up to make the Label visible when the PictureBox does:
private void side2HPBox1_ChangeVisiblity(object sender, EventArgs e)
{
Side2HPText1.Visible = side2HPbox1.Visible;
side2HPbox1DisplayNum = (BasicProcess.side2[0].HP.value*100) / (BasicProcess.side2[0].HP.max); //calculates the number displayed in the Label's text
Side2HPText1.Text = side2HPbox1DisplayNum + " %"; //sets the label's text
}
You stated:
The only notable difference between the cases is that I am using
System.Threading.Thread.Sleep in the nonfunctioning PictureBox and
Label. Does this function somehow mess with image and text loading?
That's exactly the problem. You set visibility to true, sleep, then set visibility back to false. When you Sleep() on the user interface thread, it BLOCKS execution preventing the UI from updating/repainting. You only see that last action, which was to set it back to false. One way to fix this is to use async/await and Task.Delay.
This is essentially what you are currently doing (and is incorrect):
// incorrect
private void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
System.Threading.Thread.Sleep(2000);
label1.Visible = false;
}
Here is the same code but using async/await with Task.Delay():
// correct
private async void button1_Click(object sender, EventArgs e)
{
label1.Visible = true;
await Task.Delay(2000);
label1.Visible = false;
}

Can't show all nodes of TreeView

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.

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;
}
}

Reading numpad symbols under Mono

I am developing a console application, that should listen for digits from a numpad keyboard in both num lock states - on and off. The application is running on Raspberry Pi with Arch Linux and Mono. Since I did not found a way, that is compiling under Mono, to permanently turn numlock on, I am using the following method to convert num pad commands to digits:
private string ReadNumPadSymbol(ConsoleKeyInfo keyInfo)
{
char editedSymbol;
switch (keyInfo.Key)
{
case ConsoleKey.Insert:
editedSymbol = '0';
break;
case ConsoleKey.End:
editedSymbol = '1';
break;
case ConsoleKey.DownArrow:
editedSymbol = '2';
break;
case ConsoleKey.PageDown:
editedSymbol = '3';
break;
case ConsoleKey.LeftArrow:
editedSymbol = '4';
break;
case ConsoleKey.Clear:
editedSymbol = '5';
break;
case ConsoleKey.RightArrow:
editedSymbol = '6';
break;
case ConsoleKey.Home:
editedSymbol = '7';
break;
case ConsoleKey.UpArrow:
editedSymbol = '8';
break;
case ConsoleKey.PageUp:
editedSymbol = '9';
break;
default:
return String.Empty;
}
return editedSymbol.ToString();
}
It works as expected under Windows, but under Linux, the method returns empty string, when the "5" button is pressed. For some reason it does not enters the ConsoleKey.Clear case. Is there a fix for this?
Thanks!

Categories