I am creating a tool in Visual C#.Net. The algorithm of the tool is to check for all space/s before/after a parenthesis and create an error message for the found errors.
For example: input is ( Text )
Error will be raise because space before and after the parenthesis is detected.
If errors are found the code will add the errors in listview1.items().
To make my question much clearer for you here's my code:
private void button1_Click(object sender, EventArgs e)
{
int error_counter = 0;
listView1.Items.Clear();
//requirement 8c
//check for a space in open and close parenthesis
Regex test = new Regex(#"\(\s.+\s\)|\[\s.+\s\]|\{\s.+\s\}", RegexOptions.IgnoreCase);
MatchCollection matchlist = test.Matches(richTextbox1.Text);
if (matchlist.Count > 0)
{
for (int i = 0; i < matchlist.Count; i++)
{
Match firstMatch = matchlist[i];
string firstMatch_string = firstMatch.ToString();
string[] errors = new string[matchlist.Count];
errors[i] = "Ommit Space between a bracket";
listView1.Items.Add(errors[i]);
error_counter++;
}
}
}
private void listView1_ItemActivate(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
MessageBox.Show(item.ToString());
}
}
What I looking for is that all of the items of my listview1 will be clickable, and after a click was made by the user the tool will highlight the error found in the richtextbox1.
Thanks for all your help guys!
As someone already told you, use the Index and Length properties of the Match class. Here's a short example implementing a weird textbox selection strategy. But it works effectively demonstrating the concept:
public partial class Form1 : Form
{
List<Error> errors = new List<Error>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
errors = new List<Error>();
listView1.Items.Clear();
foreach(Match m in Regex.Matches(richTextBox1.Text, #"(\(\s+|\s+\)|\[\s+|\s+\]|\{\s+|\s+\})", RegexOptions.IgnoreCase))
{
//you may decide to differentiate the msg according to the specific problem
Error error = new Error(m, "Ommit Space between a bracket");
this.errors.Add(error);
listView1.Items.Add(error.msg);
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listView1.SelectedIndices.Count > 0)
{
Error error = errors[listView1.SelectedIndices[0]];
Select(richTextBox1, error);
}
}
private static void Select(RichTextBox rtb, Error e) {
string o = rtb.Text;
rtb.Clear();
for (int i = 0; i < o.Length; i++)
{
if (i >= e.index && i <= e.index + e.length)
{
rtb.SelectionColor = Color.White;
rtb.SelectionBackColor = Color.Red;
}
else
{
rtb.SelectionColor = Color.Black;
rtb.SelectionBackColor = Color.White;
}
rtb.AppendText(o[i].ToString());
}
}
}
public class Error
{
public int index;
public int length;
public string value;
public string msg;
public Error(Match m, string msg)
{
this.index = m.Index;
this.length = m.Length;
this.value = m.Value;
this.msg = msg;
}
}
The Match object (like firstMatch) has two usefull properties here : Index and Length.
They give you the position and the length of the match in question in the original text.
With that in your knowledge, you just have to implement the highlight in the richTextBox !
Related
in this program, when the Recall button (recallBtn_Click()) is clicked, it calls a method (that calculates directions) from another class which should then call the showPath() method. the show path method should then display its output in a textBox. But the values don't show even though i can see from debugging that the values are being sent to the text box. can anybody tell me where i went wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
storeRetSelect.SelectedIndex = 0;
PrioritySelect.SelectedIndex = 0;
}
public void showPath(List<PathFinderNode> mPath)
{
var T = new Form1().directionsTextBox;
foreach (PathFinderNode node in mPath)
{
if ((node.X - node.PX) > 0) { T.Text += "Right" + System.Environment.NewLine ; }
if ((node.X - node.PX) < 0) { T.Text += "Left" + System.Environment.NewLine; }
if ((node.Y - node.PY) > 0) { T.Text += "UP" + System.Environment.NewLine; }
if ((node.Y - node.PY) < 0) { T.Text += "Down" + System.Environment.NewLine; }
}
}
private void recallBtn_Click(object sender, EventArgs e)
{
var path = new pathPlan();
string desigString = inputTextBox.Text;
int[] desig = new int[3];
for (int i = 0; i < desigString.Length; i++) { desig[i] = (int)char.GetNumericValue(desigString[i]); }
path.Recall(desig[1], desig[2], (-1) * desig[0]);
}
}
With this line you are initialising a new object and get the reference of the textbox there.
var T = new Form1().directionsTextBox;
But I assume you want to use the textbox of the form which is allready open. Change the line to the following to access the textbox of the current object.
var T = this.directionsTextBox;
I have two classes that get all the minimized windows:
The first class is WindowSnap.cs:
WindowSnap.cs
The second class is WindowSnapCollection.cs:
WindowSnapCollection.cs
In the first class in the windowSnap there is a method called GetAllWindows:
public static WindowSnapCollection GetAllWindows(bool minimized, bool specialCapturring)
{
windowSnaps = new WindowSnapCollection();
countMinimizedWindows = minimized;//set minimized flag capture
useSpecialCapturing = specialCapturring;//set specialcapturing flag
EnumWindowsCallbackHandler callback = new EnumWindowsCallbackHandler(EnumWindowsCallback);
EnumWindows(callback, IntPtr.Zero);
return new WindowSnapCollection(windowSnaps.ToArray(), true);
}
In the end i used a breakpoint on the return line and windowSnaps variable contain 15 items.
For example the first item is:
[0] = {Window Text: , Handle: 31918532}
The second one is:
[3] = {Window Text: ?How can i get from a window only the text without the Handle number ? - Stack Overflow - Google Chrome?, Handle: 64424060}
In the first item i was able to do on my own to remove this item since it's text is only "," and this window show nothing.
But number [3] and other items i want first to remove from them the part Handle: 64424060
I don't need that the user will see this information. But only the text so for example number [3] should look like:
?How can i get from a window only the text without the Handle number ? - Stack Overflow - Google Chrome?
And to fix those "?" that are in the beginning of the line.
But the idea is to display only the name of each window and not the whole text with the handle number.
This is how i add the items(windows) to form1 listBox:
this.listBoxSnap.Items.AddRange(WindowSnap.GetAllWindows(true, true).ToArray());
In the listBox i want to see the items names or description/titles not the whole text with the handle number.
You can do a little trick. It is not nice but works. Add a new listbox eg listBox1 with the same size on top of listBoxSnap.
add in public Form1():
public Form1()
{
...
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
buttonSnap.Enabled = true;
//add here ->
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList [i] = tt.Substring(0, index);
}
listBox1.Items.AddRange(myList);
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
this.listBox1.SetSelected(0, true);
}
listBoxSnap.Select();
listBox1.Select();
}
change private void listBoxSnap_SelectedIndexChanged(object sender, EventArgs e) to:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
listBoxSnap.SelectedIndex = listBox1.SelectedIndex;
drawpicbox(this.listBoxSnap.SelectedIndex);
}
and delete this.listBoxSnap.SelectedIndexChanged += new System.EventHandler(this.listBoxSnap_SelectedIndexChanged); from Form1.Designer.cs
Add this event:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var item = listBox1.IndexFromPoint(e.Location);
if (item >= 0)
{
listBox1.SelectedIndex = item;
listBoxSnap.SelectedIndex = listBox1.SelectedIndex;
cm.Show(listBox1, e.Location);
}
}
}
And lastly add in private void RefreshWindowsList()
private void RefreshWindowsList()
{
...
for (int i = listBoxSnap.Items.Count - 1; i >= 0; i--)
{
string tt = listBoxSnap.Items[i].ToString();
if (tt.Contains(" ,"))
{
listBoxSnap.Items.RemoveAt(i);
}
}
//add here ->
string[] myList = new string[listBoxSnap.Items.Count];
for (int i = 0; i < listBoxSnap.Items.Count; i++)
{
string tt = listBoxSnap.Items[i].ToString();
int index = tt.LastIndexOf(",");
myList [i] = tt.Substring(0, index);
}
listBox1.Items.Clear();
listBox1.Items.AddRange(myList);
rectangles = new Rectangle[listBoxSnap.Items.Count];
isCropped = new bool[listBoxSnap.Items.Count];
textBoxIndex.Text = listBoxSnap.Items.Count.ToString();
if (this.listBoxSnap.Items.Count > 0)
{
this.listBoxSnap.SetSelected(0, true);
this.listBox1.SetSelected(0, true);
}
listBoxSnap.Select();
listBox1.Select();
}
Valter
I am currently doing a simili-HangMan project. As I looked through many other projects up here, I haven't found what I was looking for exactly.
Notes:
* The variable motRechercher is the randomized word.
* It can be used everywhere - I did a get set for it.
MY QUESTION IS: I want to display a string in a textbox that is a random word selected from a list, how do I do that?
Here's my code for the textbox:
private void txtMot_TextChanged(object sender, TextChangedEventArgs e)
{
for (int i = 0; i <= motRechercher.Length; i++)
{
StringBuilder sb = new StringBuilder(motRechercher);
sb[i] = '_';
string sba = sb.ToString();
txtMot.Text=sba;
}
}
If the word is for an example : Cat. It should display: _ _ _
Here's my code for the random word selector (It works) - It's mostly to give an idea:
private void btnDemarrer_Click(object sender, RoutedEventArgs e)
{
Random rdn = new Random();
int nbreAleatoire = rdn.Next(0, 27);
motRechercher = lesMots[nbreAleatoire];
}
If you have any questions regarding my code I'll edit it to make it easier for you to understand/help me.
instead of
private void txtMot_TextChanged(object sender, TextChangedEventArgs e)
{
for (int i = 0; i <= motRechercher.Length; i++)
{
StringBuilder sb = new StringBuilder(motRechercher);
sb[i] = '_';
string sba = sb.ToString();
txtMot.Text=sba;
}
}
add another button for next random no to populate to text box.
inside button click do this which will check the length and get the data for you:
private void btnNext_Click(object sender, RoutedEventArgs e)
{
if(motRechercher.Length > 0)
{
String str = new String('_', motRechercher.Length);
txtMot.Text = str;
}
}
If I understand the question, this might be what you're after:
bool changing = false; // variable in class-scope
private void txtMot_TextChanged(object sender, TextChangedEventArgs e)
{
if (changing == false)
{
try
{
changing = true;
String str = new String('_', motRechercher.Length);
txtMot.Text = str;
}
finally
{
changing = false;
}
}
}
Problem solved.
The original "private void buttonSave_Click" was changed to:
private void buttonSave_Click(object sender, EventArgs e)
{
if (MusicCollection.FormMain.PublicVars.AlbumList.Count != 100)
{
MusicCollection.FormMain.PublicVars.AlbumList.Add(new Album(NameTextBox.Text));
MessageBox.Show("New Album added: " + NameTextBox.Text);
formMain.ListAlbums(formMain.AlbumsListBox.Items);
this.Close();
}
else
{
MessageBox.Show("No room for new album.");
this.Close();
}
}
Original Post:
I'm new to using C#, so appologies for any seemly obvious mistakes or terrible coding.
I'm trying to create a new Album object (that gets its Name from NameTextBox.Text on Form FormAlbumAC) and add it to List AlbumList when the user clicks the save button on FormAlbumAC. Then I want to list all of AlbumList in a ListBox on Form FormMain.
When I run the program and click the save button, I'm getting the error "ArgumentOutOfRangeException was unhandled, Index was out of range" at the line:
if (MusicCollection.FormMain.PublicVars.AlbumList[i] == null)
// line 8 on my excerpt from Form FormAblumAC
I'm not sure what I'm doing wrong. Any help would be much appreciated, thank you.
Form FormMain:
public const int MAX_ALBUMS = 100;
public int totalAlbums = 0;
public FormMain()
{
InitializeComponent();
}
public static class PublicVars
{
public static List<Album> AlbumList { get; set; }
static PublicVars()
{
AlbumList = new List<Album>(MAX_ALBUMS);
}
}
public ListBox AlbumListBox
{
get
{
return AlbumListBox;
}
}
public void ListAlbums(IList list)
{
list.Clear();
foreach (var album in PublicVars.AlbumList)
{
if (album == null)
continue;
list.Add(album.Name);
}
}
Form FormAlbumAC:
private FormMain formMain;
private void buttonSave_Click(object sender, EventArgs e)
{
int index = -1;
for (int i = 0; i < MusicCollection.FormMain.MAX_ALBUMS; ++i)
{
if (MusicCollection.FormMain.PublicVars.AlbumList[i] == null)
{
index = i;
break;
}
}
if (index != -1)
{
MusicCollection.FormMain.PublicVars.AlbumList[index] = new Album(NameTextBox.Text);
++formMain.totalAlbums;
MessageBox.Show("New Album added: " + NameTextBox.Text);
formMain.ListAlbums(formMain.AlbumsListBox.Items);
this.Close();
}
else
{
MessageBox.Show("No room for new album.");
this.Close();
}
}
Your problem (from your comments) is that your for loop's condition is incorrect. Your for loop is this:
for (int i = 0; i < MusicCollection.FormMain.MAX_ALBUMS; ++i)
There is one problem and one potential problem here. First, when this code is actually run, it's really running:
for (int i = 0; i < 100; ++i)
because MusicCollection.FormMain.MAX_ALBUMS is declared as 100. This causes an error when the length of MusicCollection.FormMain.PublicVars.AlbumList is less than 100, because you're trying to grab an index that doesn't exist.
Instead, you need to iterate from i=0 to the length of ....PublicVars.AlbumList-1, or, preferably, for(int i = 0; i < ....PublicVars.AlbumList.Count; i++).
The second potential problem is that you are potentially skipping index 0. Arrays start at index zero and continue to index length-1. As such, you probably want i++, not ++i. Depends on your implementation, though.
This may seem like a dumb question. I have a textbox that can be used to add items to a checkedlistbox at runtime on a windows form. I'm using c#. It works perfectly fine at runtime. The item gets added and stuff, when the form is open. But, when I close and open the form again, I don't see the added item in the checkedlistbox list. Note, I don't use a datasource and don't want to. I wouldn't want to hardcode anything and would prefer to use a textbox input on the form as a variable to feed into the collections list. I couldn't figure out a way to expand my checkedlistbox options. Any assistance would be appreciated.
How are you opening the form? Is it something like:
FormName form = new FormName();
form.Show()
The only reason I can think that's happening is that you're instantiating a new form instance every time you show it, instead of reusing the same form.
Have your Form take a ref List<string> values as parameter. Then make this as BindingSource for the CheckedListBox.
Here is the code:
class MyForm : Form {
List<string> values;
BindingSource source;
public MyForm()
{
InitializeComponent();
}
public MyForm(ref List<string> values):this()
{
if (values == null)
values = new List<string>();
this.values = values;
checkedListBox1.DisplayMember = "Value";
checkedListBox1.ValueMember = "Value";
source = new BindingSource(this.values, null);
checkedListBox1.DataSource = source;
}
private void AddItemButton_Click(object sender, EventArgs e)
{
this.source.Add(textBox1.Text);
textBox1.Text = string.Empty;
}
}
private void frmMain_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Properties.Settings.Default.CheckedItems))
{
string[] checkedIndicies = Properties.Settings.Default.CheckedItems.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i1 = 0; i1 < checkedIndicies.Length; i1++)
{
int idx;
if ((int.TryParse(checkedIndicies[i1], out idx)) && (checkedListBox1.Items.Count >= (idx+1)))
{
checkedListBox1.SetItemChecked(idx, true);
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
textBox1.MaxLength = 15;
// Change all text entered to be lowercase.
textBox1.CharacterCasing = CharacterCasing.Lower;
if (checkedListBox1.Items.Contains(textBox1.Text) == false)
{
checkedListBox1.Items.Add(textBox1.Text, CheckState.Checked);
textBox1.Text = "";
MessageBox.Show("Added! Click Move to see List Box");
}
else
{
MessageBox.Show("Already There!");
textBox1.Text = "";
}
}
}
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
string idx = string.Empty;
for (int i1 = 0; i1 < checkedListBox1.CheckedIndices.Count; i1++)
idx += (string.IsNullOrEmpty(idx) ? string.Empty : ",") + Convert.ToString(checkedListBox1.CheckedIndices[i1]);
Properties.Settings.Default.CheckedItems = idx;
Properties.Settings.Default.Save();
}