Getting text from a WPF combobox or text box in C# - c#

I am using a Grid to hold text boxes and combo boxes. I want to get the selected item from the combo boxes and the text from the text boxes. Is there a way to do this without having to cast the UIElement as Combo or Text box. Below is how I am currently doing this.
foreach (UIElement field in _fields)
{
string val="";
if (field is TextBox)
{
TextBox bx = field as TextBox;
val=bx.Text;
}
else if (field is ComboBox)
{
ComboBox bx = field as ComboBox;
val=bx.SelectedItem.ToString();
}
}
Thanks

The best way to access data in WPF is to use databinding or even use MVVM. A quick and dirty way could be to give your elements a name by assigning x:Name in your XAML. Then you do not need to cast the elements in code behind.

Related

Use typed text from AutoComplete in DataGridViewComboBoxColumn, even if that value is not in the Combo Box DataSource

I have a DataGridViewComboBoxColumn, sometimes the ComboBox has a list of items, but sometimes the list is empty.
I have enabled the AutoCompleteMode, in order for user to type in what item he wants.
I want to have the possibility to use the typed text from the user as a valid item.
I mean, in case the list is empty, the user can type in a text into ComboBox and to use that one as input.
Also, when the list is not empty, the user can type in a text into ComboBox and even if that item name is not in the list, to use it as input.
At the moment, it works just when the user types in a value that is in the list, then that value can be used. If the user types in a value that is not in the list, the value is not used as desired one.
This is how I enabled the AutoComplete:
private void DataGridNewOrders_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox comboBox = e.Control as DataGridViewComboBoxEditingControl;
if (comboBox != null)
{
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
comboBox.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
}
}
Is there any property that allows the typed text from ComboBox to be used even if that text is not in the list ?
Thank you.

Find a specific dynamic TextBox in a C# WPF GroupBox

I have added a GroupBox to my main Grid and am populating it dynamically with controls. I need to get a specific textbox within that GroupBox in an onClick event. I am able to loop through the GroupBox and fine, like this...
foreach (Control ctl in ((Grid)gpMccEngineProperties.Content).Children)
{
if (ctl.GetType() == typeof(TextBox))
{
TextBox textbox = (TextBox)ctl;
PropertyValue propertyValue = new PropertyValue();
propertyValue.Value = textbox.Text;
}
}
... but if i just want to access a specific TextBox i keep coming back with a null value. here is how i'm trying to get it...
TextBox txt = ((Grid)gpMccEngineProperties.Content).Children.OfType<TextBox>().Where(t => t.Name == "PropertyId_9") as TextBox;
... where PropertyId_9 is the name of a textbox that i added dynamically to the GroupBox. Any idea how i get that textbox so i can get it's value?
Thanks!
You're using the wrong Linq method. That code returns an IEnumerable of TextBoxes, not just the TextBox. Use Single or SingleOrDefault instead of Where:
TextBox txt = ((Grid)gpMccEngineProperties.Content).Children.OfType<TextBox>().Single(t => t.Name == "PropertyId_9");

How to remove a ListBoxItem according to its value when a ListBoxItem is made of more objects?

I have a ListBox and each ListBoxItem is made of two images and one label. From the application I bind the Content value of the label and the Source values of the images.
Now I would like to be able to search in the Listbox the ListBoxItem with a specific value of the label, but I have no ideas on how to do that.
Thanks for your help.
foreach (item in listBox.Items)
{
if (item is typeof(ListBoxItem) && item.Value == someValueToCheck)
{
//do something
}
}

how to get data of check box list with text boxes

I try to get data from the textboxex that is related to the ckeckboxex in the checkboxlist ..
in order to calculate the value of the selected box
this is my code :
foreach (ListItem item in listOthers.Items)
{
if (item.Selected)
{
sum += Convert.ToInt32(TextBox1.Text);//textbox of selected checkbox
}
}
lblSum.Text = sum.ToString();
What do I need to do to get the value of the textbox related to the checkbox?
Based on your comments, you have a CheckBoxList with a fixed size of 10 items and along with that 10 Textboxes (named TextBox1, TextBox2 and so on). The users marks some CheckBoxes and also enters a text into the corresponding Textboxes. You want to convert the Text of the Textboxes to an integer and calculate the sum of the marked entries.
You need to dynamically access the Textboxes by calling FindControl:
var index = 0;
foreach (ListItem item in listOthers.Items)
{
index++; // Increase here as the Textbox numbers start at 1
if (item.Selected)
{
var txt = FindControl("TextBox" + index.ToString()) as TextBox;
if (txt != null)
sum += Convert.ToInt32(txt.Text); //text of selected Checkbox
}
}
lblSum.Text = sum.ToString();
Please note that it is important to call FindControl on the container of the Textboxes. The call in the sample works if the Textboxes are located directly on the page. If they are located on a Panel named Panel1, you'd have to call:
var txt = Panel1.Findcontrol("TextBox" + index.ToString()) as Textbox;
An alternative to using a CheckBoxList that is prepared for variable sizes by design and having a fixed list of 10 TextBoxes next to it, would be to create a Repeater with a CheckBox and a TextBox in its ItemTemplate. Downsize is that you'd have to create some DTOs for DataBinding and also had to use some more dynamic control resolution (both for the CheckBox and the TextBox ).

converting cells into text boxes where check box is checked

I am using mysql and asp.net with c#. I have a grid view which will display dynamically selected table data. I am able to display the data of selected table. In the first column i have added a check box and a Button outside Grid view. When user selects Check box and clicks on button, the selected rows must turn into text boxes. I am able to find the seleted check box, but i'm unable to convert the cells into text boxes. Here's my code:
int n = GridView1.HeaderRow.Cells.Count;
for( int i=0; i < GridView1.Rows.Count;i++)
{
GridViewRow row = GridView1.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
{
for( int j=0;j<n;j++)
{
TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
}
}
}
At this line: TextBox txt = ((TextBox)GridView1.Rows[i].Cells[j]).Text;
i get a warning :
cannot convert 'System.Web.UI.Controls.TableCell' to type 'System.Web.UI.Controls.TextBox
I am unable to resolve this. Please help. Thank you
Try this.
You can remove one or few lines based on your hands-on with C#.
Concept is, you should create a TextBox, assign cell text that textbox and then Add that newly created textbox to child controls of Grid Cell of particular row.
Mark this solution if you found useful.
bool isChecked = ((CheckBox)row.FindControl("CheckBox1")).Checked;
if(isChecked)
{
for( int j=0;j<n;j++)
{
TextBox tbForCell = new TextBox();
tbForCell.Text = GridView1.Rows[i].Cells[j].Text;
GridView1.Rows[i].Cells[j].Text = "";
GridView1.Rows[i].Cells[j].Controls.Add(tbForCell);
}
}
If you want to avoid the TextBox to appear in CheckBox Column please initialise loop variable j with 1 instead of 0.
for( int j=1;j<n;j++)
Your method call is finding the cell. The Textbox is a control contained within the cell. Try something like this instead:
TextBox txt = ((TextBox) GridView1.Rows[i].Cells[j].FindControl("textbox name")).Text;
The FindControl method is documented here.
Also take #Bartdude's advice about using editable gridviews. If one will work at least as well as what you're trying to hand-roll, it's worth the time learning how to use it.

Categories