I have a ListBox with words and I need to click a button that opens an InputBox where I can search for a word and the program will run the ListBox and highlight the word I wrote in the InputBox if it's there. If the program reaches the end of the list and doesn't find the word then I'll get a MessageBox saying the word I'm looking for isn't there. I need to use some sort of cycle for this program.
I know how to make the button, InputBox and the error MessageBox, but I don't know how to do the searching and cycle.
I've read a lot of similar questions here but I don't think any of them return the result I'm looking for.
Can anyone help me? Or redirect me to a post with the answer?
This is for Winforms.
That should get you on track, it's pretty much self-explanative:
whenever text changes
find matching items in list
select them
Code:
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBox = sender as TextBox ?? throw new InvalidOperationException();
var text = textBox.Text;
if (string.IsNullOrWhiteSpace(text))
return; // nothing to search for
const StringComparison comparison = StringComparison.InvariantCultureIgnoreCase; // maybe change this
// find items matching text
var indices = new List<int>();
for (var i = 0; i < listBox1.Items.Count; i++)
{
var item = listBox1.Items[i];
if (string.Equals(item?.ToString(), text, comparison))
indices.Add(i);
}
// select them in list
if (!indices.Any())
return;
listBox1.SelectedIndices.Clear();
foreach (var index in indices)
listBox1.SelectedIndices.Add(index);
}
Of course, list selection mode has to be multiple for it to work properly.
Also, you will need to clear selection if there are no matches so as to not leave the UI in an ambiguous state (not done).
I am trying to create a foreach loop for a listview to detect which items are selected (multiselection is on) and then concatenate the item's text to a string to pass to a second page with the frame.navigate method.
I've googled and several solutions on here suggest using the listview.CheckedItems or .SelectedItems, some older ones referenced .IsSelected or .IsChecked. none of these seem to be valid in the Visual Studios version I'm using. I've used the updater to ensure I have the most recent version Visual Studios.
To be clear: I'm not asking for someone to rewrite my homework, but to find out what the working replacement for listview.checkeditems is, or if this simply no longer works what should I be using instead of a listview for the user to select items that I can detect.
Here are the instructions to my homework.
Create two arrays ( 20 elements). The first array will contain all of the items available including a Distinguisher between breakfast, lunch and dinner, the item description and the price. Populate this array though a loaded event method.
On the first page, provide three buttons to choose breakfast, lunch or dinner. When the user clicks a button, have the items for that meal description( no price) appear in a listview box on top portion of the page and also record them in the second array.
Add a checkout button to the page. When clicked go to a second page.
On the second page, display the items selected ( already stored in second array) including price in a list view box. Provide the total of cost of the items.
Everything up to the checkout button works. I've populated the array from a text file, the meal buttons load the appropriate foods into the listview and I know how to pass strings with Frame.Navigate but if I can't detect what Listview items are checked I can't pass them!
I have done a LOT of google searching to find a solution, we have no textbook for this class and the teachers answer is to re-watch some tutorial videos (half of which are for windows 8 phones and are no longer entirely correct).
Here is the snippet of code for my checkout button
private void CheckoutButton_Click(object sender, RoutedEventArgs e)
{
foreach (ListViewItem eachItem in MenuList.CheckedItems)
{
checkedMenuItems = checkedMenuItems + "/" + MenuList.Items.ToString();
}
}//end CheckoutButton_Click
The error returned for CheckedItems is
'ListView' does not contain a definition for 'CheckedItems' and no extension method 'CheckedItems' accepting a first argument of type 'ListView' could be found (are you missing a using directive or an assembly reference?)
The same error is returned for SelectedItems and no solutions are suggested by Visual Studios.
I am hoping that someone can help!
Thanks.
I'm adding the code for the whole MainPAge in case it is relevant. It references class (breakfast, lunch, dinner) in a separate .cs file.
public sealed partial class MainPage : Page
{
public static meal[] menuItems = new meal[20];
public static meal[] menuChoices = new meal[20];
public string checkedMenuItems;
public MainPage()
{
this.InitializeComponent();
Loaded += PopulateArray;
}
public async void PopulateArray(object sender, RoutedEventArgs e)
{
int counter = 0;
char switchCheck;
var file = await StorageFile.GetFileFromApplicationUriAsync(new
Uri("ms-appx:///MenuItems.txt"));
using (var inputStream = await file.OpenReadAsync())
using (var classicStream = inputStream.AsStreamForRead())
using (var streamReader = new StreamReader(classicStream))
{
while (streamReader.Peek() >= 0)
{
string itemIn = string.Format(streamReader.ReadLine());
string[] item = itemIn.Split('/');
switchCheck = Convert.ToChar(item[0]);
switch (switchCheck)
{
case 'b':
menuItems[counter] = new
breakfast(Convert.ToChar(item[0]), item[1], Convert.ToDecimal(item[2]));
// MenuList.Items.Add("works item# " + counter +
menuItems[counter].getMealType());
break;
case 'l':
menuItems[counter] = new
lunch(Convert.ToChar(item[0]), item[1], Convert.ToDecimal(item[2]));
break;
case 'd':
menuItems[counter] = new
dinner(Convert.ToChar(item[0]), item[1], Convert.ToDecimal(item[2]));
break;
}//end switch
counter++;
}//end while`
}
}//end populateArray method
private void BreakfastButton_Click(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
for (int i = 0; i < menuItems.Length && menuItems[i] != null; i++)
{
if (menuItems[i].getMealType() == 'b')
MenuList.Items.Add(menuItems[i].getMealDesc());
}//end for loop
}//end BreakfastButton_Click
private void LunchButton_Click(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
for (int i = 0; i < menuItems.Length && menuItems[i] != null; i++)
{
if (menuItems[i].getMealType() == 'l')
MenuList.Items.Add(menuItems[i].getMealDesc());
}//end for loop
}//end LunchButton_Click
private void DinnerButton_Click(object sender, RoutedEventArgs e)
{
MenuList.Items.Clear();
for (int i = 0; i < menuItems.Length && menuItems[i] != null; i++)
{
if (menuItems[i].getMealType() == 'd')
MenuList.Items.Add(menuItems[i].getMealDesc());
}//end for loop
}//end DinnerButton_Click
private void CheckoutButton_Click(object sender, RoutedEventArgs e)
{
foreach (ListViewItem eachItem in MenuList.CheckedItems)
{
checkedMenuItems = checkedMenuItems + "/" +
MenuList.Items.ToString();
}
}//end CheckoutButton_Click
}//end mainpage
From your description I could not clearly identify what type your object MenuList is.
According to the reference you could use the SelectRange method to solve this problem.
This might link help you: ListViewBase.SelectedItems Property
It seems there is a similar issue here, you can refer my answer there.
C# UWP Passing Data From One ListView To A Second Listview on the Next Page in a Array
I am writing a c# Html editor application, in which you type the code in a RichTextBox control. I want the RichTextBox to behave like notepad++ and other code editors in which the Html syntax gets highlighted in colors, like this for example:
How can I establish this in C# windows form RichTextBox? I have searched almost everywhere and didn't find anything that helped me. This is what I tried so far but I doesn't give the result I want:
private void SyntaxHighlight()
{
string[] tags = { "html","head","body","a","b","img","strong","p","h1","h2","h3","h4","h5","h6","embed","iframe","span","form",
"button","input","textarea","br","div","style","script","table","tr","td","th","i","u","link","meta","title"};
foreach (string s in tags)
{
richTextBox1.Find("<" + s);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.Find(">");
richTextBox1.SelectionColor = Color.Blue;
}
string[] attributes = { "href","src","height","width","rowspan","colspan","target","style","onclick","id","name","class"};
foreach (string s in attributes)
{
richTextBox1.Find(s + "=");
richTextBox1.SelectionColor = Color.Red;
}
}
Can someone help me? What should I write inside the SyntaxHighlight() method? can someone give me the appropriate code?
Thank you
Within your code you are only finding the 1st occurrence of the HTML tag and highlighting it. But instead, you should loop through the entire rich text content to finding proceeding occurrences of the same text. I just did a quick mock based on your exact code, please check it out.
private void highlightHTMLText()
{
string[] tags = { "html","head","body","a","b","img","strong","p","h1","h2","h3","h4","h5","h6","embed","iframe","span","form",
"button","input","textarea","br","div","style","script","table","tr","td","th","i","u","link","meta","title"};
foreach (string s in tags)
{
findAndHighlight("<" + s, Color.Blue);
findAndHighlight("</" + s, Color.Blue);
findAndHighlight(">", Color.Blue);
}
string[] attributes = { "href", "src", "height", "width", "rowspan", "colspan", "target", "style", "onclick", "id", "name", "class" };
foreach (string s in attributes)
{
findAndHighlight(s + "=", Color.Red);
}
}
private void findAndHighlight(string sSearchStr, Color oColor)
{
int index = richTextBox1.Text.IndexOf(sSearchStr);
while (index != -1)
{
richTextBox1.Select(index, sSearchStr.Length);
richTextBox1.SelectionColor = oColor;
index = richTextBox1.Text.IndexOf(sSearchStr, index + sSearchStr.Length);
}
}
Further as per this answer you should be able to make use of the same utility library Scintilla used by Notepad++ itself. As pointed out you do not need to re-invent the wheel, but as a developer I obviously prefer my own util (it is just me ;) ). Hope this helps.
Find doesn't move a cursor, it returns the location of the first match. Try this instead:
How to select text from the RichTextBox and then color it?
A little late to the party but, after wanting to create my own offline version of CodePen, I implemented my own version of html syntax highlighting following CodePen's theme.
This does syntax highlighting and markup formatting, though the formatting depends on whether or not your html is well-formed.
Just add this as a class for your RichTextBox, instantiate it accordingly and call it within whichever event works for you (I'm using it with the RTB's double_click event but that does eliminate double-click text selection). What I'm planning to do is add a timer, some boolean variables and work this within the key_up and key_down events to set the highlight update to be a bit more automatic and less intrusive on shortcuts. (which is hereby included below the class)
public void HighlightHTM(RichTextBox Htm_Input)
{
Htm_Input.Visible = false;
#region store the original caret position + forecolor
int originalIndex = Htm_Input.SelectionStart;
int originalLength = Htm_Input.SelectionLength;
Color originalColor = Color.FromArgb(200, 200, 200); // Grey
#endregion
#region try to format the markup
try { Htm_Input.Text = XElement.Parse(Htm_Input.Text).ToString(); } catch { }
#endregion
#region match everything but puncutation and equals
Regex e = new Regex(#"(.*?|=)[^\w\s]");
MatchCollection eMatches = e.Matches(Htm_Input.Text);
foreach (Match m in eMatches)
{
Htm_Input.SelectionStart = m.Groups[1].Index;
Htm_Input.SelectionLength = m.Groups[1].Length;
Htm_Input.SelectionColor = Color.FromArgb(221, 202, 126); // Yellow
}
#endregion
#region match tags
Regex t = new Regex(#"(<\w+|</\w+|/>|>)[^=]");
MatchCollection tMatches = t.Matches(Htm_Input.Text, 0);
foreach (Match m in tMatches)
{
Htm_Input.SelectionStart = m.Groups[1].Index;
Htm_Input.SelectionLength = m.Groups[1].Length;
Htm_Input.SelectionColor = Color.FromArgb(167, 146, 90); // Brown
}
#endregion
#region match quotes
Regex q = new Regex("\".*?\"");
MatchCollection qMatches = q.Matches(Htm_Input.Text);
foreach (Match m in qMatches)
{
Htm_Input.SelectionStart = m.Index;
Htm_Input.SelectionLength = m.Length;
Htm_Input.SelectionColor = Color.FromArgb(150, 179, 138); // Green
}
#endregion
#region match inner html
Regex h = new Regex(">(.+?)<");
MatchCollection hMatches = h.Matches(Htm_Input.Text);
foreach (Match m in hMatches)
{
Htm_Input.SelectionStart = m.Groups[1].Index;
Htm_Input.SelectionLength = m.Groups[1].Length;
Htm_Input.SelectionColor = Color.FromArgb(200, 200, 200); // Grey
}
#endregion
#region restoring the original colors, for further writing
Htm_Input.SelectionStart = originalIndex;
Htm_Input.SelectionLength = originalLength;
Htm_Input.SelectionColor = originalColor; // Light Grey
#endregion
Htm_Input.Focus();
Htm_Input.Visible = true;
}
Happy coding!
Edit: I should also mention that !doctype breaks formatting as it's not exactly xml-friendly in the context of "well-formed". For my purposes, all tags including body and relevant closings, css and js links are added programmatically at page save so only markup within the body tags are worked with inside the html RTB. This eliminates that problem.
You'll notice that this relies exclusively on Regex rather than on hard-coded tags and properties. I did this because tags and properties have a tendency to pop on and off the w3 scene quite often. That would force a dev to continually have to go back and edit those strings to remove deprecated tags / properties or to add new. Not optimal.
I also thought it prudent to go ahead and include the instantiation / usage examples to make this a bit more plug&play.
Above public Main(), instantiate like so:
#region Class Instantiation
SyntaxHighlight syntax = new SyntaxHighlight();
#endregion
... and, within your chosen event handler, call it like so:
private void htm_input_DoubleClick(object sender, EventArgs e)
{
syntax.HighlightHTM(Htm_Input);
}
Naturally, adding a SaveFileDialog and an OpenFileDialog pretty much provides this the functionality of your very own, albeit very basic, html editor. Toss in a WebBrowser control and apply the RTB's text as the WebBrowser's source and you've upgraded to live-view.
In the very least, this should serve as a viable reference for syntax highlighting in general. It really just boils down to identifying patterns and manipulating their colors so, for example, this will work effectively with css, javascript and even C# with some light adjusting of the pattern identification parameters.
The following is how I setup the automatic refresh with key_up / key_down and a timer set to 1000 ms:
#region Globals
int r = 0;
bool refresh = false;
#endregion
private void Htm_Input_KeyUp(object sender, KeyEventArgs e)
{
refresh = true; // enter refresh cycle
}
private void Htm_Input_KeyDown(object sender, KeyEventArgs e)
{
refresh = false; // abort refresh cycle
}
private void Timer_Refresh_Tick(object sender, EventArgs e)
{
// check if refresh cycle is entered, refresh at 3 seconds or reset the counter if aborted
if (refresh) { if (r == 3) { syntax.HighlightHTM(Htm_Input); refresh = false; r = 0; } r++; } else { r = 0; }
}
I am developing one Windows phone app. In my app I want to get the latest entered word in textbox not the last word. And I want to change that latest entered word on space key pressed. I am getting the last word on a key up event like this:
private async void mytxt_KeyUp_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Space || e.Key == Windows.System.VirtualKey.Enter)
{
if (string.IsNullOrWhiteSpace(textBox_string) == false)
{
string[] last_words = Regex.Split(textBox_string, #"\s+");
int i = last_words.Count();
last_words = last_words.Where(x => x != last_words[i-1]).ToArray(); last_word = last_words[last_words.Count() - 1];
last_word = last_word.TrimStart();
}
}
}
I am getting the last word by this method but actually I want to get latest entered word by user. Meaning, if the user moves the cursor directly to the middle of textbox and types any word then I want to get that word on space key pressed event; I want the position of that word and can change that word programmatically and update textbox.
For example, if the user types
H!! my name vanani
but then the user moves the cursor directly after 'name' and types 'is sohan'
H!! my name is sohan
then I want to get word and position of 'is' and same for 'sohan' in key up event of textbox. I need the position to replace that word with another word and update textbox with the new replaced text.
I have seen these questions. winforms - get last word.. and C# how to get latest char.. but they didn't help me. Please help me.
Like this:
if (Regex.IsMatch(textBox_string, #"\S*(?=\s?$)"))
{
Match match = Regex.Match(textBox_string, #"\S*(?=\s?$)");
string word = match.Value;
int startingIndex = match.Index;
int length = word.Length;
}
I founded answer to my question.
Here is code worked for me.
Bool isFirst = false;
int mytempindex;
private async void mytxt_KeyUp_1(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Space)
{
int i = mytxt.SelectionStart;
if (i < mytxt.Text.Length)
{
if (isfirst == false)
{
mytempindex = mytxt.SelectionStart;
isfirst = true;
}
else
{
int mycurrent_index = mytxt.SelectionStart;
int templength_index = mycurrent_index - mytempindex;
string word = mytxt.Text.Substring(mytempindex, templength_index); //It is the latest entered word.
//work with your last word.
}
}
}
}
I don't think it work in all situation but through this you can get idea about how to get latest entered word from Textbox or RichTextbox.
I got an userControl that contains a searchBox.
This UserControl is inside another one.
I got a strange behavior while i'm searching, because the suggestionCollection works in a strange way.
Example :
in the searchBox i write something all works perfectly, if i choose the item it also works.
But if i try to use backspace (after the choose) i got no suggestion.
I cannot understand why it doesn't work.
That's the code
//var deferral = args.Request.GetDeferral(); //it seems to not influence the behavior
var suggestionCollection = args.Request.SearchSuggestionCollection;
try
{
TransporterExt tr_search = new TransporterExt();
//queryText is a string inserted in the searchBox
if (string.IsNullOrEmpty(queryText)) return;
tr_search.name = queryText;
suggested.Clear(); //that's a collection..
//just a search that return a collection of objects TransporterExt
querySuggestions = await TransporterService.Search_StartsWith(tr_search);
if (querySuggestions.Count > 0)
{
int i = 0;
foreach (TransporterExt tr in querySuggestions)
{
string name = tr.name;
string detail = tr.trId.ToString();
string tag = i.ToString();
string imageAlternate = "imgDesc";
suggestionCollection.AppendResultSuggestion(name, detail, tag, imgRef, imageAlternate);
this.suggested.Add(tr);
i++;
}
}
}
catch (System.ArgumentException exc)
{
//Ignore any exceptions that occur trying to find search suggestions.
Debug.WriteLine(exc.Message);
Debug.WriteLine(exc.StackTrace);
}
//deferralComplete(); //it seems to not influence the behavior
The problem is that: all the variables have the right value, but the suggestion panel appears only if i make a particular search: it appears when i change the first letter of search, or after an wrong seach
What appends when i make a search
What appends if i use the backspace, and i what i want to fix
As i said, all works perfectly, after the "backspace" action the suggestionCollection got the right value...but the panel is missing.
Could someone help me?
You can use SearchBox and SuggestionRequested event to fire the event when type on the SearchBox. I will show an Example
<SearchBox x:Name="SearchBoxSuggestions" SuggestionsRequested="SearchBoxEventsSuggestionsRequested"/>
and write the SearchBoxEventsSuggestionsRequested handler in the code behind
private void SearchBoxEventsSuggestionsRequested(object sender, SearchBoxSuggestionsRequestedEventArgs e)
{
string queryText = e.QueryText;
if (!string.IsNullOrEmpty(queryText))
{
Windows.ApplicationModel.Search.SearchSuggestionCollection suggestionCollection = e.Request.SearchSuggestionCollection;
foreach (string suggestion in SuggestionList)
{
if (suggestion.StartsWith(queryText, StringComparison.CurrentCultureIgnoreCase))
{
suggestionCollection.AppendQuerySuggestion(suggestion);
}
}
}
}
You can add the keyword to SuggestioList, and it will show in the dropdown when you type on the Searchbox.
Create the SuggestionList
public List<string> SuggestionList { get; set; }
initialize the list
SuggestionList = new List<string>();
and add keywords to the list
SuggestionList.Add("suggestion1");
SuggestionList.Add("suggestion2");
SuggestionList.Add("suggestion3");
SuggestionList.Add("suggestion4");
SuggestionList.Add("Fruits");
Thanks.