Using VS 2012, c#, asp.net web form
Trying to create a calculator with a drop down box that determines the operator(/*-+) for two text box values.
The drop down box is called DropDownList1. I novice with C, only done some java. My question is, how do i check what operator has been selected via the drop down.
I have tried the following
protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1 = * //or whatever ( obviously won't work for obvious reasons {
Then add textbox1 and textbox2 values
}
if (-)//and so on
etc...
Although the textboxes would be strings so I have no idea how to actually add the two values after I crack the drop down box :S)
I also tried to grab it via a list index of some sort? But i wasn't sure of the syntax however I tried stuff like
DropDownList1.SelectedIndex = something? // Wasn't sure where I was going here either
Any help would be great.
PS I wanted to use a drop down box even though it's silly
You should be able to just do:
DropDownList1.SelectedItem.Value
To get the value of the selected dropdown and then you can just do a case statement on what to do with the selected operator.
switch(DropDownList1.SelectedItem.Value)
{
case "+":
// do +
break;
case "-"
// do -
break;
}
if (DropDownList1.SelectedValue.Equals("your item value here"))
or
if (DropDownList1.SelectedItem.Text.Equals("your item text here"))
Related
I am currently developing a program in C# that contains a combo box and a button to add a new item to the combo box when said button is pressed. I need it so that when the button is pressed and the item is added, it gradually builds up a list of items that have a number that increases by one for each item.
For example when the button has been pressed twice, the combo box will contain the following two items:
New Profile 1
New Profile 2
Etc.
I am making it so that the items are set to "New Profile" with the number by default so that the end-user isn't confused as to which profile is which but the user can change the items name later if they wish to, but I am struggling making this concept work in code.
I had the generic code to add a item to my combo box:
private void AddProfileButton_Click(object sender, EventArgs e)
{
ProfileList.Items.Add("New Profile");
}
So, I tried drafting some code to see if I could accomplish my ideas on my own. This is my code:
int a = 0;
var b = a + 1;
var NewProfileName = "New Profile " + b;
ProfileList.Items.Add(NewProfileName);
However; when I tried this when the form was running, I kept getting items with the same name of "New Profile 1" repeatedly. So it sort of works - just that it doesn't increase the integer how I want it to.
I think part of the problem is the:
int a = 0;
part of the code however my attempt(s) to fix this (pasting this line of code under the Form_Load event) have been a failure/inconclusive.
I would greatly appreciate someone's help and all suggestions are welcome.
Josh
Thanks to #LarsTech for the answer.
The code is:
ProfileList.Items.Add(string.Format("New Profile {0}", ProfileList.Items.Count + 1));
I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}
I have tried to get an answer to this but so far no help has been able to do what I want it to.
I have this piece of code, which is meant to look at the selected row and output it's columns into the corresponding text boxes.
private void DataGridView01_SelectionChanged(object sender, EventArgs e)
{
if (DataGridView01.SelectedRows.Count > 0)
{
personIDTextBox.Text = DataGridView01.SelectedRows[0].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.SelectedRows[0].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.SelectedRows[0].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.SelectedRows[0].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.SelectedRows[0].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.SelectedRows[0].Cells[6].Value.ToString();
}
}
When I launch the program, I get no errors but it doesn't output the data into the textbox. Anyone know what I am doing wrong?
HOOKING UP EVENTS:
It is the most basic thing you need to learn to code in VS. In short it means that the event name, here DataGridView01_SelectionChanged is connected to the event. To do so one can either use code or one inserts it into the correct slot of the events pane of the property tab. Select the DataGridView, open the events pane (the one with the flash) and locate the SelectionChanged event! Here insert the name of the event and you are done.
(I only have the German versions of VS installed..)
The result is reflected in the form_designer.cs file and it is the same thing (in reverse) as double clicking that spot and then filling in the generated code stub..
Controls have many events; one is the default event and this can be generated by double clicking the control itself in the designer. But eventually you will need all 3 ways to generate and hook up the events, (as well as sometimes removing them.)
I use a slightly different approach when trying to get data from a datagridview.
Try doing personIDTextBox.Text = DataGridView01.SelectedCells[0].Value.ToString();
but instead of the event being on selection change, switch to CellClick and change the property of the the datagridview row selection property to full row select. after that you can change the SelectedCell[0] number to match whichever cell you want
If you want to display the datagridview selected rows into corresponding textboxes, fine the below steps ,
Step 1:
1. Change the DataGridView Selection mode to FullRowSelect in Datagridview property.
2. Create the cell click event in Data grid view using property.
enter image description here
3. Write the below code and test it, It may helpful
private void DataGridView01_CellClick(object sender,DataGridViewCellEventArgs e)
{
if (DataGridView01.Rows.Count > -1)
{
PersonIdTextBox.Text=DataGridView01.Rows[e.RowIndex].Cells[0].Value.ToString();
comboBox1.Text = DataGridView01.Rows[e.RowIndex].Cells[1].Value.ToString();
Txt_FirstName.Text = DataGridView01.Rows[e.RowIndex].Cells[2].Value.ToString();
mIDDLENAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[3].Value.ToString();
sURNAMETextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[4].Value.ToString();
cITYTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[5].Value.ToString();
eMAILTextBox.Text = DataGridView01.Rows[e.RowIndex].Cells[6].Value.ToString();
}
}
I want to have a text field in a C# winform application, and the effect I want is that as the user types stuff in the text field, the program searches a database in the background, and then generates a drop down list for the user to choose from ..
Here are two web based examples but do note that my application is winforms based, not web-based .. I just want the same effect, which is why I'm showing these:
cinemasquid.com:
blu-ray.com:
How can I get the same effect for a text field in a C# winform application ?
First you will need to bind TextChanged event on the form. Then when user press any key, you will get the event. Now inside event handler retrieve the string entered by the user, using this string you perform the search (don't do the search on UI thread, else your UI will hang, you can do the search in BackgroudWorker (MSDN)). Once you get the result, bind this result to ListBox.
I had developed one application, which has autocomplete feature. In this if user enter any movie name, matching results use to be displayed in the list box.
Hope this works for you.
If you don't want to code your solution there are a handful of custom controls avaliable via Google search, for example: http://www.dotnetfunda.com/articles/article225.aspx
BUT you should keep response times in mind: if you code a database search everytime the user enters a letter on your textbox, your application could get sluggish and/or unresponsive, depending on number of records on the table, speed of your network connection and a lot of other factors.
Consider preloading a collection of strings (name, titles, whatever it is you want to display on the textbox) on memory and then performing LINQ queries to that in-memory collection to populate the autocomplete part of your control.
As Amar Palsapure say, You need to use TextChanged Event of You DropDown. Also, Background worker will be also welcome.
Here is a short example:
First, we need some data source. In this case, this will be a simple list of strings:
List<string> DataSource = new List<string>
{
" How do I use jQuery to select all children except a select element",
"How can I get the text of the selected item from a dropdown using jQuery?",
"Dropdown menu in IE6 inserting too much width, not dropping-down",
"How to display images within a drop down box instead of text",
"InfoPath 2007 - Populate drop-down list on-the-fly",
"Is there any difference between drop down box and combo box?",
"Drop Down list null value not working for selected text c#?",
"Make drop-downs automatically drop in a loop cycle",
"PHP How can I keep the selected option from a drop down to stay selected on submit?",
"Jquery Issue - Drop down list does NOT show from iPhone/Mobile Devices"
};
next, BackgroundWorker DoWork Event - this is part response for searching valid position in our Data Source:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
String text = e.Argument as String;
List<String> results = new List<string>();
//Code for seraching text - may be diffrent if you have another DataSource
foreach (String dataString in DataSource)
{
if (dataString.IndexOf(text, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
{
results.Add(dataString);
}
}
e.Result = results;
}
You can see, that Result is returning by e.Result, So we need to implement RunWorkerCompleted event too:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
comboBox1.Items.AddRange((e.Result as List<String>).ToArray());
comboBox1.DroppedDown = true;
}
This code will fill our dropDown witch returned values, and show it to user.
Of course, to make this run, you need to call RunWorkerAsync in TextChanged:
private void comboBox1_TextChanged(object sender, EventArgs e)
{
// Save position of cursor, because it like to dissapering.
int cursor = comboBox1.SelectionStart;
//Clearing items in dropDown
comboBox1.Items.Clear();
//Is something was searched before, cancel it!
while (backgroundWorker1.IsBusy)
{
if (!backgroundWorker1.CancellationPending)
{
backgroundWorker1.CancelAsync();
}
}
// And search new one
backgroundWorker1.RunWorkerAsync(comboBox1.Text);
// And bring back cursor to live
comboBox1.SelectionStart = cursor;
}
I hope that helped you
I have a grid control with unbound columns in it.
The question is, everytime I change a cell value (I am using a repository item) I immediately call the gridview.PostEditor() method so that it would right away update the grid data which requires to be updated everytime I change a specific cell. Keep in mind that I am using the PostEditor() method so that I dont have to lose the focus on a cell first to update the gridview. Anyway, calling the PostEditor() will update my grid (which works fine) and still have the reposiroty item open, a repositoty item spin edit control to be exact, but the thing is if I hit a numeric key, It will discard the old value and replace it with the new one. I don't want it to behave like this. I want it so if the old value is 10.00 and I press the "0" key it will change the value to a "100.00" not back to "0.00".
Please help!
Thanks! :)
I dont know if I understand the question. But I did a search on some of my personal devexpress stuff and found this:
private void riSpinEdit_EditValueChanged(object sender, EventArgs e)
{
TextEdit edit = grdReceiveGoods.FocusedView.ActiveEditor as TextEdit;
if (edit != null)
{
int len = edit.SelectionLength;
int start = edit.SelectionStart;
grdReceiveGoods.FocusedView.PostEditor();
edit.SelectionLength = len;
edit.SelectionStart = start;
}
}
Let me know if this solves your problem.