I have a Combobox listing the school units of my DB. My code:
_fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
QueryUnidade->Close();
QueryUnidade->Open();
while (QueryUnidade->Eof == false){
ComboBoxUn->Items->Add(QueryUnidade->FieldByName("unidade")->AsString);
QueryUnidade->Next();
}
}
In my other need combobox list the shifts that are associated with unit chosen in the first combobox, but is not showing up. My code looks like this:
void __fastcall TForm1::ComboBoxTurnoChange(TObject *Sender) {
QueryTurno->Close();
QueryTurno->ClearFields();
QueryTurno->SQL->Add("SELECT DISTINCT TURNO FROM ALUNO WHERE UNIDADE ='"+ (Trim(ComboBoxUn->Text)+"'"));
QueryTurno->Open();
while(QueryTurno->Eof == false){
ComboBoxTurno->Items->Add(QueryTurno->FieldByName("turno")->AsString);
QueryTurno->Next();
}
ComboBoxTurno->Update();
}
The problem was that my query should belongs to Event (on select) of ComboboxUn and not to ComboxTurno. New code of ComboboxUn that show the result in comboxTurno, like i wanted:
void __fastcall TForm1::ComboBoxUnSelect(TObject *Sender)
{
QueryTurno->Close();
QueryTurno->SQL->Add("SELECT DISTINCT TURNO FROM ALUNO WHERE
UNIDADE="+QuotedStr(Trim(ComboBoxUn->Text)));
QueryTurno->Open();
while(QueryTurno->Eof == false)
{
ComboBoxTurno->Items->Add(QueryTurno->FieldByName("turno")->AsString);
QueryTurno->Next();
}
}
Related
I crated an application using DialogBox. I added a ListView to it.
Is there a way to make this Listview non-selectable?
The user should not select anything, it should just display some data.
Thanks in advance!
In order to disable list items on list creation you have to subclass from ArrayAdapter. You have to override the following methods: isEnabled(int position) and areAllItemsEnabled(). In former you return true or false depending is list item at given position enabled or not. In latter you return false.
If you want to use createFromResource() you will have to implement that method as well, since the ArrayAdapter.createFromResource() still instantiates ArrayAdapter instead of your own adapter.
Finally, the code would look something like the following:
class MenuAdapter extends ArrayAdapter<CharSequence> {
public MenuAdapter(
Context context, int textViewResId, CharSequence[] strings) {
super(context, textViewResId, strings);
}
public static MenuAdapter createFromResource(
Context context, int textArrayResId, int textViewResId) {
Resources resources = context.getResources();
CharSequence[] strings = resources.getTextArray(textArrayResId);
return new MenuAdapter(context, textViewResId, strings);
}
public boolean areAllItemsEnabled() {
return false;
}
public boolean isEnabled(int position) {
// return false if position == position you want to disable
}
}
Using datagridview bound to BindingSource control bound to a LINQ to SQL class, I wonder how to position the bindingSource to a specific record, that is, when I type a Product name in a textbox, the bindingsource should move to that specific product. Here is my code:
In my form FrmFind:
NorthwindDataContext dc;
private void FrmFind_Load(object sender, EventArgs e)
{
dc = new NorthwindDataContext();
var qry = (from p in dc.Products
select p).ToList();
FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
int index = productBindingSource.Find("ProductName", tb.Text);
if (index >= 0)
{
productBindingSource.Position = index;
}
}
In the program class:
public class FindAbleBindingList<T> : BindingList<T>
{
public FindAbleBindingList()
: base()
{
}
public FindAbleBindingList(List<T> list)
: base(list)
{
}
protected override int FindCore(PropertyDescriptor property, object key)
{
for (int i = 0; i < Count; i++)
{
T item = this[i];
//if (property.GetValue(item).Equals(key))
if (property.GetValue(item).ToString().StartsWith(key.ToString()))
{
return i;
}
}
return -1; // Not found
}
}
How can I implement the find method to make it work?
You can combine the BindingSource.Find() method with the Position property.
For example, if you have something like this in your TextBox changed event handler:
private void textBox1_TextChanged(object sender, EventArgs e)
{
TextBox tb = sender as TextBox;
int index = bs.Find("Product", tb.Text);
if (index >= 0)
{
bs.Position = index;
}
}
This of course will depend on a lot of things like the particular implementation of the Find method the data source for the binding source has.
In a question you asked a little while ago I gave you an implementation for Find which worked with full matches. Below is a slightly different implementation that will look at the start of the property being inspected:
protected override int FindCore(PropertyDescriptor property, object key)
{
// Simple iteration:
for (int i = 0; i < Count; i++)
{
T item = this[i];
if (property.GetValue(item).ToString().StartsWith(key.ToString()))
{
return i;
}
}
return -1; // Not found
}
Do note that the above method is case sensitive - you can change StartsWith to be case insensitive if you need.
One key thing to note about the way .Net works is that the actual type of an object is not sufficient all the time - the declared type is what consuming code knows about.
This is the reason why you get a NotSupported exception when calling the Find method, even though your BindingList implementation has a Find method - the code that receives this binding list doesn't know about the Find.
The reason for that is in these lines of code:
dc = new NorthwindDataContext();
var qry = (from p in dc.Products
select p).ToList();
FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
When you set the data source for the binding source you include the extension method OrderBy - Checking this shows that it returns IOrderedEnumerable, an interface described here on MSDN. Note that this interface has no Find method, so even though the underlying FindableBindingList<T> supports Find the binding source doesn't know about it.
There are several solutions (the best is in my opinion to extend your FindableBindingList to also support sorting and sort the list) but the quickest for your current code is to sort earlier like so:
dc = new NorthwindDataContext();
var qry = (from p in dc.Products
select p).OrderBy(p => p.ProductName).ToList();
FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
productBindingSource.DataSource = list;
In WinForms there are no entirely out of the box solutions for the things you are trying to do - they all need a little bit of custom code that you need to put together to match just your own requirements.
I took a different approach. I figured, programmatically, every record must be checked until a match is found, so I just iterated using the MoveNext method until I found a match. Unsure if the starting position would be the First record or not, so I used the MoveFirst method to ensure that is was.
There is one assumption, and that is that what you are searching for is unique in that column. In my case, I was looking to match an Identity integer.
int seekID;
this.EntityTableBindingSource.MoveFirst();
if (seekID > 0)
{
foreach (EntityTable sd in EntityTableBindingSource)
{
if (sd.ID != seekID)
{
this.t_EntityTableBindingSource.MoveNext();
}
else
{
break;
}
}
}
I didn't really care for either answer provided. Here is what I came up with for my problem:
// Create a list of items in the BindingSource and use labda to find your row:
var QuickAccessCode = customerListBindingSource.List.OfType<CustomerList>()
.ToList().Find(f => f.QuickAccessCode == txtQAC.Text);
// Then use indexOf to find the object in your bindingSource:
var pos = customerListBindingSource.IndexOf(QuickAccessCode);
if (pos < 0)
{
MessageBox.Show("Could not find " + txtQAC.Text);
}
else
{
mainFrm.customerListBindingSource.Position = pos;
}
In an MVVM environment, I have a ListCollectionView bound to an ObservableCollection. My Foo objects have a IsDefault property to them, and my requirement is to have that item first in the list, and the rest should be alpha-sorted.
So this code only sorts the whole list, obviously:
_list = new ListCollectionView(Model.Data);
_list.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
Not sure how to make sure that item #3 (for example, which has IsDefault=true) be at the top of the list, and the rest (that have IsDefault=false) be alpha sorted.
Is this a case to use _list.CustomSort and implement IComparer in some way?
Yes, this is exactly the case where you need to use ListCollectionView.CustomSort. Custom sorting is mutually exclusive with using SortDescriptions; the documentation for the former is explicit about this:
Setting this property clears a previously set SortDescriptions value.
So what you need to do is define an IComparer and use it to sort the view:
class CustomComparer : IComparer
{
public int Compare (object lhs, object rhs)
{
// missing: checks for null, casting to Model.Data, etc etc
if (lhsModelData.IsDefault && rhsModelData.IsDefault) {
return lhsModelData.Name.CompareTo(rhsModelData.Name);
}
else if (lhsModelData.IsDefault) {
return -1;
}
else if (rhsModelData.IsDefault) {
return 1;
}
else {
return lhsModelData.Name.CompareTo(rhsModelData.Name);
}
}
}
I have a datagrid with binding item source. I have set CanUserSortColumns property of datagrid into TRUE and so do with all inner columns in datagrid but user still doesn't be able to sort columns.
Is there something I have missed ?
Are you explicitly defining DataTemplate for your headers? In case yes you have to set property on your column "SortMemberPath" to your CLR property on which you want to sort your column. This link might prove helpful to you, have a look at it -
WPF4 Datagrid doesn't sort on column headers
Thanks guys. That worked. I just want to add.
The types of those columns must implement the non-generic IComparable, which is usually not a problem if you are using primitive or .net types. But if you have your own types, then you will have to add it.
E.g.
/* this is my own type */
public struct Distance : ..., IComparable, IComparable<Distance>, ... {
...
public int CompareTo(object obj)
{
if (obj == null) { return 1; }
if (obj.GetType() != typeof(Distance)) { return 0; }
return CompareTo((Distance)obj);
}
public int CompareTo(Distance other) { return _meters.CompareTo(other._meters); }
}
Is there a way to make some of the items in a ListBox readonly/disabled so they can't be selected? Or are there any similar controls to ListBox to provide this functionality?
ListBox doesn't have support for that. You can bolt something on, you could deselect a selected item. Here's a silly example that prevents even-numbered items from being selected:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
for (int ix = listBox1.SelectedIndices.Count - 1; ix >= 0; ix--) {
if (listBox1.SelectedIndices[ix] % 2 != 0)
listBox1.SelectedIndices.Remove(listBox1.SelectedIndices[ix]);
}
}
But the flicker is quite noticeable and it messes up keyboard navigation. You can get better results by using CheckedListBox, you can prevent the user from checking the box for an item:
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (e.Index % 2 != 0) e.NewValue = CheckState.Unchecked;
}
But now you cannot override drawing to make it look obvious to the user that the item isn't selectable. No great solutions here, it is far simpler to just not display items in the box that shouldn't be selectable.
#Hans solution causing that the item id selected for a short time and then selection disappearing. I don't like that - this can be confusing for the enduser.
I prefer to hide some edit option buttons for the item that should be disabled:
if (lbSystemUsers.Items.Count > 0 && lbSystemUsers.SelectedIndices.Count > 0)
if (((RemoteSystemUserListEntity)lbSystemUsers.SelectedItem).Value == appLogin)
{
bSystemUsersDelete.Visible = false;
bSystemUsersEdit.Visible = false;
}
else
{
bSystemUsersDelete.Visible = true;
bSystemUsersEdit.Visible = true;
}
Here is the list that lists the users and disallow to edit user that is actually logged in to the edit panel.
ListBox doesn't have a ReadOnly (or similar) property, but you can make a custom ListBox control. Here's a solution that worked pretty well for me:
https://ajeethtechnotes.blogspot.com/2009/02/readonly-listbox.html
public class ReadOnlyListBox : ListBox
{
private bool _readOnly = false;
public bool ReadOnly
{
get { return _readOnly; }
set { _readOnly = value; }
}
protected override void DefWndProc(ref Message m)
{
// If ReadOnly is set to true, then block any messages
// to the selection area from the mouse or keyboard.
// Let all other messages pass through to the
// Windows default implementation of DefWndProc.
if (!_readOnly || ((m.Msg <= 0x0200 || m.Msg >= 0x020E)
&& (m.Msg <= 0x0100 || m.Msg >= 0x0109)
&& m.Msg != 0x2111
&& m.Msg != 0x87))
{
base.DefWndProc(ref m);
}
}
}
I know this is old thread, but i'll post a workaround for other readers in future.
listBox.Enabled = false;
listBox.BackColor = Color.LightGray;
This will change background color of list box to Light Gray. So this is not builtin "native way" to do it, but at least gives user some feedback that he is not supposed to / can't edit that field.
To get read-only behaviour I have MyCBLLocked, a boolean associated with the MyCBL checkbox list control, and on the CheckItem event I do:
private void MyCBL_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (MyCBLLocked)
e.NewValue = e.CurrentValue;
}
So instead of
MyCBL.Enabled = false;
I use
MyCBLLocked = true;
and the user can scroll through the many selections but not mess things up with changes.