combo box .text properties display wrong value - c#

I have one of the screen design as in the above image
Now you can see I got fill up product series approver 1 and approver 2 and so on.
But I keep getting empty value from approver 1.text or approver2.text.
What is the cause of this error??

i have found the problem. cause of postback.
it will trigger SelectedIndexChanged event
protected void CbxRejDocNum_SelectedIndexChanged(object sender, EventArgs e)
{
LoadDocumentDataRej(); // for load information
}
i remove the event during Capture Phase
CbxRejDocNum.SelectedIndexChanged -= new
System.EventHandler(CbxRejDocNum_SelectedIndexChanged);

This is how you can get combo box's selected text "comboBoxName.SelectedItem.Text"

I presume you're using ASPxClientComboBox.
You can get the text via GetText() method.
var text = mycombobox.GetText();

Related

C# WinForms ListBox changes selection to first item

I got a problem with a ListBox in a WinForm application. I have two ListBoxes inside of a tab control and depending on the selection in the first one (lb1), the DataSource of the second one (lb2) changes. This is done in the SelectedValueChanged Event.
private void listBox_ControlUnits_SelectedValueChanged(object sender, EventArgs e)
{
ControlUnit unit = (sender as ListBox).SelectedItem as ControlUnit;
textBox_ProjectNameTab.Text = unit.ProjectName;
listBox_ControlCircuits.DataSource = null;
listBox_ControlCircuits.DataSource = unit.ControlCircuits;
}
lb1 is filled with a DataSource, too.
Now if I select a value in lb1 the selection automatically jumps back to the first item and I can not figure out why. is this some kind of UI update problem?
Even without the SelectedValueChanged event and the connection to the second listbox the issue occures.
Short gif of the problem, sorry for the blurriness
If I select one item more than once it works somehow (as seen in the gif).
Edit:
I found the problem but I do not quite understand what happens.
I have another listBox on another tab of my tab control. This listBox has the same DataSource as lb1. This seems to cause this behavior.
I finally found the problem:
I did not know that if I use the same DataSource for two ListBoxes they share the BindingContext per default.
I created a new BindingContext for the second ListBox and now the selection does no longer change.
listBox_allGroups.DataSource = null;
listBox_allGroups.DataSource = x.y;
listBox_allGroups.DisplayMember = "Name";
listBox_ControlUnits.DataSource = null;
listBox_ControlUnits.DataSource = x.y;
listBox_ControlUnits.DisplayMember = "Name";
listBox_ControlUnits.BindingContext = new BindingContext();
You can use a variable to hold the selected item
object _selecteditem=null;
and check it in ListBox click event.
prive void ListBox1_Click(object sender,EventArgs e)
{
if(ListBox1.SelectItem == _selecteditem) return;
// do ...
}

Get selected Item name from combo box

I have a combo box as shown in the code below. I would like to display the name of the selection in a message box when I select it. What I am trying is -
<dxb:BarEditItem.EditTemplate>
<DataTemplate>
<dxe:ComboBoxEdit x:Name="PART_Editor"
SelectedIndexChanged="OnSelectedIndexChanged" Name="comboBox">
<dxe:ComboBoxEdit.Items>
<system:String>Item1</system:String>
<system:String>Item2</system:String>
</dxe:ComboBoxEdit.Items>
</dxe:ComboBoxEdit>
</DataTemplate>
How can I add the code in the backend for getting the selected name inside a message box?
Do you mean to handle this on the SelectedIndexChanged event? If so you can get the combobox that triggered the event.
private void OnSelectedIndexChanged(object sender, RoutedEventArgs e)
{
ComboBox cb = (ComboBox)sender;
string selectedText = cb.SelectedText;
//Code to display the selectedText into a message box
}
I'm not sure what do you mean by the "name of the selection", so I'm assuming you want to get hold of the text that is displayed in the combo, which represents the selected item.
Once you have the combo itself in your hands:
private void OnSelectedIndexChanged(object sender, RoutedEventArgs e)
{
var combo = (ComboBoxEdit)sender;
(...)
}
you have several options. Most reliable one (in my opinion) would be to use combo.DisplayText property, which is a read-only property holding the actual text that should be displayed in the combo (with consideration of DisplayMember property, DisplayTextConverter property and CustomDisplayText event).
Another option (in your particular case) would be (string)combo.SelectedItem. Note though, that combo.SelectedItem returns the actual selected item and not it's text representation. The above is fine as long as items are of type string. Should they not be, you'll get an InvalidCastException. Also, it's possible that in that case what you get might not be what you see (as noted in previous paragraph there are several ways to modify the displayed text).
Yet another option is combo.Text, which takes into consideration DisplayMember, but not DisplayTextConverter nor CustomDisplayText.
EDIT
Turns out that at the time SelectedIndexChanged is raised, DisplayText property is not yet updated to reflect the newly selected item (which isn't especially surprising). To deal with that, you should "postpone" the retrieval of the DisplayText value. I'd personally go with something along these lines (using a Dispatcher associated with the combo):
private void OnSelectedIndexChanged(object sender, RoutedEventArgs e)
{
var combo = (ComboBoxEdit)sender;
combo.Dispatcher.BeginInvoke(new Action(() =>
{
var text = combo.DisplayText;
(...)
}));
}
just type comboboxName.Text and you will get the selected item of combobox

Dynamically get textbox's name

private void txt_f_name_TextChanged(object sender, TextChangedEventArgs e)
{
string textbox_name_1,textbox_name_2;
TextBox textbox_1 = (TextBox)e.Source;
textbox_name1= textbox_1.Text;
TextBox textbox_2 = (TextBox)e.OriginalSource;
textbox_name_2;= textbox_2.Text;
}
now both textbox_name_1 and textbox_name_2 are getting same result.
if i try to get another thing like text,with etc... these are also getting the same result....
but i think there may be some difference.
so,i want to know the major difference between e.source and e.OriginalSource.
There are cases source and original source differ.
Common cases where the source may be adjusted include content elements
inside a content model for a control (the contents of a list item, for
instance, will report the list item element as the Source and the
actual element within the list item will be the OriginalSource.
ref from MSDN:
i'm not sure what you try to do with your code. to check source and original source text property do like below, and you can do the same thing by adding list view with items having text box.
private void txt_f_name_TextChanged(object sender, TextChangedEventArgs e)
{
string textbox_name_1,textbox_name_2;
TextBox textbox_1 = (TextBox)e.Source;
textbox_name1= textbox_1.Text;
TextBox textbox_2 = (TextBox)e.OriginalSource;
textbox_name_2 = textbox_2.Text;
}
From the documentation
This originalsource property acquires its value once, before the class event handlers or any instance handlers are invoked, and is never adjusted past this point.
With routed events other events may have fired before your handler.
[OriginalSourece][1]
http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx

C# ComboBox in DropDownList style, how do I set the text?

I want to use a ComboBox with the DropDownList style (the one that makes it look like a button so you can't enter a value) to insert a value into a text box. I want the combobox to have a text label called 'Wildcards' and as I select a wildcard from the list the selected value is inserted in to a text box and the combobox text remains 'Wildcard'. My first problem is I can't seem to set a text value when the combobox is in DropDownList style. Using the properties pallet doesn't work the text value is simply cleared when you click off, adding comboBox.Text = "Wildcards"; to form_load doesn't work either. Can anyone help?
The code you specify:
comboBox.Text = "Wildcards";
...should work. The only reason it would not is that the text you specify is not an item within the comboBox's item list. When using the DropDownList style, you can only set Text to values that actually appear in the list.
If it is the case that you are trying to set the text to Wildcards and that item does not appear in the list, and an alternative solution is not acceptable, you may have to be a bit dirty with the code and add an item temporarily that is removed when the drop-down list is expanded.
For example, if you have a form containing a combobox named "comboBox1" with some items and a button named "button1" you could do something like this:
private void button1_Click(object sender, EventArgs e)
{
if (!comboBox1.Items.Contains("Wildcards"))
{
comboBox1.Items.Add("Wildcards");
}
comboBox1.Text = "Wildcards";
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
if (comboBox1.Items.Contains("Wildcards"))
comboBox1.Items.Remove("Wildcards");
}
That's pretty quick and dirty but by capturing the DropDownClosed event too you could clean it up a bit, adding the "Wildcards" item back as needed.
You can select one of items on formload or in form constructor:
public MyForm()
{
InitializeComponent();
comboBox.SelectedIndex = 0;
}
or
private void MyForm_Load(object sender, EventArgs e)
{
comboBox.SelectedIndex = 0;
}
Try this
comboBox1.SelectedValue = "Wildcards";
This may be a possible solution:
comboBox1.SelectedValue = comboBox1.Items.FindByText("Wildcards").Value;

DropdownList.selectedIndex always 0 (yes, I do have !isPostBack)

(Scroll down to bottom of post to find solution.)
Got a asp.net page which contains a
Datalist. Inside this datalist, there
is a template containing a
dropdownlist and each time the
datalist is filled with an item, a
ItemCreatedCommand is called. The
itemCreatedCommand is responsible for
databinding the dropdownlist.
I think the problem lies here, that
I'm using ItemCreatedCommand to
populate it - but the strange things
is that if I choose the color "green",
the page will autopostback, and I will
see that the dropdown is still on the
color green, but when trying to use
it's SelectedIndex, I always get 0...
protected void DataListProducts_ItemCreatedCommand(object
source, DataListItemEventArgs e)
var itemId = (String)DataListProducts.DataKeys[e.Item.ItemIndex];
var item = itemBLL.GetFullItem(itemId);
var DropDownListColor = (DropDownList)e.Item.FindControl("DropDownListColor");
//Also tried with :
//if(!isPostBack) {
DropDownListColor.DataSource = item.ColorList;
DropDownList.Color.Databind();
// } End !isPostBack)
Label1.test = DropDownListColor.SelectedIndex.toString();
// <- THIS IS ALWAYS 0! *grr*
I've narrowed down the code a bit for
viewing, but still you can see what
I'm trying to do :) The reason for
why I'm doing this, and not declaring
the datasource for the colors directly
i aspx-page, is that I need to run a
test if(showColors), but I do not want
to clutter up the html-page with code
that I feel should be in the code
behind-file.
EDIT: After trying to alter
SelectedIndexChange - I'm having a
"logical" confusion in my head now -
how am I to alter elements inside the
datalist? Since, as far as I know - I
do not have any way to check which of
the items in the datalist this
particular dropdownlist belongs to...
Or? I'm going to try out a few ways
and see what I end up with ;) But do
please post your thoughts on this
question :)
SOLUTION:
Either bubble the event to ItemCommand, or Handle the event, get the senders parent(which is a datalistItem and manipulate elements in there.
protected void DropDownListColor_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownListColor = (DropDownList)sender;
DataListItem dataListItem = (DataListItem)dropDownListColor.Parent;
var item = items[dataListItem.ItemIndex];
var color = item.ItemColor[dropDownListColor.SelectedIndex];
var LabelPrice = (Label)dataListItem.FindControl("LabelPrice");
LabelPrice.Text = color.Price;
}
When the DataList is data-bound, the AutoPostBack has not been handled yet, i.e. the values in the ItemCreated event are still the original values.
You need to handle the SelectedIndexChange event of the dropdown control.
Regarding your 2nd question:
I suggest you remove the AutoPostBack from the dropdown, add an "Update" button, and update the data in the button Click event.
The button can hold Command and CommandArgument values, so it's easy to associate with a database record.
some MSDN links with C# examples on bubbling
http://msdn.microsoft.com/en-us/library/system.web.ui.control.onbubbleevent.aspx
http://msdn.microsoft.com/en-us/library/aa719644(VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa720044(VS.71).aspx
Thank You for your solution
protected void ddlOnSelectedIndexChanged(object sender, EventArgs e) {
try {
ModalPopupExtender1.Show();
if (ViewState["Colors"] != null) {
FillColors(ViewState["Colors"].ToString());
}
DropDownList dropDownListColor = (DropDownList)sender;
DataListItem dataListItem = (DataListItem)dropDownListColor.Parent;
Image image = (Image)dataListItem.FindControl("mdlImage");
Label ProductCode = (Label)dataListItem.FindControl("lblprdCode");
Label ProductName = (Label)dataListItem.FindControl("lblProdName");
DropDownList ddlQuantity = (DropDownList)dataListItem.FindControl("ddlQuantity");
Label ProductPrice = (Label)dataListItem.FindControl("lblProdPrice");
Label TotalPrice = (Label)dataListItem.FindControl("lblTotPrice");
//Label ProductPrice = (Label)dataListItem.FindControl("lblProdPrice");
} catch (Exception ex) {
}
}

Categories