I have a problem. I'm making a web browser in a C# Windows Form, and I've added bookmarks. My problem? Removing them. I can't seem to get good code that will remove the bookmarks! Here's my code:
private void button2_Click_1(object sender, EventArgs e)
{
int removeBookmarkI = 0;
string removeBookmarkName = Microsoft.VisualBasic.Interaction.InputBox("Which bookmark would you like to delete?", "Delete Bookmark", "Google");
for (int i = 0; i < bookmark_names.Length; i++)
{
if (bookmark_names[i] == removeBookmarkName)
{
removeBookmarkI = i;
string[] holdBookmark_names_temp = new string[bookmark_names.Length];
string[] holdBookmark_url_temp = new string[bookmark_names.Length];
for (int j = i; j < bookmark_names.Length - 1; j++)
{
bookmark_names[j] = bookmark_names[j + 1];
bookmark_url[j] = bookmark_url[j + 1];
}
int skipNum = 0;
for (int j = 0; j < bookmark_names.Length - 1; j++)
{
if (bookmark_names[j] == removeBookmarkName)
{
skipNum = 1;
}
holdBookmark_names_temp[j] = bookmark_names[j + skipNum];
holdBookmark_url_temp[j] = bookmark_url[j + skipNum];
}
bookmark_names = new string[bookmark_names.Length - 1];
bookmark_url = new string[bookmark_url.Length - 1];
for (int j = 0; j < bookmark_names.Length; j++)
{
bookmark_names[j] = holdBookmark_names_temp[j];
bookmark_url[j] = holdBookmark_url_temp[j];
}
this.Controls.Remove(buttonsAdded[removeBookmarkI]);
buttonsAdded.Remove(buttonsAdded[bookmark_names.Length - 1]);
this.Controls.Remove(buttonsAdded[bookmark_names.Length - 1]);
System.IO.File.WriteAllLines("C:\\Users\\Public\\IAmAPerson_Web_Explorer\\bookmarks\\bookmark_names.txt", bookmark_names);
System.IO.File.WriteAllLines("C:\\Users\\Public\\IAmAPerson_Web_Explorer\\bookmarks\\bookmarks.txt", bookmark_url);
bookmark_names = new string[bookmark_names.Length - 1];
bookmark_url = new string[bookmark_url.Length - 1];
bookmark_names = System.IO.File.ReadAllLines(#"C:\Users\Public\IAmAPerson_Web_Explorer\bookmarks\bookmark_names.txt");
bookmark_url = System.IO.File.ReadAllLines(#"C:\Users\Public\IAmAPerson_Web_Explorer\bookmarks\bookmarks.txt");
bookmarkButtonSpacing -= 100;
break;
}
if (i == bookmark_names.Length - 1)
{
System.Windows.Forms.MessageBox.Show("You do not have a bookmark named " + removeBookmarkName + "!");
}
}
}
It should prompt for what bookmark you want to delete, remove it, shift all other bookmarks over, and remove it from the web explorer directory files. What's going on? How can I fix this? I'm using a list for my buttons called buttonsAdded. Also, I am aware that this code is probably the worst looking code in C# EVER, but I'm new, and I have yet to learn the best ways to code.
Related
I have to rewrite the LCS algorithm because some company policies.
I've already get done the LCS algorithm, but next step is to identify which lines were removed from the previous text and which one were added in the current text.
I tried a simple check thought the lines, but it won't work if I got a text with duplicated lines.
He is my code:
LCS Method
private static string[] LcsLineByLine(string previous, string current)
{
string[] Previous = previous.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string[] Current = current.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
string lcsResult = string.Empty;
int a = Previous.Length;
int b = Current.Length;
int[,] table = new int[a + 1, b + 1];
//create a table with first line and column equal 0
for (int i = 0; i <= a; i++)
table[i, 0] = 0;
for (int j = 0; j <= b; j++)
table[0, j] = 0;
//create a table matrix
for (int i = 1; i <= a; i++)
{
for (int j = 1; j <= b; j++)
{
if (string.Equals(Previous[i - 1].Trim(), Current[j - 1].Trim(), StringComparison.InvariantCultureIgnoreCase))
{
table[i, j] = table[i - 1, j - 1] + 1;
}
else
{
table[i, j] = Math.Max(table[i, j - 1], table[i - 1, j]);
}
}
}
//get the lcs string array with the differences
int index = table[a, b];
string[] lcs = new string[index + 1];
lcs[index] = "0";
while (a > 0 && b > 0)
{
if (string.Equals(Previous[a - 1].Trim(), Current[b - 1].Trim(), StringComparison.InvariantCultureIgnoreCase))
{
lcs[index - 1] = Previous[a - 1].Trim();
a--;
b--;
index--;
}
else if (table[a - 1, b] > table[a, b - 1])
a--;
else
b--;
}
return lcs;
}
And this is the code that is not working with duplicated lines with same value.
Method to get all deleted items in the previous text:
private List<DiffItem> GetDiffPrevious(string[] previous, string[] diff)
{
List<DiffItem> differences = new List<DiffItem>();
//check items deleted
int line = 0;
for (int i = 0; i < previous.Length; i++)
{
bool isAbsent = false;
for (int j = 0; j < diff.Length; j++)
{
if (string.Equals(previous[i].Trim(), diff[j].Trim(), StringComparison.InvariantCultureIgnoreCase))
{
differences.Add(new DiffItem() { Position = line, Text = diff[j], Status = DiffStatus.Equal });
line++;
isAbsent = false;
break;
}
else
{
isAbsent = true;
}
}
//mark as deleted
if (isAbsent)
{
differences.Add(new DiffItem() { Position = line, Text = previous[i].Trim(), Status = DiffStatus.Deleted });
line++;
}
}
return differences;
}
If anyone could help me or any feedback would be great. Just a reminder, I cannot use third party libraries.
Thanks in advance.
I found the solution!
Basically, I rewrite the two lists and translated using Hashtable, so all the values will be unique by line. Then, I use the method LCS and got the result as expected.
I hope it helps somebody.
I am very new programmer, the user is able to input vales into a ListBox, there is also a option button where the user can sort the numbers in ascending order. However I am also asked to create a option button to unsort back into its original form, however I am not sure how I would do this. I am trying to do this without the use of any containers/arrays. here is my code to sort:
private void sorted()
{
int a = lstHoldValue.Items.Count;
for (int i = 0; i < a - 1; i++)
{
var k = 0;
for (var j = 1; j < a - i; j++)
{
if (Convert.ToInt32(lstHoldValue.Items[j]) < Convert.ToInt32(lstHoldValue.Items[k]))
{
var temp = lstHoldValue.Items[j];
lstHoldValue.Items[j] = lstHoldValue.Items[k];
lstHoldValue.Items[k] = temp;
k = j;
}
else
{
k++;
}
}
}
}
I've run into a real headache here.
I have a small program, which displays some images stored on disk. 8 images are displayed in a listview at a time, but when the images are large enough, the memory use (according to the task manager) reaches more than 1300mb! I suspect that there are some images or something which aren't deallocated, but I seem to be unable to pinpoint exactly where. I have tried both disposing all images in listview.largeimagelist.images, tried Clear()'ing the imagelist, but it doesn't make a difference at all.
Here is the current code:
private void btnLoadNewImages_Click(object sender, EventArgs e)
{
int k = lsvImgResult.Items.Count;
for (k = lsvImgResult.Items.Count; k >= 1; k--)
{
Seen.Push((MyFile)lsvImgResult.Items[k - 1].Tag);
imageList.Images.Clear();
}
int i = 0;
lsvImgResult.Items.Clear();
DisplayedImages.Clear();
imageList.Images.Clear();
imageList.ImageSize = new Size(100, 100);
imageList.ColorDepth = ColorDepth.Depth32Bit;
int HowMany = 0;
if (UnSeen.Count >= 8)
{
HowMany = 8;
}
else
{
HowMany = UnSeen.Count;
}
for (i = 1; i <= HowMany; i++)
{
MyFile CurFile = UnSeen.Pop();
Image j = Image.FromFile(CurFile.Filename);
DisplayedImages.Enqueue(CurFile);
imageList.Images.Add(j);
}
lsvImgResult.LargeImageList = imageList;
for (int j = 0; j < imageList.Images.Count; j++)
{
ListViewItem lstItem = new ListViewItem();
lstItem.ImageIndex = j;
lstItem.Tag = DisplayedImages.Dequeue();
lstItem.ToolTipText = ((MyFile)lstItem.Tag).Filename;
lsvImgResult.Items.Add(lstItem);
}
tabImagesLeft.Text = "Images left: " + UnSeen.Count;
}
I have generated 8*16 ovalshape's in a form. The code is:
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 8; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Transparent;
ovl.Name = "oval" + j + "" + i;
ovl.Location = new Point((ovl.Width * i) * 2, (ovl.Height * j) * 2);
ovalShape.Add(ovl);
}
}
foreach (OvalShape os in ovalShape)
{
Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer =
new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
os.Parent = shapeContainer;
this.Controls.Add(shapeContainer);
}
Now I want access to each ovalshape differently. How could I do this?
Since ovalShape is a List<OvalShape>, you can use the indexer to access any one item:
var anOval = ovalShape[0];
You are already accessing each ovalshape in ovalShape differently in your foreach loop
foreach (OvalShape os in ovalShape)
{
//...
}
Otherwise you can also access each ovalshape by it's index, as
var newOvalShape = ovalShape[0];
You have already named your controle like ovl.Name = "oval" + j + "" + i;
So , I think you can create dictrionary like Dictionary<string , OvalShape> dic
Then you can set it like
//...
ovl.Name = "oval" + j + "" + i;
dic.add(ovl.Name , ovl);
//...
Then , you can access this dictionary in other methods , and access it by its name.
im tunning this loop and what to populate the array with the output of my methods, im not sure about that last part "array2DB[i,i] =" how shold i do this.
updated loop based on replyes
private void BackGroundLoop()
{
for (int i = 1; i < 31; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}
I'm not 100% sure what you want to do, but you want probably this instead of your last line:
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
-1 in index is needed, because arrays are indexed from 0 in .NET.
This is the most you can do, without running into exception:
int[,] array2DB = new int[2, 30];
for (int i = 0; i < 30; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0,i] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1,i] = int.Parse(UpdateFormClass. nyeSaker.ToString());
}
Note that you can't have array2DB[2, *] or above because it will generate an arrayoutofbound exception.
You have to use two for loops. One for each the x and y axis of the array.
for (int i = 0; i < 2; i++){
for (int j = 0; j < 30; j++)
{
....
array2DB[i,j] = int.Parse(UpdateFormClass.gamleSaker.ToString())
, int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}