Check Label in email and delete specific ones - c#

I have this code that is suppose to check each label for the word "closed", and after its done checking it will remove all the text that is in the labels and place everything thats NOT labeled "closed" into the TO section of an email. I dont know what im doing wrong but it doesnt work. Any suggestions?
foreach (Control c in Controls)
{
if (c is Label)
{
// Grab label
Label lbl = c as Label;
if (lbl.Text.Contains("closed"))
{
lbl.Text.Replace("closed", "");
}
}
}

Apparently you forgot to assign the modified text value, because Replace() method returns replaced text as return value:
lbl.Text = lbl.Text.Replace("closed", "");
But there might be more problems with your code, it's not very clear how is your problem related to emails.

Initially take your input (i.e. label list in a List)
List<string> TotalLabels = GetAllLabels();
for (int i = 0; i < TotalLabels.Count; i++)
{
if (TotalLabels[i].Contains("closed"))
{
TotalLabels.RemoveAt(i);
i--;
}
}
Now you have a final list which you wanted removing "closed".

Related

How to add a specific number of labels to a label array?

I've been trying to add a few candles to an array so I can use that array for the rest of my code, using the candles' properties more easily. However it doesn't seem as my code is correct and I would love someone to help me with this. (Color != DarkGoldenrod differentiates the candles from the other labels in my project, all of which have the same color)
private Label[] CandlesToMatrix()
{
Label[] candles = new Label[7];
foreach (Control ctrl in this.Controls)
{
if ((ctrl is Label) && (ctrl.BackColor != Color.DarkGoldenrod))
{
for (int i = 0; i < 7; i++)
{
candles[i] = (Label)ctrl;
}
}
}
return candles;
}
The problem you are facing is that you assign each element in the array the control that matches the criteria.
The code runs like: Enumerate all controls and check if it is a label and not some specific color. If he finds one, he will fill up the whole array with a reference to that control. If the array was already filled by the previous match, it will be overwritten.
So you end up with an array filled with either a null or the last matching control.
I think you would like to have the array filled with 'unique' controls. So every time you find a match, you have to increase the index to write it to.
For example:
private Label[] CandlesToMatrix()
{
Label[] candles = new Label[7];
// declare a variable to keep hold of the index
int currentIndex = 0;
foreach (Control ctrl in this.Controls)
{
if ((ctrl is Label label) && (label.BackColor != Color.DarkGoldenrod))
{
// check if the currentIndex is within the array. Never read/write outside the array.
if(currentIndex == candles.Length)
break;
candles[currentIndex] = label;
currentIndex++;
}
}
}
I'll add another example......
C# has so much more to offer. This is a little old c programming style. Fixed size arrays etc. In C# you have also a List which uses the type you use between the < and >. For example. List<Label>.
Here is an example which uses a List.
private Label[] CandlesToMatrix()
{
List<Label> candles = new List<Label>();
// int currentIndex = 0; You don't need to keep track of the index. Just add it.
foreach (Control ctrl in this.Controls)
{
if ((ctrl is Label label) && (label.BackColor != Color.DarkGoldenrod))
{
candles.Add(label);
}
}
return candles.ToArray(); // if you still want the array as result.
}
and... you could also use Linq (which is a next step)
private Label[] CandlesToMatrix()
{
return this.Controls
// check the type.
.OfType<Label>()
// matches it the creteria?
.Where(label => ((ctrl is Label label) && (label.BackColor != Color.DarkGoldenrod))
// fillup an array with the results
.ToArray();
}

C# assigning several buttons to the same method

I'm trying to build a simple game of 'HangMan' using Win8 GUI. I've build the GUI with 26 buttons on screen, each represent one letter of the alphabet.
I want to connect all the buttons to the same method that checks whatever the value of the button that was pressed matches one of the letter in the selected word. I've looked at this question answers that supposed to help me in this but the one difference i think is that all the logic and methods in my game are on different class, the "Game Manager" class.
How can I subscribe multiple buttons to the same event handler and act according to what button was clicked?
Plus i didnt quit understood how, using this solution the method which i will assign all the buttons to will know which button was pressed. I hope i explaind my situation clear enough, if not i can provide parts of the code for better understanding.
It's a basic method for what you want:
static string key = "Level solution";
char[] chars = key.ToCharArray();
void check (object sender)
{
var button = sender as Button;
character = Convert.ToChar(button.Text);
int i = 0;
foreach (char c in chars)
{
//checks every character to mark them
if (c == character)
{
chars[i] = ' ';
//Makes character unusable for later use
//Anything you want now for true letters, for example showing pics or adding them to a label
}
i++;
}
//checks every character of key again, to see if player is won
int count = 0;
foreach (char c in chars) {
if (c != ' ') count++;
//Adds a number for anything expect space
}
if (count == 0)
{
MessageBox.Show("You won!");
}
}
You can paste it on top of your codes and then use just this code in any button:
check(sender);

Change text of a listview

I have a quick question about listView's and how check if a ListView (which contains null items) has a certain string?
Here is the code which add to sepcific items (under column 5 in the listView). It basically checks if that item appears in Google search or not. If it does, it'll write yes to that specific row, if not it'll leave it blank:
string google2 = http.get("https://www.google.com/search?q=" + textBox1.Text + "");
string[] embedid = getBetweenAll(vid, "type='text/html' href='http://www.youtube.com/watch?v=", "&feature=youtube_gdata'/>");
for (int i = 0; i < embedid.Length; i++)
{
if (google2.Contains(embedid[i]))
{
listView1.Items[i].SubItems.Add("Yes");
}
}
Now what I am trying to do is check if that certain column contains items that say Yes. If it does color them Green if not don't.
Here's the code for that:
if (i.SubItems[5].Text.Contains("Yes"))
{
labelContainsVideo.ForeColor = System.Drawing.Color.Green;
}
My issue is I keep getting an error that says InvalidArgument=Value of '5' is not valid for 'index'.
My hunch is that there are null items in column 5 which might be messing it up but I dont know.
Any idea on how to fix this?
Check the Item to see if the SubItem Collection has the correct Number of values.
i.e.
int threshold = 5;
foreach (ListViewItem item in listView1.Items)
{
if (item.SubItems.Count > threshold)
{
if (item.SubItems[5].Text.Contains("Yes"))
{
// Do your work here
}
}
}

Append int to end of string or textbox name in a For Loop C#

I have a C# application in which there are several textboxes with the same name except for a number on the end which starts at 1 and goes to 19. I was hoping to use a for loop to dynamically add values to these text boxes by using an arraylist. There will be situations where there will not be 19 items in the arrayList so some text boxes will be unfilled. Here is my sample code for what I am trying to do. Is this possible to do?
for (int count = 0; count < dogList.Count; count++)
{
regUKCNumTextBox[count+1].Text=(dogList[count].Attributes["id"].Value.ToString());
}
So you've got a collection of text boxes that are to be filled out top-to-bottom? Then yes, a collection of TextBox seems appropriate.
If you stick your TextBox references in an array or a List<TextBox> -- I wouldn't use an ArrayList as it's considered deprecated in favor of List<T> -- then yes, you can do that:
TextBox[] regUKCNumTextBox = new []
{
yourTextBoxA,
yourTextBoxB,
...
};
Then yes your logic is possible, you can also query the control by it's name, though that would be heavier at runtime - so it's a tradeoff. Yes, in this solution you must set up a collection to hold your text box references, but it will be more performant.
Try this:
(By the way I am assuming you use WinForms)
for (int count = 0; count < dogList.Count; count++)
{
object foundTextBox = this.Controls.Find("nameofTextBoxes" + [count+1]);
if (foundTextBox != null)
{
if (foundTextBox is TextBox)
{
((TextBox)foundTextBox).Text=(dogList[count].Attributes["id"].Value.ToString());
}
}
}
With this code you are trying to find a Control form your Forms Controls collection. Then you have to make sure the control is of the TextBox type. When it is; cast it to a TextBox and do what you want with it. In this case; assign a value to the Text property.
It would be more efficient to keep a collection of your TextBoxes like in the solution offered by James Michael Hare
Yikes; something doesn't seem quite right with the overall design there; but looking past that, here's a quick stab at some pseudo code that might work:
for (int count = 0; count < dogList.Count; count++)
{
var stringName = string.Format("myTextBoxName{0}", count);
var ctrl = FindControl(stringName);
if(ctrl == null) continue;
ctrl.Text = dogList[count];
}

Use variable as part of textbox name in C#

I am developing a program wth 30 text boxes and 30 check boxes next to them. I want people to check names and then press the send button. The program then saves the names in a txt file with a true or false statement next to them, it then uploads the file to a ftp server for me analize.
The problem I am facing is that I don't want to write code for every text and check box to load and save it's value on the txt file.
If I name the text boxes something like tbox1;tbox2;tbox3 etc. How would use a loop to say write the value of tbox i + ; + cbox i on line i of thing.txt or vice versa?
Please any help would be grately apreciated because this will save me a lot of unnesacery code writing!
You should create a List<TextBox>, and populate it with your textboxes in the constructor.
You can then loop through the list and process the textboxes.
for (int i = 0; i <= count; i++)
{
TextBox textbox = (TextBox)Controls.Find(string.Format("tbox{0}", i),false).FirstOrDefault();
CheckBox checkbox = (CheckBox)Controls.Find(string.Format("cbox{0}", i),false).FirstOrDefault();
string s = textbox.Text + (checkbox.Checked ? "true" : "false");
}
You can loop over all the controls on your form and retrieve the values from them based on their type/name.
Loop through controls in your form/control and investigate name:
foreach (Control control in f.Controls)
{
if (control is TextBox)
{
//Investigate and do your thing
}
}
Assuming this is ASP.NET, you could use something like this:
StringBuilder sb = new StringBuilder();
for(int i = 1; i < 30; i++){
TextBox tb = FindControl("TextBox" + i);
Checkbox cb = FindControl("CheckBox" + i);
sb.AppendFormat("TextBox{0}={1}; {2}", i, tb.Text, cb.Checked);
}
string result = sb.ToString();
// Now write 'result' to your text file.

Categories