Access to method list in current file (VS AddIn) - c#

I want to get list of methods in current file, when my tool is fired, but I don't have idea how that combobox is nested in the _applicationObject object. Can you help me?

I did it this way:
private void ListMethods()
{
CodeNamespace codenamespace = null;
for(int i = 1; i <= _applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Count; i++)
{
if(_applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Item(i).Kind == vsCMElement.vsCMElementNamespace)
{
codenamespace = (CodeNamespace)_applicationObject.ActiveWindow.ProjectItem.FileCodeModel.CodeElements.Item(i);
}
}
for(int i = 1; i <= codenamespace.Members.Count; i++)
{
if(codenamespace.Members.Item(i) is CodeClass)
{
ListMethodsForClass(codenamespace.Members.Item(i) as CodeClass);
}
}
}
private void ListMethodsForClass(CodeClass codeclass)
{
for (int i = 1; i <= codeclass.Members.Count; i++)
{
message(codeclass.Members.Item(i).Name);
}
}
I hope it will help in the future some of you :)

Related

System.StackOverflowException when reading in a file c#

I'm trying to read a file in to my wpf program but the System.StackOverflowException is occuring! Any help would be amazing, thank you!!
My Code:
private void BestSellerListtxt_TextChanged(object sender, TextChangedEventArgs e)
{
string[] TopFictionArray = File.ReadAllLines("TopFiction.txt");
string[] TopNonFictionArray = File.ReadAllLines("TopNonFiction.txt");
for (int i = 0; i < TopFictionArray.Length; i++)
{
BestSellerListtxt.Text = TopFictionArray[i];
}
BestSellerListtxt.Text = "";
for (int i = 0; i < TopNonFictionArray.Length; i++)
{
BestSellerListtxt.Text = TopNonFictionArray[i];
}
}
Try this and with the help of string.Join(Delimeter, array) you can concat arrays elements without using loops and easily can print
BestSellerListtxt.Text = string.Join("," , TopFictionArray);
BestSellerListtxt_TextChanged = strin.join("," , TopNonFictionArray);

How can I select and Deselect all items by checkbox in dynamically added check _list_box in c#?

I wanted to do select/ deselect with one checkbox. I have tried to get the index of the selected item. I haven't been able to get this, so I tried to change the name of the box. Still no success.
foreach (var item in DATAsetname_INIlist)
{
checkedListBox2.Items.Add(item);
}
if (checkedListBox2.Items.Count != 0)
{
checkedListBox2.Items.Add("Select all");
}
private void checkedListBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (checkedListBox2.Items.Count != 0)
{
if (checkedListBox2.SelectedItem.ToString() == "Select all")
{
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
checkedListBox2.SetItemChecked(i, true);
}
string changed = "Deselect all";
checkedListBox2.SelectedItem =changed;
}
if (checkedListBox2.SelectedItem.ToString() == "Deselect all")
{
for (int i = 0; i < checkedListBox2.Items.Count; i++)
{
checkedListBox2.SetItemChecked(i, false);
}
string changed = "Select all";
checkedListBox2.SelectedItem = changed;
}
}
}
can you please help on this ? thank you
Have done quick exercise. Please modify your code accordingly.
/// <summary>
/// Check all check boxes and vice versa
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ChkSelectAll_CheckedChanged(object sender, EventArgs e)
{
//Declare your checkedListBox2 count
iCount = checkedListBox2.Items.Count;
if (sender != null)
{
for (int i = 1; i <= iCount; i++)
{
CheckBox ck = null;
Control[] chkTest = this.Controls.Find("chkDrive" + i, true);
if (chkTest.Length > 0)
{
if (chkSelectAll.Checked)
{
for (int j = 0; j < chkTest.Length; j++)
{
ck = (CheckBox)chkTest[j];
ck.Checked = true;
}
}
else
{
for (int j = 0; j < chkTest.Length; j++)
{
ck = (CheckBox)chkTest[j];
ck.Checked = false;
}
}
}
}
}
}
can you try this.
try
{
checkedListBox1.SelectedIndexChanged -= checkedListBox1_SelectedIndexChanged;
if (checkedListBox1.Items.Count != 0)
{
if (checkedListBox1.SelectedItem.ToString() == "Select all")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, true);
}
string changed = "Deselect all";
checkedListBox1.Items[checkedListBox1.SelectedIndex] = changed;
}
else if (checkedListBox1.SelectedItem.ToString() == "Deselect all")
{
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
checkedListBox1.SetItemChecked(i, false);
}
string changed = "Select all";
checkedListBox1.Items[checkedListBox1.SelectedIndex] = changed;
}
}
}
catch (Exception ex)
{
}
finally
{
checkedListBox1.SelectedIndexChanged += checkedListBox1_SelectedIndexChanged;
}
If this is C# based, windows application.
Search form collection and find all check box on form, using for each.
Code
foreach(CheckBox item in Form.Control)
{
Item.Checked=true;
}
For check list box
for(int i;I<checklistbox.items.count;I++)
{
checklistbox.SetItemChecked(I,true);
}
When i work with c# i had litle simillar problem in my case was enought was to replace if(string==string) by string.equals(string)
Btw few questions:
checkedListBox2.SelectedItem.ToString() what is value in here where comparing?
Does your listener work at all?
Not better way to check which checkBox is taken comparing sender in switch?
How u declare checkedListBox2
By checkedListBox2.Items.Add(item); i suspect u create checkBoxes by hand can u show it? meybe something is there wrong
This part added after Lian comment
foreach(var item in DATAsetname_INIlist)
{
checkedListBox2.Items.Add(item);
}
if (checkedListBox2.Items.Count != 0) {
checkedListBox2.Items.Add("Select all");
}
private void checkedListBox2_SelectedIndexChanged (object sender, EventArgs e)
{
if (checkedListBox2.Items.Count != 0 && checkedListBox2.SelectedItem.ToString().equals("Select all")) {
changeStateOfSelectedItem("Deselect all", true);
} else if (checkedListBox2.SelectedItem.ToString().equals("Deselect all")) {
changeStateOfSelectedItem("Select all", false);
}
}
private void changeStateOfSelectedItem (String state, bolean stateToReplace){
for (int i = 0; i < checkedListBox2.Items.Count; i++) {
checkedListBox2.SetItemChecked(i, stateToReplace);
}
string changed = state;
checkedListBox2.SelectedItem = changed;

How can i improve performance of my application?

I have three different function which i call based on the condition. When data is lesser my application performance is good. However, i have around 11000 rows of data my application taking 1-2 hour to complete the process.
I have some logic written in my functions which filters data and add into collection. Is there is any way i can improve the performance of my application using multi-threading or parallel processing. so that my function runs parallel and performance get improved.
I created a sample of my requirement below:
private void button1_Click(object sender, EventArgs e)
{
String[] AllData = new String[7];
AllData[0] = "1";
AllData[1] = "2";
AllData[2] = "3";
AllData[3] = "1";
AllData[4] = "2";
AllData[5] = "3";
AllData[6] = "1";
for (int i = 0; i < AllData.Length; i++)
{
CommonFunction(AllData[i]);
}
}
private void CommonFunction(String AllData)
{
if (AllData == "1")
{
Function1(1);
}
else if (AllData == "2")
{
Function2(2);
}
else
{
Function3(3);
}
}
private void Function1(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
private void Function2(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
private void Function3(int ID)
{
for (int i = 0; i < 10; i++)
{
}
}
Any help is highly appreciated.
Instead of sequential for loop, you can try to use Parallel.ForEach.
for (int i = 0; i < AllData.Length; i++)
{
CommonFunction(AllData[i].ToString());
}
can be converted to
Parallel.ForEach(AllData, someData =>
{
CommonFunction(someData);
});

CheckedListBox.SelectedItems.Count = 1?

In C#, I have checked list boxes, that I need to store the data in arrays, but when I start the event that writes the objects to the array, I have to set the size of the array, which I naturally set to the amount of items checked. However, the items checked, for both checked list boxes I have is 1, no matter how many I check. Can someone help?
public partial class Form3 : Form
{
public static object[] dtype;
public static bool loaded = false;
bool typeselecte = false;
bool typeselectd = false;
public Form3()
{
InitializeComponent();
}
private void Form3_Shown(object sender, EventArgs e)
{
if (loaded)
{
int counte = 0;
int countd = 0;
types1.Items.AddRange(dtype);
types2.Items.AddRange(dtype);
if (typeselecte)
{
for (int i = 0; i < types1.Items.Count; i++)
{
if (i == Form1.enumber[counte])
{
types1.SelectedItems[i] = Form1.esearch[i];
counte++;
}
}
}
if (typeselectd)
{
for (int j = 0; j < types2.Items.Count; j++)
{
if (j == Form1.dnumber[countd])
{
types2.SelectedItems[j] = Form1.dsearch[j];
countd++;
}
}
}
}
}
public void dtypes()
{
dtype = new object[types1.Items.Count];
for (int i = 0; i < types1.Items.Count; i++)
{
dtype[i] = types1.Items[i];
}
}
private void button1_Click(object sender, EventArgs e)
{
if (types1.SelectedItems.Count > 0)
typeselecte = true;
if (types2.SelectedItems.Count > 0)
typeselectd = true;
Form1.esearch = new object[types1.SelectedItems.Count];
Form1.dsearch = new object[types2.SelectedItems.Count];
Form1.enumber = new int[types1.SelectedItems.Count];
Form1.dnumber = new int[types2.SelectedItems.Count];
int counte = 0;
int countd = 0;
if (typeselecte)
{
for (int i = 0; i < types1.SelectedItems.Count; i++)
Form1.esearch[i] = types1.SelectedItems[i];
}
if (typeselectd)
{
for (int j = 0; j < types2.SelectedItems.Count; j++)
Form1.dsearch[j] = types2.SelectedItems[j];
}
if (typeselecte)
{
for (int k = 0; k < types1.Items.Count; k++)
{
if (Form1.esearch[k] == types1.Items[k])
{
Form1.enumber[counte] = k;
counte++;
}
else
{
k--;
}
}
}
if (typeselectd)
{
for (int l = 0; l < types2.Items.Count; l++)
{
if (Form1.dsearch[l] == types2.Items[l])
{
Form1.dnumber[countd] = l;
countd++;
}
else
{
l--;
}
}
}
this.Close();
}
}
Form1.esearch and dsearch are object arrays, which the size hasn't been picked yet, and e and dnumber are int arrays that have unknown size as well, I just didn't feel the need to put in that code.
I believe you need to use the property CheckedItems as opposed to SelectedItems.

Getting position in a list and comparasion

I am trying to get positions in a list of some values to compare them with another list.
for (int i = 0; i <= commands.ToArray().Length; i++)
{
levensheteinvalues_commands.Add(commands.ToArray()[i].ToString());
levensheteinvalues_numbers.Add(
Program.ComputeLevenshteinDistance(args[0],
commands.ToArray()[i].ToString()));
}
for (int i = 0; i <= commands.ToArray().Length; i++)
{
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
Why doesn't
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
work? And when I get the value how can I compare it to another list?
The code you have posted have some serious problems. This May solve your problem since your code as well as your approach is very unclear and ambiguous. I have blindly edited the code to fix the serious problems.
for (int i = 0; i < commands.Count(); i++) {
levensheteinvalues_commands.Add(commands.ElementAt(i).ToString());
Program.ComputeLevenshteinDistance(args[0], commands.ElementAt(i).ToString()));
}
for (int i = 0; i < commands.Count(); i++) {
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
int index = levensheteinvalues_numbers.IndexOf(levensheteinvalues_numbers.Min());

Categories