How to select a TabItem based off of it's Header - c#

In my program I have a tabItem that gets selected when a TreeViewItem with an equivalent header is selected.
This is what I currently have (It works):
(parent_TreeViewItem.Items.Contains(SelectedItem))
{
tabControl1.SelectedItem = tabControl1.Items //Changes tab according to TreeView
.OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == SelectedItem.Header.ToString());
}
The difference with what I'm doing this time is that the tabItem's header that I'm selecting is composed of a string and an integer.
For example: The TreeViewItem selected will always have a header named "Arrival", but the tabItem's header will have a form like "Arrival" + integer. The integer value will come from the parent node's header.
For this process I'm thinking that I'll first need to get the header value of the parent node, since it contains that integer value I need. Then I'll need to modify my code above in someway to query for a node with a header like, "Arrival" + parentHeader.
How would I do something like this?
Thank you.
UPDATE
My current code, using #varocarbas's answer. I am using the first version of the answer that involved setting the integer curNumber to the value of the parent's header. The code compiles but does not do anything when the "Arrival" node is clicked on.
if (parent_TreeViewItem.Items.Contains(SelectedItem.Parent)) //Location - Actions tabs
{
TreeViewItem parentItem = (TreeViewItem)SelectedItem.Parent;
int curNumber = getNumber(parentItem.Header.ToString());
tabControl1.SelectedItem = tabControl1.Items //Changes tab according to TreeView
.OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == SelectedItem.Header.ToString() + curNumber.ToString());
}
public static int getNumber(string parentNodeHeader)
{
int curNumber = 0;
curNumber = Convert.ToInt32(parentNodeHeader);
return curNumber;
}
UPDATE 2: Because the "Arrival" node is the grandchild of the node I was using as a parent I have changed the if statement in my first line to:
if (parent_TreeViewItem.Items.Contains(SelectedItem.Parent))

Firstly, you have to get the parent node and the number contained in its header:
TreeViewItem parentItem = (TreeViewItem)selectedItem.Parent;
int curNumber = getNumber(parentItem.Header.ToString());
getNumber is a function to retrieve the number from its exact location in the parent node header. You have to tell more about that in order to write a proper function; for the time being, just the basics (it extracts all the numbers in the input string):
private int getNumber(string parentNodeHeader)
{
int curNumber = 0;
//Required string-analysis actions
//Sample functionality: extract all the numbers in the given string
string outString = "";
int count = -1;
do
{
count = count + 1;
Char curChar = Convert.ToChar(parentNodeHeader.Substring(count, 1));
if (Char.IsNumber(curChar))
{
outString = outString + parentNodeHeader.Substring(count, 1);
}
} while (count < parentNodeHeader.Length - 1);
if (outString != "")
{
curNumber = Convert.ToInt32(outString);
}
return curNumber;
}
And then you have to update the query to account for the new information:
.OfType<TabItem>().SingleOrDefault(n => n.Header.ToString() == selectedItem.Header.ToString() + curNumber.ToString());
UPDATE
The function above just shows the kind of code I usually rely on; but for simple situations (like the proposed one of getting all the numbers in a string), you might prefer to rely on Regex, as suggested by Viv. You might rely on something on the lines of:
private int getNumber(string parentNodeHeader)
{
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(parentNodeHeader, #"\d+");
return Convert.ToInt32(m.Value);
}
This function only delivers the first set of consecutive numbers it finds; different result than the function above but enough as a proof of concept (intention of this answer).

Related

Making a simple change function for a database app C#

Sorry if this is an ultra beginner question but, I need to be able to change all field values except the item number by giving its item number not array index. I already have a add and delete feature.
using System;
struct ItemData
{
public int ItemNumber;
public string Description;
public double PricePerItem;
public int QuantityOnHand;
public double OurCostPerItem;
public double ValueOfItem;
}
class Program
{
public static void Main()
{
int invItems = 0;
var items = new ItemData[10];
while (true)
{
Console.Write("1. Add, 2. Change, 3. Delete, 4. List:");
string strx = Console.ReadLine();
var choice = int.Parse(strx);
Console.WriteLine();
switch (choice)
{
This is what I have so far but not sure where to start
case 2: //change items
{
Console.Write("Please enter an item ID No:");
string input = Console.ReadLine();
int changeItemNumber = int.Parse(input);
bool foundItem = false;
for (int x = 0; x < invItems; x++)
{
if (items[x].ItemNumber == changeItemNumber)
{
//code
}
}
if (!foundItem)
{
Console.WriteLine("Item {0} not found", changeItemNumber);
}
break;
}
It's still a little unclear what you're asking so if this is wide of the mark then feel free to let me know.
Start by mapping out the flow of the process and then implement one piece at a time. Here's a basic series of operations you'll want to do, not necessarily in this order:
Input ItemNumber.
Find index of matching record in array.
If not found report error and stop.
Get record from user.
Set ItemNumber in record to entered value.
Store record in array at same index as original.
If you think about the way the rest of the program works you should see that some of those parts - maybe most of them - are already done somewhere in your code. Your add operation probably already has the Get record from user code. Your delete operation probably has Input ItemNumber and Find index of matching record in array done already.
So pull those parts out into methods and reuse them where it makes sense. Anywhere you're writing the same code multiple times with minor changes try to see if you can extract the code into a small method you can call with maybe some parameters. Your "input a number" code for instance - used for operation selection, item number entry and presumably whatever you're doing for delete - can be pulled out to a supporting method like this one:
static int InputNumber(string prompt)
{
Console.Write($"{prompt}: ");
var inputText = Console.ReadLine();
return int.Parse(inputText);
}
Now when you want to change the way you're doing number input you have a central place to make the changes and you don't have to worry about tracking down all the places you wrote the same code.
The same goes for the rest of the pieces. When two operations share some of the same code then pull that code out to supporting methods. Finding the index of an item in the array by some criteria can - and should - be one of those:
// Return array index of matching item or -1 if not found
int ItemIndex(int itemNumber)
{
for (int i = 0; i < invItems; i++)
{
if (items[i].ItemNumber == itemNumber)
return i;
}
return -1;
}
Likewise the code - whatever that looks like - that your add operation uses to get a record from the user.
Once you do that your change item case looks a lot simpler:
case 2: // change items
var itemNumber = InputNumber("Please enter an item ID");
var index = ItemIndex(itemNumber);
if (index == -1)
{
Console.WriteLine($"Item {itemNumber} not found.");
}
else
{
var newItem = InputItemData();
newItem.ItemNumber = itemNumber;
items[index] = newItem;
}
break;
For bonus points you can actually provide the current values to the InputItemData method so the user can re-use them instead of just asking for a complete new record each time.

How to iterate through multiple variables?

I'd like to create a short program to download several pictures from a website.
On a form, I would like to enter a root-link to a website with placeholders.
The placeholders can be defined with Start/End value and asc/desc.
For example: the original link is
google.de/1236-01.jpg
and I'd like to generate all links from
google.de/1236-1.jpg
up to
google.de/9955-12.jpg
So my input would be "google.de/[0]-[1].jpg" and placeholders are set to:
[0] = start 1236|end 9955|asc
[1] = start 1|end 12|asc
Via GetValidCharacters() I get a String-List of valid combinations for each entered placeholder (can be selected via ascending/descending + start&end).
The goal I'm struggling with is to build all combinations of this link, because I need to determine while runtime, how much placeholders I have.
My idea was to loop over an queue and enquueue each new build line, until there is none left with placeholders, but I don't know how to do this.
I need to make sure that all combinations are entered and they are entered only once.
private static void CreateDownloadList()
{
Queue<string> tmpQueue = new Queue<string>(); //temp queue
tmpQueue.Enqueue(DL_path); //DL_Path = google.de/[0]-[1].jpg
string line = "";
while ((line = tmpQueue.Dequeue()) != null) //not empty
{
if (line.Contains("[")) //placeholder
{
string tmpLine = line;
//how to determine, which placeholder is next?? need to know this and replace this with every combination, I get from GetValidCharacters(start, end, DESC)
}
else //done
{
_urlList.Add(line);
}
}
}
how about a simple for loop?
for (int i = 1236; i <= 9955; i++)
{
for (int j = 1; j <= 12; j++)
{
tmpQueue.Enqueue(string.Format("google.de/{0}-{1}.jpg", i, j));
}
}
I'm not going give you the full code but here is some pseudo code that would solve the problem.
given :
todostack -- stack object that holds a list of unresolved items
replace_map -- map object that holds marker string and map of all values
marker_list -- list of all markers
final_list -- list object that holds the results
(note you can probably use marker_list and replace_map in one object -- I have them separate to make my code clearer)
init :
push todostack with your starting string
set marker_list and replace_map to correct values (from parameters I assume)
clear final_list
algorithm :
while (there are any items in todostack)
{
curitem = todostack.pop
if (curitem contains a marker in marker_list)
{
loop for each replacement in replace_map
{
new_item = curitem replaced with replacement
todostack.push(new_item)
}
}
else
add curitem to final_list
}
#Hogan this was the hint to the correct way.
solution is this
private void CreateDownloadList()
{
Queue<string> tmpQueue = new Queue<string>();
tmpQueue.Enqueue(downloadPathWithPlaceHolders);
while(tmpQueue.Count > 0)
{
string currentItem = tmpQueue.Dequeue();
bool test = false;
if(currentItem.Contains("["))
{
foreach(Placeholder p in _placeholders)
{
if(currentItem.Contains(p.PlaceHolder))
{
foreach(string s in p.Replacements)
{
tmpQueue.Enqueue(currentItem.Replace(p.PlaceHolder, s));
}
test = true;
}
if(test)
break;
}
}
else
{
_downloadLinkList.Add(currentItem);
}
}
}

Sending an Array to Controls

So, I'm a student beginning to learn C# in school and one of my current projects has come to involve outputting an expandable, class method-bound array to controls on a form, and I've hit a confusing snag. I've set the proper access modifiers to public, I've successfully used this formatting for an expandable array before, and my program shows no build errors, but the data won't output.
I can see my logic error being a part of my array declaration, which in past permutations seemed like it wasn't expanding, though my current strategy, which calls the Expand() method, directly increasing my index noobIdentifier, which is a universal variabel within the class, outside of the actual method.
//Create Form Instance
AttendanceReport report = new AttendanceReport();
//Expand Array
Expand();
//Create Array
int SIZE = noobIdentifier + 1;
Noob[] info = new Noob[SIZE];
//Assign Data to Array
info[noobIdentifier].s_class = Class;
info[noobIdentifier].s_name = Name;
info[noobIdentifier].s_id = ID;
info[noobIdentifier].s_password = Password;
info[noobIdentifier].s_formerDistrict = District;
info[noobIdentifier].s_grade = Grade;
info[noobIdentifier].s_country = Country;
info[noobIdentifier].index = SIZE;
It could also be a part of my actual output block in the same method. I have considered trying to use a For loop instead of a Foreach in my Listbox output, though the latter seemed more concise.
//Ouput Gateway
if (output == true)
{
//Temp Holder
int i;
//Output items to Attendance Report Listbox
foreach (Noob noob in info)
{
//Send Index to string
string indexer = noob.index.ToString();
//Convert to Int
i = int.Parse(indexer);
report.newStudentSelectionBox.Items.Add(i);
}
}
//Output Text Gateway
if (textOutput > 0)
{
//Declare Index
int n = textOutput - 1;
report.classOutput.Text = info[n].s_class;
report.nameOutput.Text = info[n].s_name;
report.idOutput.Text = info[n].s_id.ToString();
report.gradeOutput.Text = info[n].s_grade.ToString();
report.districtOutput.Text = info[n].s_formerDistrict;
report.countryOutput.Text = info[n].s_country;
}
Both output and textOutput are the method inputs, with textOutput being declared as an int so that it can be used as a direct reference to array placements.
Give this a try
In Noob class:
public class Noob
{
//Your Definition...
public override string ToString() => this.Name;
}
Modify loop like:
//Output items to Attendance Report Listbox
foreach (Noob noob in info)
{
report.newStudentSelectionBox.Items.Add(noob);
}

Convert user entered string into an object in c#

I have a battleship like terminal game, the user enters a coordinate like e2, and the program checks one of the instance variables of my object Box, it checks whether hasShip is true, if its true then it will make the coordinate e2 false, and give the output "Ship destroyed"
The problem is that all my objects are called a1,a2,a3,a4,a5,b1,b2 and so on.
I have created 25 instances of the Box class. All names as such.
Once the program gets input, either e4 ,e5 etc. I want to convert that string into an object.
For example( I want to do something like this )
target = Console.ReadLine();
target.hasShip == true;
I want to convert target into an object, then use target to use the methods of the Box class.
Because the other approach requires me to make loads of if statements, which isn't clean code, doesn't look good, and is a waste if you ask me.
Thanks in advance,
New Answer: use an Array
I am slow. I did not pay attention that you are making a battleship-like game, and that we know that the "boxes" make a rectangle. We can store this efficiently in an array.
Why I did not catch up to this fact earlier? I guess I need to wake up properly.
So, use an array:
var board = new Box[5, 5];
Now, to populate it, we can do a double for loop:
for(var indexRow = 0; indexRow < 5; indexRow++)
{
for(var indexCol = 0; indexCol < 5; indexCol++)
{
board[indexRow, indexCol] = new Box();
}
}
Note: pay attention that the indexes go from 0 to 4. For a total of 5 values: {0, 1, 2, 3, 5}.
And to query from it, we will need the indexes...
Addendum on populating the array
In comments, OP has said that each Box has an id and the ship positions are picked at random.
We can give the id in the loop:
for(var indexRow = 0; indexRow < 5; indexRow++)
{
for(var indexCol = 0; indexCol < 5; indexCol++)
{
var box = new Box();
box.vhID = (((char)(((int)'a') + indexRow))).ToString() + ((char)(((int)'1') + indexCol)).ToString();
board[indexRow, indexCol] = box;
}
}
What I am doing here is constructing the id from the indexes. Basically taking the value of 'a' and adding the indexRow will give us 'a' when indexRow is 0, 'b' when it is 1 and so on. Similarly, we get the digit that represents the column.
Note: We convert the char to int, do the addition, then convert back to char... and then from char to string. Once we have string, we can concatenate them.
I do not think we need this id. But, hey, you can do it like this.
OP also mentions that he will pick 4 ship positions at random. Fair enough:
var random = new Random();
for (var ships = 0; ships < 4; ships++)
{
board[random.Next(0, 4), random.Next(0, 4)].hasShip = true;
}
Since the user inputs an string, I suggest to create a function to convert it to the index pair:
var input = Console.ReadLine();
if (TryGetCoordinates(input, out int irow, out int icol))
{
var target = board[irow, icol];
}
else
{
Console.WriteLine("The cell {0} does not exist.", input);
}
// ...
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
// ...
}
Start by validating null:
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
indexRow = -1;
indexCol = -1;
if (cell == null)
{
return false;
}
// ...
}
Note: Feel free to use Trim, ToUpper or ToUpperInvariant.
We know that must be a letter followed by a digit, we can validate the length:
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
indexRow = -1;
indexCol = -1;
if (cell == null)
{
return false;
}
if (cell.Length != 2)
{
return false;
}
// ...
}
We extract the characters and from them the coordinates. Noting that the first one is a letter, and the other a digit. We can also validate they are withing bounds.
bool TryGetCoordinates(string cell, out int indexRow, out int indexCol)
{
indexRow = -1;
indexCol = -1;
if (cell == null)
{
return false;
}
if (cell.Length != 2)
{
return false;
}
indexRow = (int)cell[0] - (int)'a';
indexCol = (int)cell[1] - (int)'1';
return indexRow < 5 && indexRow >= 0 && indexCol < 5 && indexCol >= 0;
}
And of course, you can do a loop of the validation similar to what was explained in the old answer.
Note: the issue with value types I describe in the old answer still applies with the array.
Old Answer: Use a Dictionary
I believe you do not want to convert the string to an object (the string is an object by the way), you want to pick the Box object you previously created based on the string. And you want to do it without using if statements. What you need is a dictionary.
So, you would have Dictionary<string, Box> meaning that it is a dictionary that you can query by string and stores Box.
Addendums:
In this case, string is the key type, by which we will access the dictionary. When we add an object to the dictionary we identify it with a key, and when we retrieve it, we also use the key. The key does not have to be string, you can choose a different type. string is convenient in this case because it is what you get from Console.ReadLine().
You can create the dictionary to store whatever type you need. If you do not need Box, you can create a dictionary that stores something else.
Creating and populating the Dictionary
Then, you add to the Dictionary all your Box objects, like this:
var dict = new Dictionary<string, Box>();
// ...
dict.Add("a1", CreateBoxA1());
Where CreateBoxA1 represents whatever means you have to create the object. No, you do not need to create a method for each Box... you can do it like this:
dict.Add("a1", new Box());
Or whatever. I do not know how you create them, so consider that a placeholder, ok? ok.
Querying and retrieving values from the Dictionary
Once you have all your Box instances in your dictionary, you can get the one you need using the string:
Console.WriteLine("Enter the name of the Box:");
var name = Console.ReadLine();
var target = dict[name];
Addendum: The value you get from dict[name] is the value that you added to the dictionary with that key. So, if the user typed "a1" it dict[name] will be the value that we added with "a1" (dict.Add("a1", new Box());). Again, if what you need is not Box you can create a dictionary to store a different type.
Input validation
You can also use the Dictionary to validate if the string corresponds to a Box that exists, for example:
Console.WriteLine("Enter the name of the Box:");
var name = Console.ReadLine();
if (dict.KeyExists(name))
{
var target = dict[name];
// ...
}
else
{
Console.WriteLine("The Box {0} does not exist", name);
}
It goes without saying, but... you can make a loop based on that, for example:
Box target = null;
while(true)
{
Console.WriteLine("Enter the name of the Box:");
var name = Console.ReadLine();
if (dict.KeyExists(name))
{
target = dict[name];
break;
}
Console.WriteLine("The Box {0} does not exist", name);
}
Also, it goes without saying, but... you can add your own validations and sanitation steps. For example using ToUpper, ToUpperInvariant or Trim. And I would remind you that changing strings to lower or upper case is culture sensitive.
See also: Best Practices for Using Strings in .NET.
Editing an removing objects from the dictionary
Once you have the object you retrieved from the Dictionary...
var target = dict[name];
We can use it, and even modify it:
var target = dict[name];
if (target.hasShip) // no need for "== true" if hasShip bool
{
target.hasShip = false;
Console.WriteLine("Ship Destroyed");
}
An special note must be done if Box is value type. For a custom type that means that it is not a class but a struct. The problem with value types is that they are copied on assignment, meaning that when you do var target = dict[name]; with a value type, you get a copy. You must then update the dictionary once you manipulated it:
var target = dict[name];
if (target.hasShip) // no need for "== true" if hasShip bool
{
target.hasShip = false;
dict[name] = target;
Console.WriteLine("Ship Destroyed");
}
Note: As I said above, this is only needed for value types.
And you can even remove the Box from the dictionary if that is necesary:
dict.Remove(name);

if ListBox.SelectedIndex contains a number 1

Basically i'm trying to make it so if the Selected Index from a listbox (for example lets say the selected index is "Server1") contains a number ("1") or a certain word ("Server") it would do a certain thing. For example: If the selected index from the listbox contains a number 1, it would enter 1 into a text box. If the selected index contains a number 2, it would open up a application, just for an example.
what i tried:
if (csrListBox.SelectedIndex = "1");
and:
{
List<string> items = ListBox.Items.Cast<string>().ToList();
foreach (string item in ListBox.Items)
{
Regex regex = new Regex(#"1");
Match match = regex.Match(item);
if (match.Success)
{
*Doing Something*
}
}
ListBox.DataSource = items;
}
Try this:
if (csrListBox.SelectedIndex == 1);
The second "=" sign matters - it states you're doing a boolean check instead of assigning value "1" to the value.
Mistake - You are assigning value.
Use compare operator ==. and SelectedIndex is int not string.
if (csrListBox.SelectedIndex == 1)
{
// Your code goes here.
}
I would suggest you work with ListBox item values instead of selectedIndex as the SelectedIndex can only be an integer value. Something like this linq query will return if the ListBox contains a certain value that is selected. This takes care of multi-selections too.
var myValue = "1";
bool listContainsItem = ListBox.Items.Any(item => item.Value == myValue && item.Selected);

Categories

Resources