Get value from ComboBox? - combobox

I have in MyForm a ComboBox (myComboBox ) with EnumType myEnumType .
In the modified method , I want to get the value.
I used the code looklike:
if (myComboBox.enumTypeValue() == myEnumType::Value1 )
{
//action
}
Or anothe way
if (myComboBox.enumType(myEnumType::Value1) )
{
//action
}
In both cases I can not take the value.
What is the way? Thanks, all!
Enjoy!

To get the value of the ComboBox use the selection method,
if(myComboBox.selection() == myEnumType::Value1) {
//action
}

I used the code looklike:
public boolean modified()
{
boolean ret;
if(btMailTxt.valueStr()== 'FR')
{
btMailSubject.text('ooox');
btMailBody.text('Bonjouro');
}
if(btMailTxt.valueStr()== 'NL')
{
btMailSubject.text('assigntest');
btMailBody.text('Bonjour');
}
ret = super();
return ret;
}

Related

trying to find an Element on DOM with typescript

I m totally newbie in typescript. I m just trying to find an element with a selector. whatever i tried findElement() method is always turning undefined. Where am i doing wrong ?
Any help greatly appreciated !
var cdnBasePath: any = this.findElement('meta[name="cdnBasePath"]').attr('content');
public findElement(selector: any) {
if (angular.isDefined(this.$window.jQuery)) {
return this.$document.find(selector);
} else {
return angular.element(this.$document.querySelector(selector));
}
}
In order for "this" to be defined you have to declare your findElement as a instance method of some class. Like this:
class SomeClass
{
public SomeMethod()
{
var cdnBasePath: any = this.findElement('meta[name="cdnBasePath"]').attr('content');
}
public findElement(selector: any)
{
if (angular.isDefined(this.$window.jQuery)) {
return this.$document.find(selector);
} else {
return angular.element(this.$document.querySelector(selector));
}
}
}
Otherwise you can have it as static method:
class UI
{
public static findElement(selector: any)
{
if (angular.isDefined(this.$window.jQuery)) {
return this.$document.find(selector);
} else {
return angular.element(this.$document.querySelector(selector));
}
}
}
var cdnBasePath: any = UI.findElement('meta[name="cdnBasePath"]').attr('content');
Or just drop 'this' and have it as global function (I do not recommend this)
function findElement(selector: any)
{
if (angular.isDefined(this.$window.jQuery)) {
return this.$document.find(selector);
} else {
return angular.element(this.$document.querySelector(selector));
}
}
var cdnBasePath: any = findElement('meta[name="cdnBasePath"]').attr('content');
Hope this helps.

How to refresh the binding again using mvvmlight

The CurrentSelectedBall is updated whenever I changed its value due to two-way binding.
When I click to update the function UpdateRedBall is called so the redball in the database is updated. But the view list of balls is not updated as the ObservableCollection<RedBall> RedBalls is not changed at all.
How to fix this problem? I guess something needs to be done after _context.SaveChanges();
Also I can not simply do DataGridA.itemsSource = RedBalls to make a hard refresh here as first DataGridA is not accessible in the MainviewModel.
Some of the methods:
public ObservableCollection<RedBall> RedBalls
{
get { return new ObservableCollection<RedBall>(_context.RedBalls.OrderBy(m=>m.DateNumber));}
set
{
_redBalls = value;
RaisePropertyChanged("RedBalls");
}
}
public RedBall CurrentSelectedRedBall
{
get { return _currentSelectedRedBall; }
set
{
_currentSelectedRedBall = value;
RaisePropertyChanged("CurrentSelectedRedBall");
}
}
private void UpdateRedBall()
{
if (CurrentSelectedRedBall != null)
{
var ballToUpdate = _context.RedBalls.SingleOrDefault(x => x.Id == CurrentSelectedRedBall.Id);
ballToUpdate.DateNumber = CurrentSelectedRedBall.DateNumber;
ballToUpdate.First = CurrentSelectedRedBall.First;
ballToUpdate.Second = CurrentSelectedRedBall.Second;
ballToUpdate.Third = CurrentSelectedRedBall.Third;
ballToUpdate.Fourth = CurrentSelectedRedBall.Fourth;
ballToUpdate.Fifth = CurrentSelectedRedBall.Fifth;
ballToUpdate.Sixth = CurrentSelectedRedBall.Sixth;
}
_context.SaveChanges();
//RedBalls = RedBalls
}
I have a strong dislike for getters that 'do stuff'. It should be impossible for a get to fail. I would consider moving the logic in your RedBalls getter elsewhere. I'm also concerned that you're newing up the RedBalls ObservableCollection on every get as it can cause problems with bindings. Bound ObservableCollections should be instantiated once, then altered using Add, Remove and Clear. Working with them this way should also solve the problem of updating your collection appropriately.
public ObservableCollection<RedBall> RedBalls
{
get { return _redBalls; }
set
{
_redBalls = value;
RaisePropertyChanged("RedBalls");
}
}
public RedBall CurrentSelectedRedBall
{
get { return _currentSelectedRedBall; }
set
{
_currentSelectedRedBall = value;
RaisePropertyChanged("CurrentSelectedRedBall");
}
}
private void UpdateCurrentSelectedRedBall()
{
UpdateRedBall();
var redBalls = _context.RedBalls.OrderBy(m=>m.DateNumber);
if(redBalls != null)
{
RedBalls.Clear();
foreach(RedBall rb in redBalls)
{
RedBalls.Add(rb);
}
}
}
private void UpdateRedBall()
{
if (CurrentSelectedRedBall != null)
{
var ballToUpdate = _context.RedBalls.SingleOrDefault(x => x.Id == CurrentSelectedRedBall.Id);
ballToUpdate.DateNumber = CurrentSelectedRedBall.DateNumber;
ballToUpdate.First = CurrentSelectedRedBall.First;
ballToUpdate.Second = CurrentSelectedRedBall.Second;
ballToUpdate.Third = CurrentSelectedRedBall.Third;
ballToUpdate.Fourth = CurrentSelectedRedBall.Fourth;
ballToUpdate.Fifth = CurrentSelectedRedBall.Fifth;
ballToUpdate.Sixth = CurrentSelectedRedBall.Sixth;
}
_context.SaveChanges();
}
Use something like this: https://gist.github.com/itajaja/7507120 - basically you need to subscribe for PropertyChanged and raise CollectionChanged events from that.

Do event after PropertyChanged Event is done

I am trying to do a "Clear Flag" in my code. I have data being shown on the UI continuously, and I want the user to be able to clear the data when the code is running with a "Clear" button. I have a second thread collecting and updating the data on the UI Thread. I tried to use a flag in the properties that when it sees the "Clear Flag" is true to rests to a predetermined value. When the clear button is pressed I set my "Clear Flag" to true and it clears the data, but my problem is knowing when it is done and setting the "Clear Flag" back to false when it is done.
private bool _clear;
public bool Clear
{
get
{
return _clear;
}
set
{
_clear = value;
OnPropertyChanged("Clear");
if (value)
{
OnPropertyChanged(String.Empty);
}
}
}
private int _motorRPM;
public int MotorRPM
{
get
{
return _motorRPM;
}
set
{
_motorRPM = value;
OnPropertyChanged("MotorRPM");
}
}
private int _aveRPM;
public int AveRPM
{
get
{
return _aveRPM;
}
set
{
if (Clear)
{
_aveRPM = 0;
}
else
{
_aveRPM = (_aveRPM + value) / 2;
}
OnPropertyChanged("AveRPM");
}
}
private int _minRPM = Int32.MaxValue;
public int MinRPM
{
get
{
return _minRPM;
}
set
{
if (Clear)
{
_minRPM = Int32.MaxValue;
}
else
{
if (value < _minRPM)
{
_minRPM = value;
}
}
OnPropertyChanged("MinRPM");
}
}
private int _maxRPM;
public int MaxRPM
{
get
{
return _maxRPM;
}
set
{
if (Clear)
{
_maxRPM = 0;
}
else
{
if (value > _maxRPM)
{
_maxRPM = value;
}
}
OnPropertyChanged("MaxRPM");
}
}
As you can see I have a "Clear" Property that when set to true will call an update all properties with OnPropertyChanged(String.Empty) with each property resting to a known value when "Clear" is true.
How do I set Clear back to false when all PropertyChanged events are done?
As said Niclas, I would said: do not do it like this! The purpose of the PropertyChanged in your ViewModel is to update the UI. If you had some sort of logic depending of your propertychanged, it will be a nightmare to debug!
Keep your property as clean as possible. Now to update some dependent value it would be better to do something like this:
private bool _clear;
public bool Clear
{
get
{
return _clear;
}
set
{
_clear = value;
OnPropertyChanged("Clear");
if (_clear)
ClearValues();
}
}
public void ClearValues()
{
AveRPM=0;
MinRPM=0;
MaxRPM=0;
...
Clear=False;
}
Not perfect but seems much more readable and maintainable to me.
Hope it helps.

Remove duplicates from listbox/combobox in WPF

I've seen answers mentioning something compact like this:here
List<T> withDupes = LoadSomeData();
List<T> noDupes = withDupes.Distinct().ToList();
So I tried the following (syntax)
List<InfoControl> withDupes = (List<InfoControl>)listBox1.ItemsSource;
listBox1.ItemsSource = withDupes.Distinct().ToList();
but withDupes is null ? Perhaps I am retrieving the wrong data list. I added InfoControls one at a time.
Is there something else I should be implementing in InfoControl class? (Equal,hashCode)?
Thanks
Addendum 1: [ignore I should not be translating from Java :) ]
Also have (translated from a Java example, not sure it's 100% correct) declared in the InfoControl class..
public Boolean Equals(Object obj)
{ if (obj == this) { return true; }
if (!(obj is InfoControl)) { return false; }
InfoControl other = (InfoControl)obj;
return this.URL.Equals(other.URL); }
public int hashCode()
{ return this.URLFld.Content.GetHashCode(); }
Addendum 2:
When I try to use override based on the msdn link custom type example it says it is sealed :)
It does not seem distinct is stepping thru GetHashCode() and I am still getting the same listbox.items.count after distinct.
bool IEquatable<InfoControl>.Equals(InfoControl other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return URL.Equals(other.URL);
}
public int GetHashCode(InfoControl obj)
{
return obj.URL.GetHashCode();
}
Addendum 3:
When I try override VS2010 says it is sealed? "cannot override inherited member 'System.Windows.DependencyObject.GetHashCode()' because it is sealed" what am I doing wrong?
public override int GetHashCode()
{
return URL.GetHashCode();
}
public string URL
{
get { return this.URLFld.Content.ToString() ; }
set
{
this.URLFld.Content = value;
}
}
.
Addendum 4:
public partial class InfoControl : UserControl
, IEquatable<YouTubeInfoControl>
{
private string URL_;
public string URL
{
get { return URL_; }
set
{
URL_ = value;
}
}
bool IEquatable<YouTubeInfoControl>.Equals(YouTubeInfoControl other)
{
if (Object.ReferenceEquals(other, null)) return false;
if (Object.ReferenceEquals(this, other)) return true;
return URL == other.URL;
}
public override int GetHashCode()
{
return URL.GetHashCode();
}
}
A ListBox's items can either be set via ListBox.Items or ListBox.ItemsSource, if you add items using listBox1.Items.Add this does not affect the ItemsSource which will stay null. In this case you should get your initial list from listBox1.Items.
If you're adding the InfoControl objects one at a time, the ItemSource of the listBox will remain set to NULL. You're better off binding a List to the listbox which will allow you to get the data back off of the ItemSource property later

Is there a nullable datepicker that I can bind to?

I am looking for a datepicker like what microsoft provides, but it doesn't support null values, and since this is tied to a nullable field on a database that isn't acceptable.
I found this one, but according to the comments at the bottom of the page it has issues with binding to a database. I also have one in my project that I inherited, but it has similar issues (sometimes it shows values, sometimes it doesn't). Does anyone know of one that works?
Use a date picker to populate a textbox and if they want the field to be null, just erase the contents of the textbox (and then handle the blank input accordingly).
This also provides the added benefit of allowing the user to type in their date if they so choose.
Smart FieldPackEditor has a datepicker that is nullable. I believe it does everything that you need. I wish this was around when I was dealing with this sort of stuff. I still remember all the workarounds I had to implement with Microsoft's datepicker control. Uggh!
http://www.visualhint.com/index.php/fieldpackeditor/
why not use a client side datepicker to populate a text field. If the textfield is empty, then you have a null date, otherwise convert the value.
jQuery has a nice easy to use datepicker. http://jqueryui.com
This one seems to work, one of my co-workers had it:
using System;
using System.Windows.Forms;
namespace CustomControls
{
public class NullableBindableDateTimePicker : System.Windows.Forms.DateTimePicker
{
private Boolean isNull = false;
private DateTimePickerFormat baseFormat = DateTimePickerFormat.Short;
private Boolean ignoreBindOnFormat = false;
public NullableBindableDateTimePicker()
{
this.Format = baseFormat;
if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
}
public Boolean IsNull
{
get { return isNull; }
set
{
isNull = value;
this.Checked = value;
}
}
//TODO: Add BaseCustomFormat
public DateTimePickerFormat BaseFormat
{
get { return baseFormat; }
set { baseFormat = value; }
}
public object BindMe
{
get
{
if (IsNull) return System.DBNull.Value;
else return base.Value;
}
set
{
//String s = this.Name;
if (ignoreBindOnFormat) return;
if (System.Convert.IsDBNull(value))
{
// for some reason setting base.format in this.format calls set BindMe.
// we need to ignore the following call
ignoreBindOnFormat = true;
this.Format = DateTimePickerFormat.Custom;
ignoreBindOnFormat = false;
this.CustomFormat = " ";
IsNull = true;
}
else
{
ignoreBindOnFormat = true;
this.Format = baseFormat;
ignoreBindOnFormat = false;
if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
IsNull = false;
base.Value = (DateTime)value;
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.BindMe = DBNull.Value;
}
}
protected override void OnCloseUp(EventArgs eventargs)
{
base.OnCloseUp(eventargs);
BindMe = base.Value;
}
}
}

Resources