How do I add a find text feature in C#? - c#

Picture of my program
This program has a bunch of buttons of produce items. When the user clicks a button it automatically copies the bar-code of that item. For example if the user clicks the "Apples" button it will copy 43490905, aka the bar-code for apples. The user can then paste this number anywhere they want.
This produce list will get VERYY long, like 1000+ items. I don't want to navigate through all the items just to find apples.
How do I add a "Find" functionality? Like in google chrome I can just hit ctrl+f and type in apples and boom I can get all the results highlighted on the page.
Thank you.

For convenience, you should choose the TextChanged function for your TextBox, or if you still want to use the button, let copy these code and paste into your Button_Click function
private void textBox1_TextChanged(object sender, EventArgs e)
{
string text = textBox1.Text;
foreach (Control c in this.Controls)
{
if (c is Button)
{
if (!c.Text.ToUpper().Contains((text.ToUpper())))
{
//You can set the hightlight, or anything here
c.Visible = false;
}
else
{
//You can set the hightlight, or anything here
c.Visible = true;
}
}
}
}

Related

How to check all the Text boxes are filled? [duplicate]

This question already has answers here:
How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?
(28 answers)
Closed 2 years ago.
I created a form with multiple panel over it and I use panel control to display relevant information. Third panel have 20 text boxes and I want to check whether all the details are filled. So I simply used this code below.
private void Calculatebutton_Click(object sender, EventArgs e)
{
foreach(Control c in Controls)
{
if(c is TextBox)
{
Console.Beep();
if (!String.IsNullOrWhiteSpace(textbox.Text) &&
!String.IsNullOrEmpty(textbox.Text))
{
SaveToDatabaseButton.Enabled = true;
}
}
}
}
The problem is that the condition in the if statement is getting false, I cannot hear any beep sound or the other button enabled. if I changed from "c is TextBox" to "c is Panel" i can hear the beep sound for three times. I also tried the code like this
if(c is TextBox)
{
c.Text = " ";
}
But nothing works. Please help me to overcome this problem. Thanks in advance.
I tested your code and it entered the beep just fine, consider Ahmed's comments about the text boxes being in another container or form.
Not related to your question directly but, the logic of when to enable the button is incorrect as far as I can tell, because only one of the text boxes being not empty will suffice to enable the button to save to database, I assume you want all text boxes not empty for that to happen.
I would suggest something like this if that's the case:
private void Calculatebutton_Click(object sender, EventArgs e)
{
var isThereEmptyTextBox = false;
foreach(Control c in Controls)
{
if(c is TextBox)
{
Console.Beep();
if (!String.IsNullOrWhiteSpace(textbox.Text) &&
!String.IsNullOrEmpty(textbox.Text))
{
isThereEmptyTextBox = true;
}
}
}
SaveToDatabaseButton.Enabled = !isThereEmptyTextBox;
}
As soon as just one text box is empty that flag will be set to true and will stay that way whether more empty text boxes appear or not, then you use that flag to enable/disable the button.

Double press of a button c#

I am struggling to make a button to be pressed multiple times with different outcomes. I have a few buttons to add some strings to a list. The problem is that I want to press the button to add an item and, if I press it again, to delete the same item.
private void labelPineapple_Click(object sender, EventArgs e)
{
if (!My_Pizza.Items.Contains(pineapple))
{
My_Pizza.Items.Add(pineapple);
labelPineapple.BackColor = Color.Green;
}
}
You want some kind of toggle behavior, we have this "toggle" behavior when the next click "denies" the last one. If I had some more details on if you have a specific button for each item or if you're a selection a item in a list and then pressing the button, I'd be more precise.
If you're selecting an item and then clicking "remove", you can do something like this:
private void DeleteItem_Click(object sender, EventArgs e)
{
listBox1.SelectedItems.Remove(listBox1.SelectedItems);
}
If somehow you're using the same button to remove the last value you added, you can use a local variable to store the old value like in:
private void labelPineapple_Click(object sender, EventArgs e)
{
if (!My_Pizza.Items.Contains(pineapple))
{
My_Pizza.Items.Add(pineapple);
labelPineapple.BackColor = Color.Green;
_oldValue = pineapple;
}
else
{
My_Pizza.Items.Remove(_oldValue);
}
}
If the same button will always and only add/remove the same item, use a toggle button instead.
Sometimes we developers try to solve simple things with harder approaches, when things get too complex, try to write down what you're trying to achieve and the possible solutions.
Update: if you have the pineapple object at the moment you're removing it, you don't need to store it as _oldValue. You can remove it directly inside your else statement.

If PictureBox Clicked Event

I'm trying to program a matching game. My idea is:
(1). When a certain pictureBox1 is clicked, it becomes invisible
(2). A MessageBox shows up, prompting "Pick another box."
(3). Finally, I need to program an if/else statement where if pictureBox13 is clicked it becomes invisible; else, (if another pictureBox is clicked) a MessageBox prompts "Try again." And both picture boxes become invisible, but I don't know what I am doing wrong:
// Program From Below
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Now Pick Another Hidden Picture!");
pictureBox1.Visible = false;
if (pictureBox13_Click)
{
MessageBox.Show("Great!");
pictureBox13.Visible = false;
}
else
{
MessageBox.Show("Try Again!");
}
}
There is a red squiggly line under if (pictureBox13_Click)
It would be better if every PictureBox had it's a state, that you would then manipulate using a Click_Event. Microsoft has a comprehensive tutorial for a matching game here: https://msdn.microsoft.com/en-us/library/dd553235.aspx
As other suggested, you can use same event handler for all your pictureBoxes and cast sender to PictureBox to see what PB was clicked :
List<string> selectedPictureBoxes;
public MyForm() // ctor
{
selectedPictureBoxes = new List<string>();
foreach(Control c in this.Controls)
if(c is PictureBox) c.Click += pictureBox_Click;
}
private void pictureBox_Click(object sender, EventArgs e)
{
PictureBox _clicked = sender as PictureBox;
if(!selectedPictureBoxes.Contains(_clicked.Name))
selectedPictureBoxes.Add(_clicked.Name);
else ....
}
You could create an int for the selected boxes (in this example, box1 and box2) which are both set to 0 and then create an on click event which sets the int to the clicked box.
if(box1 != 0)
{
box2 = 'insert selected box number'
}
else
{
box1 = 'insert selected box number'
}
After two boxes have been selected, both integers can be set to false, this allows for you to use a switch instead of if, which could shorten the code substantially if a separate if statement is required for each pair of pictures.

User input validation with a foreach loop in form constructor

I'm trying to use this custom method for user input validation in text boxes. But I feel something missing in this approach. Now use cant move to next text box if the validation failed. Is this a good thing or bad thing to do?
private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox currenttb = (TextBox)sender;
if (currenttb.Text == "")
{
MessageBox.Show(string.Format("Empty field {0 }", currenttb.Name.Substring(3)));
e.Cancel = true ;
}
else
{
e.Cancel = false;
}
}
Adding the handler to the textboxes with a foreach loop in the form constructor:
foreach(TextBox tb in this.Controls.OfType<TextBox>().Where(x => x.CausesValidation == true))
{
tb.Validating += textBox_Validating;
}
How about using Error Provider, it will display exclamation if validation fail
I thought it would be a bad user experience if you popup too much message box. May be you should consider use labels to display error message beside each text box. In your programm, I can't even close the window by clicking the close button if I left the text box empty.

C# Switch-case Loop for Datagridview cells

I am working on a form with datagridview and webbrowser controls. I have three columns as URL, username and password in datagridview. What I want to do is to automate the login for some websites that I use frequently. For that reason I am not sure if this is the right approach but I created the below code. The problem is with the argument of switch.
I will click the row on datagridview and then click the login_button so that the username and password info will be passed to the related fields on the webpage. Why I need a switch-case loop is because all the webpages have different element IDs for username and password fields.
As I said, I am not sure if datagridview allows switch-case, I searched the net but couldn't find any samples.
private void login_button_Click(object sender, EventArgs e)
{
switch (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString())
{
case "http://www.website1.com":
webBrowser1.Document.GetElementById("username").InnerText = dataGridView1.Rows[3].Cells[3].Value.ToString();
webBrowser1.Document.GetElementById("password").InnerText = dataGridView1.Rows[3].Cells[4].Value.ToString();
return;
case "http://www.website2.com":
webBrowser1.Document.GetElementById("uname").InnerText = dataGridView1.Rows[4].Cells[3].Value.ToString();
webBrowser1.Document.GetElementById("pswd").InnerText = dataGridView1.Rows[4].Cells[4].Value.ToString();
return;
}
HtmlElementCollection elements = this.webBrowser1.Document.GetElementsByTagName("Form");
foreach (HtmlElement currentElement in elements)
{
currentElement.InvokeMember("Login");
}
}
I'm not sure if it's definite like this in C#, but you may have to perform the switch on a temporary variable
e.g.
string site = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
switch(site)
{
....
}
if nothing else it will make debugging easier.
also each case should end with a break; not a return;
Have you made sure that
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
returns the URL?
That would be the first place to start.
Maybe:
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString()
As for the datagrid + switch, the datagrid doesn't know/care about the switch. All it knows that when the button is pressed call:
private void login_button_Click(object sender, EventArgs e)
What happens if you hardcode the URL? Does everything else work?
EDIT:
D'oh that isn't going to work!
You have returns in your switch. So you are ending the method and never calling:
HtmlElementCollection elements = this.webBrowser1.Document.GetElementsByTagName("Form");
foreach (HtmlElement currentElement in elements)
{
currentElement.InvokeMember("Login");
}

Categories