I have a form which contains infinite number of options which user and add.
Some options are in textbox, and some are in combobox(selected is the value to be extracted).
The form is in a way that user can add as many combo box and text box he wants and let him write those information onto XML.
How do I code that in c#? If anyone can give me a short example of one each which loops through each added combo box and text box, that would be great.
Thank you in advance.
You can iterate through all controls inside a form like this.
foreach(Control control in this.Controls)
{
//here 'this' is representing the form you want to iterate through
//you can check whether it is a combobox or a text box
if(control.GetType() == typeof(Combobox))
{
//this is a combo box
}
else if(control.GetType() == typeof(Textbox))
{
//this is a text box
}
}
using above method you will find the controls inside a particular form. After that you can write information in a XML file
Related
Using C# and WinForms, I can set a form to be localizable, put a label on it, and set text for the label in as many languages as I want. The text will be stored as string resources in one resource file for each language. Then, a user can select a language and all labels on the form will change to the correct language.
This does not seem to work for combo boxes. I can add items to a combo box for a localizable form and they will be stored in resource files using names such as ComboBox1.Item, ComboBox1.Item1, and ComboBox1.Item2, but the displayed text does not change when the combo box changes.
I've seen various suggestions for how to localize combo boxes, based on binding them to dictionaries or lists of tuples, but it seems to me that if items are stored in resource strings, there should be some more automatic way to use those resource strings. Is there?
Edit: Here is what should be a minimal example. A form has a text box, a button, a label and a combo box. The label and combo box each have resources for French (fr-FR) and Spanish (es-ES). The language name is entered in the text box, and the button changes the form's language using the following method:
private void ChangeLanguage(string lang)
{
ComponentResourceManager crm = new ComponentResourceManager(typeof(Form2));
CultureInfo culture = CultureInfo.CreateSpecificCulture(lang);
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
foreach (Control c in this.Controls)
{
crm.ApplyResources(c, c.Name, culture);
}
}
The result is that the label's text changes but the text of the combo box items does not.
If you close and reopen the form, everything will work fine. But if you would like to change the culture without closing the form, you need to add extra processing for ComboBox:
if (c is ComboBox)
{
var combo = (ComboBox)c;
var count = combo.Items.Count;
combo.Items.Clear();
combo.BeginUpdate();
for (int i = 0; i < count; i++)
{
var number = i == 0 ? "" : $"{i}";
var item = crm.GetString($"{c.Name}.Items{number}");
combo.Items.Add(item);
}
combo.EndUpdate();
}
crm.ApplyResources(c, c.Name);
Also keep in mind that your function is just applying the resource on the controls on the form and it's ignoring nested controls. For example, if some controls are hosted on a panel, it will ignore them. To fix this issue, take a look at this post.
Note:
In general , I recommend restarting the form to apply new language, because the custom logic is not limited to ComboBox, you need specific logic for ComboBox, ListBox, ListView, TreeView, DataGridView, ToolStrip, ContextMenuStrip, MenuStrip, StatusStrip and maybe smoe other controls which I forget to mention.
In short, I believe saving the selected culture in a setting and then Application.Restart() and applying culture in Main method is what you are looking for.
Is there a way in C# to reference a control, in my case a TextBox, by using the value of a string variable? I am using the code below to make a single method that multiple control can use for the 'LostFocus' event. The sender TextBox then needs to calculate results based on the contents of other TextBoxes. The problem is that there are about 12 rows of TextBoxes, and while this code works to reuse the event method, I can't think of a way to reference the correct boxes that are not the sender. All of the boxes have similar names (ex - miCellSaturation, miCellRecords, orSaturation, orRecords), so my thinking was that if I can isolate part of the TextBox name with a Substring command, and then concatenate that with another string to form the complete TextBox name, this would work. I can do all that, but I don't know of a way to use the concatenated string to reference that box. Would this require iterating through all the boxes until it matches the correct name?
TextBox box = (TextBox)sender;
string boxName = box.Name;
if(boxName.EndsWith("Saturation"))
{
}
Not sure to understand you problem correctly, but if you need to find the references to a particular type of control with its name ending with a predefined string, then you could use
var list = YourForm.Controls.OfType<TextBox>()
.Where(x => x.Name.EndsWith("YourString"));
foreach(TextBox t in list)
{
Console.WriteLine(t.Name);
......
}
This could work only if your searched controls are directly included in the controls collection of the form. If these textboxes are included in some control container then you need to apply these lines to the appropriate control container instead of the form
I have text box to fill the ph.numbers in that, here what i want is, whenever i'm entering the value in that text box i need to add another empty text box dynamically where i can add another value in that in the same way if they are entering any value into this new empty box another empty text box should add at the bottom of this. And after all there is a confirm link button. When clicking on it all these ph.numbers entering on the text boxes should get. How is it possible?
You add textboxes dynamically using jquery and on the button click/submit there values to the server. What you can do is on keyup event of a textbox you can add another below it.
Try this.
I'm basing this condition that your number format is of 11, Ex: 09123456789
So on the Textbox_Textchanged event of your textbox.
if(textbox.text.length >= 11)
{
Textbox text = new Textbox();
text.TextChanged += Textbox_Textchanged; // So it would also trigger the event
// Do positioning here
}
Im thinking you can better optimize this by having a better EventArgs but you get the idea right?
I have no idea how to do this.
What I am trying to do is to allow user to copy certain field by using ether right click mouse or keyboard shortcut.
I need this because I am storing some fields as a code which cannot be retyped easily.
ListView allow to select only individual row at the time, and I want to select only one field of the whole table layout.
How do I do this?
PS. Or, how do I allow user to modify the content of each field. At least when they can modify the field they can copy/paste the content (changes will not be saved to my database file).
To allow the user to edit the item, you can set LabelEdit on the ListView to true. Here is the description from MSDN:
"When the LabelEdit property is set to true, the user is able to modify the text of an item by clicking the item text to select it and then clicking the item text again to put the label text into edit mode. The user can then modify or replace the item's text label."
Note that this does not apply to the subitems.
Copying the data out could be slightly more involved depending on the user interface that you desire (i.e. button or context menu). The easy solution would be to add a button to the form that, when pressed, would copy the content of the selected item (or any of its subitems) to the clipboard.
private void button1_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count != 0)
{
Clipboard.SetText(listView1.SelectedItems[0].Text);
}
}
There is some "Clipboard" class
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
You could simply make a button or something, and set the selected item text in the clipboard.
I have a pretty simple form with a listbox, a text box, and two buttons.
The list box items are populated from a sql database table. The user may chose to select one or multiple items from the listbox.
The text box is used to write more details about the items in the listbox. One button can then be clicked to update another database table with these details.
I want to make it where if any items are selected from the listbox, those contents are automatically copied into the text box field on the fly as they are selected.
Is this possible?
I've been able to make this happen on the button click event - just not on the fly as they are selected. I want it to occur before the additional details are being sent to the database
I've also tried using several different listbox events, but have been unable to obtain the results I am looking for.
Any suggestions?
try this out
you will have to handle the SelectedIndexChanged event on the listbox.
here is an example with example controls.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = "";
foreach (string nextitem in listBox1.SelectedItems)
{
textBox1.Text += nextitem + " ";
}
}
im not too sure HOW you want the text to appear in the textbox so that would be up to you in the foreach loop.
yes, the SelectedIndexChanged event fires on every selection change, and you can concatenate together the items in the listbox. But if you are talking the description that's not visible too, you need to store the description in each listboxitem tag property, and in your code retrieve the description from there.