I have a checkedListBox on a winform.
I fill the check box with some code see below.
All works well.
Now I need to get the value of the checked items. I am having some issues.
When I do the foreach(var item in clbtest.CheckedItems)
I get this when doing a Immediate window... ? item
{ Text = "Depos", Value = "Q:\\Scanning Department\\_DT SEARCH INDEXES\\Depos" }
Text: "Depos"
Value: "Q:\\Scanning Department\\_DT SEARCH INDEXES\\Depos"
I don't know how to get at the Value field. I have tried several ways but nothing seems to work.
private void FillIndexes()
{
string stext = cblIndexToSearch.Text;
cblIndexToSearch.Items.Clear();
cblIndexToSearch.Text = "";
cblIndexToSearch.DisplayMember = "Text";
cblIndexToSearch.ValueMember = "Value";
foreach (var item in indexlist)
{
cblIndexToSearch.Items.Insert(0,
new { Text = item.indexName, Value = #item.indexPath });
}
}
Hope this helps. Just create a checklistbox named "checkedListBox1" and the code should work. I detailed a little on what it does.
checkedListBox1.Items.Clear();
checkedListBox1.Text = "";
checkedListBox1.DisplayMember = "Text";
checkedListBox1.ValueMember = "Value";
checkedListBox1.Items.Insert(0,
new { Text = "Rawr", Value = "Whatever 2011"});
string temp = checkedListBox1.Items[0].ToString(); //gets the string of the item. (switch 0 to the index you want)
string string_Value = temp.Split(new string[] { "Value = " }, StringSplitOptions.None)[1]; //splits the string by the value part and returns the string value.
string_Value = string_Value.Substring(0, string_Value.Length - 2); ; //removes the last 2 characters since they are not part of the value.
//you now have the value in string form.
Related
I have the following function which works fine and produces the results when their is one table selected. But when there is two tables being selected my _newList function is just replacing the fields in the array I will show you in graphical form best i can.
private void genXmlSchema_Click(object sender, EventArgs e)
{
List<TableDefnition> _newList = new List<TableDefnition>();
PersistentObject _testObjects = new PersistentObject();
List<GridViewRowInfo> _checkRows = GetCheckedRows(rgTableNames);
List<PersistentObject> _testObjectsList = new List<PersistentObject>();
foreach (GridViewRowInfo row in _checkRows)
{
var currentRow = (TableNames)row.DataBoundItem;
_newList = db.GetALLTableDeiniations(currentRow.TABLE_NAME);
rgFieldsOfTable.DataSource = db.GetALLTableDeiniations(currentRow.TABLE_NAME);
_testObjectsList.Add( BuildSchema(_newList, currentRow.TABLE_NAME));
}
_newPObject.PersistentObjects.AddRange(_testObjectsList);
_newPObject.ClassPrefix = "Persistent";
_newPObject.ClassSuffix = "";
_newPObject.Language = "VB";
_newPObject.Path = #"C:\Sage200SchemaExtensions";
_newPObject.GenerateSeparateFiles = "false";
_newPObject.GenerateBusinessObjects = "false";
_newPObject.BaselineSchema = #"C:\Program Files (x86)\Sage 200 SDK\SageObjectStore.xml";
_newPObject.DataTypes = "";
_newPObject.Enumerations = "";
_newPObject.MemberVariablePrefix = "_";
_newPObject.ApplicationNamespace = "BusinessObjects";
schemeContent.Text = HelperXml.ToXML(_newPObject);
}
On the second pass you will that it has duplicated the second table in both of the lists.
First Array Number after second pass
Second Array Number after second pass
As you can clearly see its lost the reference to the first table with 4 columns completely I no its something simple to do with my list creation and destruction but cant figure it out
I want to get a couple of values from a textfile in C#. Example:
1.sex=male
1.name=barack
1.lastname=obama
1.age = 55
2.sex=female
2.name= kelly
2.lastname=clinton
2.age = 24
3.sex = male
3.firstname= mike
3.lastname= james
3.age= 19
I only want to get all the "name", "lastname" and ages from the textFile, not the "sex". How can I filter this? I have tried something like this, but it only shows 1 value.
var list = new List<string>();
var text = File.ReadAllLines(#"C:\Users\Jal\Desktop\Test.text");
foreach (var s in text)
{
if (s.Contains("Name"))
{
if (s.Contains("Name"))
{
var desc = s.Substring(s.IndexOf("=") + 1);
list.Add(desc);
ListView.Items.Add(desc);
}
}
}
I found this code on Stack Overflow, but it doesn't get all of the values I want.
var names = new List<string>();
var lastnames = new List<string>();
var text = File.ReadAllLines(#"C:\Users\Jal\Desktop\Test.text");
foreach (var s in text)
{
if (s.Contains("lastname"))
{
var lastname = s.Substring(s.IndexOf("=") + 1);
lastnames.Add(lastname);
continue;
}
if (s.Contains("name"))
{
var name = s.Substring(s.IndexOf("=") + 1);
names.Add(name);
continue;
}
}
And in same way you can add another properties.
s.Contains("Name") won't ever be true on this case because it's case-sensitive, and your string in the file is "name".
Try using s.Contains("name")
But you would be better off using a Regex for this kind of thing.
I have a combobox farmRegion that I fill in this way
private void fillRegionData() {
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Description", typeof(string));
farmRegion.ValueMember = "ID";
farmRegion.DisplayMember = "Description";
farmRegion.SelectedValue = "ID";
for (int i = 0; i < StaticData.RegionNames.Count; i++)
{
dt.Rows.Add(StaticData.RegionValues[i], StaticData.RegionNames[i]);
}
farmRegion.DataSource = dt;
}
where StaticData.RegionNames is:
public static List<string> RegionNames = new List<string>() { "Select Region", "EASTERN", "WESTERN", "NORTHERN", "EASTERN2", "NORTHERN", "MIDDLE" };
and StaticDate.RegionValues is
public static List<string> RegionValues = new List<string>() { "-10", "1", "2", "3", "4", "5", "6" };
when I save the form, I save the text of the combobox, not the value (this is a requirement issue).
now I want to reload the comboxbox again.
I already know the text but I need to make the combobox fires and the option text is already selected.
I tried this:
farmRegion.Text = myText
but still the first option is selected.
Before setting the text farmRegion.Text = myText put a break point and checks the combobox datasource, and ensure myText is present in combobox.
If you handled any events of combobox put a break point on that events and check what happend after the execution of farmRegion.Text = myText statement.
These two steps doen't solve your issue then find out the index of your text value as
int index = farmRegion.FindString(myText);
farmRegion.SelectedIndex = index;
You can try this
farmRegion.SelectedIndex = farmRegion.FindStringExact(myText)
Another Approach:
Note: This may throw argumentexception if the item was not found.
farmRegion.SelectedIndex = farmRegion.Items.IndexOf(myText);
try like this,
DataRow[] drs = ((DataTable)cmb.DataSource).Select("Description='" + myText + "'");
if (drs.Length > 0)
{
cmb.SelectedValue = drs[0]["ID"].ToString();
}
else
{
//Value not found
}
EDITED:
Sometime when you set the text of combobox will not return the value of ValueMember from SelectedValue it may return null and SelectedIndex may return -1.
I'm trying to bind my printers name in the dropdown and managed show all the printer name in my dropdown. When I try to get the value of selected item, it show the first value for all the printer. Below is the code
Code to bind the printer name:
PrintModuleAX printModuleAX = new PrintModuleAX();
var result = printModuleAX.GetAllPrinterNames();
JObject o = JObject.Parse(result);
JArray sizes = (JArray)o["PrinterNames"];
var dt = new DataTable();
dt.Columns.Add("PrinterValue");
dt.Columns.Add("PrinterName");
for (int i = 0; i < sizes.Count; i++)
{
dt.Rows.Add((string)sizes[i], (string)sizes[i]);
}
ddlPrinterName.DataSource = dt;
ddlPrinterName.DataTextField = dt.Columns["PrinterName"].ToString();
ddlPrinterName.DataValueField = dt.Columns["PrinterValue"].ToString();
ddlPrinterName.DataBind();
code to get the selected value:
var printername = ddlPrinterName.Text.ToString();
var printername1 = ddlPrinterName.SelectedValue.ToString();
var printername2 = ddlPrinterName.SelectedItem.ToString();
printername, printername1, printername2 show the same value even though I chose a different printer.
Check that the databinding is not happening every page load. (use if (!page.ispostback))
Try with below code
ddlPrinterName.DataSource = dt;
//just specify the column name
ddlPrinterName.DataTextField = "PrinterName";
ddlPrinterName.DataValueField = "PrinterValue";
ddlPrinterName.DataBind();
var printername1 = ddlPrinterName.SelectedItem.Text; //gives text
var printername2 = ddlPrinterName.SelectedItem.Value; //gives value
first of all there is a searchbox form and a view form. after passing the value of the id in the searchbox, it should return all the values that matches with the id of that person after the textchange method occured. but it doesn't display a single value on the textboxes. here is my code
public void first_tab_search(string key)
{
key = txtSearch.Text;
var first = from a in dbcon.personal_informations where a.last_name == key select a;
foreach (var setThem in first)
{
txtsurname.Text = setThem.last_name;
txtfirstname.Text = setThem.first_name;
txtmiddlename.Text = setThem.middle_name;
txtID.Text = setThem.userid;
txtweight.Text = setThem.weight;
txttin.Text = setThem.tin;
txtsss.Text = setThem.sss;
txtaeno.Text = setThem.agency_employee_no;
txtbloodtype.Text = setThem.blood_type;
txtcitizenship.Text = setThem.citizenship;
txtcivilstatus.Text = setThem.civil_status;
txtcpno.Text = setThem.cell_no;
txtdob.Text = setThem.datetime_of_birth.ToString();
txtemail.Text = setThem.email_address;
txtgender.Text = setThem.sex;
txtgsis.Text = setThem.gsis_id;
txtheight.Text = setThem.height;
txtnameext.Text = setThem.name_ext;
txtpagibig.Text = setThem.pagibig_id;
txtpermaaddr.Text = setThem.permanent_address;
txtpermatelno.Text = setThem.permanent_telno;
txtpermazip.Text = setThem.permanent_zipcode;
txtphilhealth.Text = setThem.philhealth;
txtpob.Text = setThem.place_of_birth;
txtresidentialaddr.Text = setThem.residential_address;
txtresitelno.Text = setThem.residential_telno;
txtresizip.Text = setThem.residential_zipcode;
txtweight.Text = setThem.weight;
}
}
You have a whole host of problems going on here.
You pass a key into the method, and then immediately overwrite it with the contents of your search box.
Your search could return more than one result, and therefore your code is looping through each result and overwriting the output values with the last returned row. Use += rather than + in your loop, i.e.
txtsurname.Text += setThem.last_name;
Your code is currently case sensitive, this may be the desired approach but might not be.