Is it possible to call a variable from string input - c#

I have five Labels in my form.
The names of the label are the following.
1) labelTeam1Name.Text
1) labelTeam2Name.Text
3) labelTeam3Name.Text
4) labelTeam4Name.Text
5) labelTeam5Name.Text
now I want to run a loop and get the text values of the label
for( int i = 1; i < 6; i++ )
{
string str = labelTeam(i)Name.Text // Get the values based on the input i
}
I can definitely fill these values in an array or list and then call them in the loop.
is it possible to do something like this labelTeam(i)Name.Text?

You can use Controls and OfType
Filters the elements of an IEnumerable based on a specified type.
var results = Controls.OfType<Label>().Select(x => x.Text);
or
foreach (var ctrl in Controls.OfType<Label>())
{
//var str = ctrl.Text;
}
if you need to base this on the name, you could use Find or
var labels = Controls.OfType<Label>().ToList();
for (int i = 0; i < labels.Count(); i++)
{
var label = labels.FirstOrDefault(x => x.Name == $"blerp{i}derp");
if (label != null)
{
}
}

You can use the Controls.Find() method:
for( int i = 1; i < 6; i++ )
{
string str = ((Label)Controls.Find("labelTeam" + i + "Name",true)[0]).Text // Get the values based on the input i
}

You can use a Label array.
System.Windows.Forms.Label[] Labels = new System.Windows.Forms.Label[5];
Labels[0] = labelTeam1Name;
Labels[1] = labelTeam2Name;
Labels[2] = labelTeam3Name;
Labels[3] = labelTeam4Name;
Labels[4] = labelTeam5Name;
for( int i = 0; i < Labels.Lenth; i++ )
{
string str = Labels[i].Text;
}

Related

Replacing char occurrences in a string with x or y

I'm a bit out of practice and currently having a hard time with this one question.
If a character appears only once in the given string, I need to replace it with 'x' If a character appears several times in the string I need to replace it with 'y' (Case sensitive) e.g. "Passable" would return as "xyyyyxxx".
So far I've tried converting the string to a char array and comparing it to a copy of itself using nested loops, but I cant figure out a way to save the index of all occurrences to change them into x or y.
Unfortunately it didn't work out too great, and I'm sure there is a much simpler way of doing this that I'm missing, any ideas?
This is what I have so far:
for (int i = 0; i < OccuredArray.Length; i++)
{
for (int x = 0; x < CharArray.Length; x++)
{
if (CharArray[x] == OccuredArray[i])
{
TimesOccured++;
}
}
if (TimesOccured > 1)
{
for (int y = 0; y < OccuredArray.Length; y++)
{
if (OccuredArray[i] == OccuredArray[y])
{
OccuredArray[i] = 'y';
}
}
}
if (TimesOccured == 1)
{
for (int y = 0; y < OccuredArray.Length; y++)
{
if (OccuredArray[i] == OccuredArray[y])
{
OccuredArray[i] = 'x';
}
}
}
TimesOccured = 0;
}
What about simplifying it a bit? Use a Dictionary<char, int> to count the occurences:
string input = "Passable";
var charCounter = new Dictionary<char, int>();
foreach (char c in input)
{
charCounter.TryGetValue(c, out int count);
charCounter[c] = ++count;
}
string output = string.Concat(input.Select(c => charCounter[c] == 1 ? "x" : "y"));
If you wanted to use a loop then you could do it like this:
var text = "Passable";
var countsByChar = new Dictionary<char, int>();
foreach (var c in text)
{
if (!countsByChar.TryAdd(c,1))
{
countsByChar[c]++;
}
}
foreach (var c in countsByChar.Keys)
{
text = text.Replace(c,
countsByChar[c] == 1
? 'x'
: 'y');
}
Console.WriteLine(text);
You could use a LINQ query instead though:
var text = "Passable";
var countsByChar = text.GroupBy(c => c)
.ToDictionary(g => g.Key,
g => g.Count());
foreach (var c in countsByChar.Keys)
{
text = text.Replace(c,
countsByChar[c] == 1
? 'x'
: 'y');
}
Console.WriteLine(text);
Based on your code I modified and commented on some cases which you should check out:
// So, I suggest this is the input string.
var OccuredArray = "abbcdd";
// It is always better for the length to be in a variable and to be used in for loops.
var OccuredArrayLenght = OccuredArray.Length;
// Here we will fill the placeholders depending on their appearance.
var OccuredArrayResult = new char[OccuredArrayLenght];
// Here we will trace the occurrences in which each index corresponds to a char from the ASCII table.
var OccuredArrayCharMap = new bool[byte.MaxValue];
for (int CurrentCharIndex = 0; CurrentCharIndex < OccuredArrayLenght; CurrentCharIndex++)
{
var CurrentChar = OccuredArray[CurrentCharIndex];
// If we have processed this char, we continue with the next one
if (OccuredArrayCharMap[CurrentChar] == true)
{
continue;
}
var Placeholder = 'x';
// Then we will check all the characters, starting from the next index
for (int NextCharIndex = CurrentCharIndex + 1; NextCharIndex < OccuredArrayLenght; NextCharIndex++)
{
//if (CharArray[FollowingCharIndex] == OccuredArray[OccuredArrayCharIndex])
//{
// TimesOccured++;
//}
// If char occured twice then the placeholder is y and we can break here
if (OccuredArray[NextCharIndex] == CurrentChar)
{
Placeholder = 'y';
break;
}
}
//if (TimesOccured > 1)
//{
// for (int y = 0; y < OccuredArray.Length; y++)
// {
// if (OccuredArray[OccuredArrayCharIndex] == OccuredArray[y])
// {
// OccuredArray[OccuredArrayCharIndex] = 'y';
// }
// }
//}
//if (TimesOccured == 1)
//{
// for (int y = 0; y < OccuredArray.Length; y++)
// {
// if (OccuredArray[OccuredArrayCharIndex] == OccuredArray[y])
// {
// OccuredArray[OccuredArrayCharIndex] = 'x';
// }
// }
//}
//TimesOccured = 0;
// Here we fill ouer result array with coresponding placeholder at matched index.
for (int CharIndex = CurrentCharIndex; CharIndex < OccuredArrayLenght; CharIndex++)
{
if (OccuredArray[CharIndex] == CurrentChar)
{
OccuredArrayResult[CharIndex] = Placeholder;
}
}
// And at the end we say that this char was already replaced.
OccuredArrayCharMap[CurrentChar] = true;
}
var ResultString = new string(OccuredArrayResult);
Console.WriteLine(OccuredArray);
Console.WriteLine(ResultString);

Is it possible to remove the first element of a datatable cell, which is a string value?

I have a datatable filled with strings.
On a column of these I want to remove the first 2 elements.
""Edit: By element i mean the first 2 characters of the string of a datacell. End of Edit""
Such thing is easy in strings, using the remove command. But is the same possible in a datacell?
PS: Some might answer "Why not define the string first, and then place it on the cell" but I'd rather avoid it, since my cells are defined by a series of concatenations. Here's how I'm doing it.
//create a list of the columns name
List<string> listadecolunas = new List<string>();
foreach (DataColumn coluna in DataAccess.Instance.tabelafinal5.Columns)
{
listadecolunas.Add(coluna.ColumnName);
}
//create a concatenated string that encolsures all the following columns
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + " + " + elemento0;
}
}
}
I didn't want to fill this with detail, but since I've posted the code, in the end I will get a datacell that begins with + and I want to remove it, it's just that.
Edit2:
My cell looks like this
I wanna remove the first + :)
You can do this with a little helper like that:
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
bool isFirst = true;
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
if(isFirst)
{
isFirst = false;
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + elemento0;
}
else
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + " + " + elemento0;
}
}
}
}
A little more efficient with a StringBuilder but same concept:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < DataAccess.Instance.tabelafinal5.Rows.Count; i++)
{
bool isFirst = true;
sb.Clear();
for (int y = 2; y < listadecolunas.Count; y++)
{
string elemento0 = DataAccess.Instance.tabelafinal5.Rows[i][y].ToString();
if (elemento0 != "")
{
if(isFirst)
{
isFirst = false;
}
else
{
sb.Append(" + ")
}
sb.Append(elemento0);
}
}
if(sb.Length > 0)
{
DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] = DataAccess.Instance.tabelafinal5.Rows[i]["Expressão"] + sb.ToString();
}
}
Now, if you even construct the Cell across different methods and you do not know what has happened before, you would set isFirst based on if the current value of the cell is empty or not instead of assigning true to it by default.

How do you retrieve inputted text from a textbox array?

I am creating a calculator where the user enters a number into a textbox specifing how many inputs (textboxes) the user wants to have (Code not shown). I have used a textbox array to create these textboxes. The problem comes when I want to get the text from these textboxes to perform the calculations, the code I have written so far for this is shown below:
int n;
TextBox[] textBoxes;
Label[] labels;
double[] values;
public void GetValue()
{
n = Convert.ToInt16(txtInputFields.Text);
values = new double[n];
textBoxes = new TextBox[n];
for (int i = 0; i < n; i++)
{
}
}
I am unsure what to put in the for loop for this; I have tried the following:
values[n] = Convert.toDouble(textBoxes[n].Text);
but it gives me the error: Index was outside the bounds of the array.
I am new to C# and programming in general so any help would be much appreciated.
Thanks.
EDIT: Code to create textboxes is shown here:
public void InstantiateTextFields()
{
n = Convert.ToInt16(txtInputFields.Text);
int posLeft = 100;
textBoxes = new TextBox[n];
labels = new Label[n];
// Creates number of inputs and labels as specified in txtInputFields (n).
for (int i = 0; i < n; i++)
{
textBoxes[i] = new TextBox();
textBoxes[i].Top = 100 + (i * 30);
textBoxes[i].Left = posLeft;
textBoxes[i].Name = "txtInput" + (i + 1);
labels[i] = new Label();
labels[i].Top = 100 + (i * 30);
labels[i].Left = posLeft - 50;
labels[i].Text = "Input " + (i + 1);
labels[i].Name = "lblInput" + (i + 1);
}
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBoxes[i]);
this.Controls.Add(labels[i]);
}
}
Your code in the GetValue method recreates the array of textboxes and doing so destroys the orginal content (the textboxes dynamically created InstantiateTextFields).
In this way your loop fails with Object Reference not set.
You just need to use the global variable without reinitiaizing it
public void GetValue()
{
n = Convert.ToInt16(txtInputFields.Text);
values = new double[n];
// textBoxes = new TextBox[n];
for (int i = 0; i < n; i++)
{
values[i] = Convert.ToDouble(textBoxes[i].Text);
}
}
There is something to be said about reading the input text and converting it to double without checks. If your user types something that cannot be converted to a double your code will crash on the Convert.ToDouble line. Use instead
double temp;
for (int i = 0; i < n; i++)
{
if(double.TryParse(textBoxes[i].Text, out temp)
values[i] = temp;
else
{
// Not a double value....
// A message to your user ?
// fill the array with 0 ?
// Your choice....
}
}
values[n] = Convert.toDouble(textBoxes[n].Text); gives you error because n is outside of the array. You allocate an array with the size of n which is zero indexed aka the last element is at position n-1.
for (int i = 0; i < n; i++)
{
values[i] = Convert.toDouble(textBoxes[i].Text);
}

multi line textbox to array C#

I am trying to transfer values from each line of a multi line text box into either a string array or a multidimensional array. I also have 3 multi-line text boxes which need to put into the same array. Below is one of the methods I have been trying:
ParkingTimes[0] = tbxtimeLimitS1.Text;
for (int i = 1; i <= 10; i++)
ParkingTimes[i] = tbxparkingTimesS1.Lines;
ParkingTimes[11] = tbxtimeLimitS2.Lines;
for (int x = 0; x <= 10; x++)
for (int i = 12; i <= 21; i++)
ParkingTimes[i] = tbxparkingTimesS2.Lines;
ParkingTimes[11] = tbxtimeLimitS2.Lines[0];
for (int x = 0; x <= 10; x++)
for (int i = 23; i <= 32; i++)
ParkingTimes[i] = tbxparkingTimesS3.Lines;
What am I doing wrong? Is there a better way to accomplish this?
You can simply do
string[] allLines = textbox.Text.Split('\n');
This will split each line and store the results in the appropriate index in the array. You can then iterate over them like so:
foreach (string text in allLines)
{
//do whatever with text
}
You could use a List instead of a string array
Then the AddRange method could simplify your method eliminatig the foreach loop
List<string> ParkingTimes = new List<string>()
ParkingTimes.Add(tbxtimeLimitS1.Text);
ParkingTimes.AddRange(tbxparkingTimesS1.Lines);
ParkingTimes.AddRange(tbxtimeLimitS2.Lines);
ParkingTimes.AddRange(tbxparkingTimesS2.Lines);
ParkingTimes.AddRange(tbxtimeLimitS2.Lines);
ParkingTimes.AddRange(tbxparkingTimesS3.Lines);
If your code still requires a string array it is possible to get back the array with
string[] myLines = ParkingTimes.ToArray();
An example of this List<string> functionality could be found on MSDN here
You can do something like this:
var totalLines = new List<String>();
totalLines.AddRange( tbxparkingTimesS1.Lines );
totalLines.AddRange( tbxparkingTimesS2.Lines );
totalLines.AddRange( tbxparkingTimesS3.Lines );
if you need it in an array instead of a list, then call:
var array = totalLines.ToArray();
Hope it helps.
This works for me:
string[] textArray = textBox1.Text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.None);
string[] split = textBox1.Text.Split('\n');
or you can also use:
int srlno=10;
string[] split = new string[srlno];
foreach(string x in textBox1.Lines)
{
split = (split ?? Enumerable.Empty<string>()).Concat(new[] { x }).ToArray();
}

How to use different labels in one for cycle

ex: label[i].text. in
int r = GridView1.Rows.Count;
for (int i = 0; i < r; i++)
{
Label[i]+"1".text="something";
}
Here, in one for cycle, I want to fill different labels. Label id's are Label01, Label02,Label03 and so on. What is the correct syntax?
If you can 'predict' the Id of the Label then you can find it:
int r = GridView1.Rows.Count;
for (int i = 0; i < r; i++)
{
string id = "baseName" + i; // your naming scheme
var lbl = (Label) this.FindControl(id);
lbl.text="something";
}
You can add all the labels into a List<Label> or SortedList<Label> and iterate over that.
var labels = new SortedList<Label>();
lables.Add("Lable01", Label01);
lables.Add("Lable02", Label02);
...
int r = GridView1.Rows.Count;
for (int i = 0; i < r; i++)
{
lables["Label" + i.ToString("00")].text = "somthing";
}
There are no control arrays in .NET, as there were in VB6.
Label myLabel = this.FindControl("Label"+i+"1") as Label;
myLabel.Text = "my text";
THIS should solve your problem

Categories

Resources