I have a silverlight page with a listbox and a combobox...
Based on what the user clicks in the list box, I want to populate the dropdownbox. The completed event is the same for each item in the list box (items include "BaseTypes", "Bays", "Face", etc)
How can I make the completed method generic so I don't have to have one for each call?
private void lstEdits_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ServiceCylinderClient obj = new ServiceCylinderClient();
obj.GetBaysCompleted += new EventHandler<GetBaysCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));
string selectedItem = lstEdits.SelectedItem as string;
switch selectedItem
{
case "BaseTypes":
obj.GetBaseTypesCompleted += new EventHandler<GetBaseTypesCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));
obj.getGetBaseTypesAsync();
break;
case "Bays":
obj.GetBaysCompleted += new EventHandler<GetBaysCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));
obj.getGetBaysAsync();
break;
}
}
As it stands now, I will have to have a "completed method" for each call, but since they would all do the same thing (just set the list box items source)..i'd like to make it generic to simplify things.
void GetBaseTypesCompleted(object sender, getBaseTypesCompletedEventArgs e)
{
lstEdits.ItemsSource = e.Result;
}
void GetBaysCompleted(object sender, getBaysCompletedEventArgs e)
{
lstEdits.ItemsSource = e.Result;
}
Thanks in advance!
I believe you would need to use reflection to read the 'Result' property off the 'CompletedEventArgs' as they do not all come from a base type that exposes 'Result'.
You should be able to do something like the following:
lstEdits.ItemsSource = (IEnumerable)e.GetType().GetProperty("Result").GetValue(e, null);
I think it dont have easy solution for this problem,because every completedmethod has different EventArgs for different result.
Related
So this is my first time using visual studio and c#. i'm making a simple task manager. I have a ListBox that displays all the current running processes. I want to make a button that will sort the ListBox in alphabetical order when i click it the first time, then reverse alphabetical order on the second click, and then back to unordered when i click it again.
So far i've made a button to sort the listbox like so...
private void sortButton_Click(object sender, EventArgs e)
{
processListBox.Sorted = true;
}
This works, but how do i get it so that when i click the sort button again, it will sort it differently? I was thinking of some kind counter and sort it based on whether the number was odd or even, but i have 3 cases for this (Alphabetical, Reverse Alphabetical, and original unordered)
Thank you.
The classic way to solve this is with a flag:
private bool sortDirection = false; //Or use an enum!
private void sortButton_Click(object sender, EventArgs e)
{
sortDirection = !sortDirection;
if (sortDirection)
{
//Sort one way
}
else
{
Sort the other
}
}
The answer to your more general question is similar, use a flag or enum. Alternatively, use a control that includes a flag like a Checkbox or ToggleButton. Then you can just go off of the IsChecked property.
To do more than two pieces of functionality, I would use an enum and the modulus operator:
private MyEnum sortType;
private void sortButton_Click(object sender, EventArgs e)
{
sortType = (MyEnum)(++((int)sortType) % MyEnum.GetValues().Count());
switch (sortType)
{
}
}
Note that you could cache the count value, and probably improve the casting code a few other ways.
With your code I have an idea:
private void sortButton_Click(object sender, EventArgs e)
{
processListBox.Sorted = !processListBox.Sorted;// true;
}
That sould work
You can use Checkbox instead with display type of Button. So add checkbox to form. then write these inside or behind InitializeComponent
this.checkbox1.ThreeState = true;
this.checkbox1.Appearance = System.Windows.Forms.Appearance.Button;
Voila now you have 3 state button.
Then you can switch like this (inside CheckedChanged event).
switch (checkbox1.CheckState)
{
case CheckState.Unchecked: // original unordered
break;
case CheckState.Checked: // Alphabetical
break;
case CheckState.Indeterminate: // Reverse Alphabetical
break;
}
Hi there just building a small conversion calculator and Im adding a combo box to it so its not as cluttered and easy to manage. I wont to add a few options to my combo box so that the user has different options to choose from. However I'am going to build a small class with the conversion calculations so that when a different option is selected within the combo box the correct method will be called. I will add a code snippet to show use were i'am at. I was just using the message box just so i know that it was working. Any code snippets would be great.
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem kilo = ((sender as ComboBox).SelectedItem as ComboBoxItem);
}
private double workOutKilo()
{
double result = 2;
return result;
}
Assign each ComboBoxItem's Tag control a function, within the SelectionChanged event, call the function.
Yeah if you use a switch than it might be easy to choose combined by the code you already have.
I would suggest something like this:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBoxItem kilo = ((sender as ComboBox).SelectedItem as ComboBoxItem);
switch (kilo.ToString())
{
case "Kilo":
//Method();
break;
//...
}
}
I guess this would do the job:
private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox kilo = (sender as ComboBox);
int index = kilo.SelectedIndex;
switch (kilo.ToString())
{
case "0":
//Method();
break;
//...
}
}
I have a form that loads 3 pre-defined scores in a list box. I want to convert a selected score into a string, and then output that string in a textbox. So far i think i've converted the item to a string, and tried setting it to the textbox but it doesn't seem to be working.
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
if (this.lstStudents.SelectedIndex >= 0)
{
string a = lstStudents.Items.Cast<string>().ToString();
txtDisplay.Text = a;
}
btnUpdate.Enabled = false;
Assuming your question is about Windows Forms, One way to get the selected item is to use code like this:
txtDisplay.Text =lstStudents.SelectedItem.ToString();
It is common to want to get the selected item that the user has selected, to do this, you need to place the above code in an event to look like this for example:
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = this.lstStudents.SelectedItem.ToString();
}
An event can be wired to the control either by code or via the VS IDE, you can't just copy and paste the above code. Ask me if you don't know how to do that.
If you want to grab the first item only, then Plutonix comment above applies. You don't need the IF statement.
Since this is the process at load time, why not try just :
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
txtDisplay.Text = lstStudents.Items[0].ToString();
btnUpdate.Enabled = false;
EDIT
then add at the listbox's event SelectedIndexChanged :
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = lstStudents.Items[lstStudents.SelectedIndex].ToString();
}
I am using visual studio 12, coding in asp.net using c#.
I have 3 dropdownlists in my code, all of which are bound by lists that I created.
I need some advise as to which method is better to call the postbackvalues of ddl's to perform a task.
Option 1
When a user selects an item from drop down list 3, the postbackvalue is sent from Dropdownlist3_SelectedIndexChanged to the dropdownlist2_selectedindexchanged by calling the method. Only after I have both the postbackvalues I would like to produce a chart. Regardless of what the chart holds and regardless of what the data is in the drop down list.
So something like
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to have the postbackvalue of drop down list 3 here so i can use its value and dropdownlist2's postbackvalue to produce a chart.
}
and in the dropdownlist3
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
// I would like to call DropDownlist2_SelectedIndexChanged(...) method so I can send the postbackvalue of DDL3 for use in DDL2.
}
Option 2:
Define a global variable that stores the postbackvalue of Dropdownlist3 and use that value in Dropdownlist2_SelectedIndexChanged method for further use such as produces a chart.
I have read a lot about global variables but do not understand the con's about them.
I am not sure if this is what you are after, but perhaps having a third method which is called that handles the updating of the chart...
for example
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
BuildChart();
}
private BuildChart()
{
var ddl3Value = DropDownList3.SelectedValue;
var ddl2Value = DropDownList2.SelectedValue;
if(ddl3Value != null && ddl2Value != null)
{
//build chart.
}
}
I had a DataGrid and a ContextMenuStrip in it. When I click SelectMenuStrip in a row I want the ClickEvent of the context menu to get all data in that row in a databean class and return that databean class, so that I can fill the data in another class-
All was fine I defined event as below
private CustomerDataBean toolStripMenuItem1_Click(object sender, EventArgs e)
{
CustomerDataBean custdatabean = null;
int rowno = tblcustomerdataview.CurrentCellAddress.Y;
custdatabean.Customerpk = int.Parse(tblcustomerdataview.Rows[rowno].Cells[0].Value.ToString());
custdatabean.Contactno = tblcustomerdataview.Rows[rowno].Cells[6].Value.ToString();
custdatabean.Emailid = tblcustomerdataview.Rows[rowno].Cells[7].Value.ToString();
custdatabean.Other = tblcustomerdataview.Rows[rowno].Cells[8].Value.ToString();
return custdatabean;
}
but in designer.cs I am getting an error in line:
this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
The error is:
Error 1:
'WindowsFormsApplication3.CustomerDataBean WindowsFormsApplication3.CustomerSearch.toolStripMenuItem1_Click(object, System.EventArgs)' has the wrong return type D:\WindowsFormsApplication3\WindowsFormsApplication3\Merchandising\customerSearch.Designer.cs 83 46 NFTRANS
Where have I done something wrong?
Let me explain the situation
I had a jobcodeform where user should input the customercode in combobox if he forget customercode he can use a buton to go to another form called customersearch where there is a datagrid table with a context menustrip which when clicked gets the whole details of the selected row in a customerdatabean and return that back to the first jobcodeform
Your code doesn't make a lot of sense. Click events don't return anything (except void), they really just run a procedure.
Your quick fix is to match the signature of the handler:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
// do something, don't return anything
}
What you need to define is what are you trying to do with your CustomerDataBean object. If you are just trying to add it to a list, then add it to a list:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
CustomerDataBean custdatabean = new CustomerDataBean();
// set properties
myBeans.Add(custdatabean);
}
The code you currently have doesn't even create a CustomerDataBean object. It's null, and then you are trying to update a null object. That isn't going to work.
Ask yourself where should your click event return the object to?
What code will process that bean?
The others have explained what is wrong with your click event.
Here's one possible method:
Have your click event call a separate method to process the bean. Something like this, perhaps:
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
CustomerDataBean custdatabean = null;
int rowno = tblcustomerdataview.CurrentCellAddress.Y;
custdatabean.Customerpk = int.Parse(tblcustomerdataview.Rows[rowno].Cells[0].Value.ToString());
custdatabean.Contactno = tblcustomerdataview.Rows[rowno].Cells[6].Value.ToString();
custdatabean.Emailid = tblcustomerdataview.Rows[rowno].Cells[7].Value.ToString();
custdatabean.Other = tblcustomerdataview.Rows[rowno].Cells[8].Value.ToString();
processBean(custdatabean);
}
private void processBean(CustomerDataBean bean)
{
//Code to process the bean here.
}
ToolStripMenuItem click event handlers need to return void.