GTK Sharp Refresh / Redraw widget - combobox

I'm trying to develop a really simple GUI with mono and GTK sharp. I have a few two combobox and I need to change the options available in the secod based on the selected item from the first one:
//Create window container
var mainContainer = new Table (3, 1, false);
//add form
var formContainer = new Table (1, 3, true);
//FIRST COMBOBOX
pdfSelect = new ComboBox(new string[] {"Monthly", "Monthly Offline", "Quarterly" }) { Active = 2 };
pdfSelect.Changed += new EventHandler(OnPdfSelectChanged);
formContainer.Attach (pdfSelect, 1, 2, 0, 1);
//MONTH COMBOBOX
monthSelect = new ComboBox(new string[] { "January","February","March","April","May","June","July","August","September","October","November","December" }){ Active = 0 };
formContainer.Attach(monthSelect, 2, 3, 0, 1);
mainContainer.Attach(formContainer, 0, 1, 0, 1);
win.Add(mainContainer);
My change event is below:
public static void OnPdfSelectChanged(object o, EventArgs args) {
ComboBox combo = o as ComboBox;
if (o == null) return;
if (combo.ActiveText.Equals("Monthly") || combo.ActiveText.Equals("Monthly Offline"))
{
monthSelect = new ComboBox(new string[] { "January","February","March","April","May","June","July",
"August","September","October","November","December" }) { Active = 0 };
}
else
{
monthSelect = new ComboBox(new string[] { "January", "April", "July", "October" }) { Active = 0 };
}
//TODO REFRESH WINDOW
}
The code works but the second combobox seems to dont be updated. I guess I need to refresh the window?
Thanks

The problem is that you're assigning a new Gtk.ComboBox to the monthSelect variable but that doesn't automatically remove the old combobox from the window and add this new one. What you want is to clear the contents of the combobox and add the new content. Something like:
monthSelect.Clear();
monthSelect.AppendText("January");
monthSelect.AppendText("April");
monthSelect.AppendText("July");
monthSelect.AppendText("October");

I got it to work the two mean things is to call Clear() as fog said (+1) but after that you need to assign a cell rendertext or the new items will not be displayed:
Gtk.ComboBox.Clear Method "Unsets all the mappings on all renderers for this combo box."
public static void OnPdfSelectChanged(object o, EventArgs args) {
ComboBox combo = o as ComboBox;
if (o == null) return;
//Set entries
var entries = new string[] { "January","February","March","April","May","June","July", "August","September","October","November","December" };
if (combo.ActiveText.Equals("Quarterly"))
{
entries = new string[] { "January", "April", "July", "October" };
}
//clear previous data
monthSelect.Clear();
//cells data
ListStore store = new ListStore(typeof(string));
foreach (var item in entries) {
store.AppendValues(item);
}
//assign data to combobox
monthSelect.Model = store;
//renderer for cells
var cellRenderer = new CellRendererText();
monthSelect.PackStart(cellRenderer, true);
monthSelect.AddAttribute(cellRenderer, "text", 0);
//set first item as active
monthSelect.Active = 0;
}

Maybe too late, but...
((ListStore)combobox.Model).Clear();
combobox.AppendText("item");

Related

ImageComboBoxEdit selected value not settable

I'm trying to add an ImageComboBoxEdit control onto a UserControl within my WinForms application.
public ShortCutUserControl()
{
var imageCollection = new ImageCollection { ImageSize = new Size(48, 48) };
imageCollection.Images.Add(Image.FromFile(#"Keyboard\ctrl.ico"));
imageCollection.Images.Add(Image.FromFile(#"Keyboard\alt.ico"));
functionKeyImageComboBoxEdit.Properties.LargeImages = imageCollection;
ImageComboBoxItem ctrlItem = new ImageComboBoxItem
{
Description = "Ctrl",
ImageIndex = 0
};
ImageComboBoxItem altItem = new ImageComboBoxItem
{
Description = "Alt",
ImageIndex = 1
};
functionKeyImageComboBoxEdit.Properties.Items.Add(altItem);
functionKeyImageComboBoxEdit.Properties.Items.Add(ctrlItem);
}
When the control is loaded:
I can't change the currently either directly through code or in the UI.
functionKeyImageComboBoxEdit.SelectedIndex = 0;
I've tried attaching events to the functionKeyImageComboBoxEdit, but none of these seem to be fired/captured;
functionKeyImageComboBoxEdit.SelectedIndexChanged += FunctionKeyImageComboBoxEditOnSelectedIndexChanged;
private void FunctionKeyImageComboBoxEditOnSelectedIndexChanged(object sender, EventArgs eventArgs)
{
//throw new NotImplementedException();
}
What am I missing from my code? I've been looking at the DevExpress ImageComboBoxEdit Documentation but can't see any problem.
The cause of the issue is that you don't set values for ImageComboBoxItems. Do this like:
ImageComboBoxItem ctrlItem = new ImageComboBoxItem
{
Description = "Ctrl",
ImageIndex = 0,
Value = "Ctrl"
};
ImageComboBoxItem altItem = new ImageComboBoxItem
{
Description = "Alt",
ImageIndex = 1,
Value = "Alt"
};

Xamarin.Forms - Detect orientation & adjust page

I'd like to know the orientation of the device (Android, iOS & Windows Phone) at the time I'm building up my page. The page is having a grid with 3 columndefinitions and should have 5 columndefinitions as soon as the orientation got changed to landscape.
Grid grid = new Grid
{
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Fill,
RowSpacing = 15,
ColumnSpacing = 15,
Padding = new Thickness(15),
ColumnDefinitions =
{
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) },
new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }
}
};
for (int i = 0; i < 12; i++)
{
Image img = new Image()
{
Source = "ButtonBlue.png"
};
//if(DependencyService.Get<IDeviceInfo>().IsPortraitOriented())
//{
grid.Children.Add(img, i % 3, i / 3);
//}
//else
//{
// grid.Children.Add(button, i % 5, i / 5);
//}
}
this.Content = new ScrollView
{
Orientation = ScrollOrientation.Vertical,
Content = grid
};
So here I added 12 images to test my code. The page is looking good in portrait-orientation and is having a lot of space between columns if the device is in landscape-orientation.
I'm also trying to use dependency injection to retrieve the information. The DependencyService is doing his job, but I don't have any success retrieving the orientation of the device...
In xamarin.forms, you can get notification from android part by using MessageCenter.
1.In Shared Project
public partial class MyPage : ContentPage
{
public MyPage ()
{
InitializeComponent ();
Stack = new StackLayout
{
Orientation = StackOrientation.Vertical,
};
Stack.Children.Add (new Button { Text = "one" });
Stack.Children.Add (new Button { Text = "two" });
Stack.Children.Add (new Button { Text = "three" });
Content = Stack;
MessagingCenter.Subscribe<MyPage> (this, "Vertical", (sender) =>
{
this.Stack.Orientation = StackOrientation.Vertical;
this.ForceLayout();
});
MessagingCenter.Subscribe<MyPage> (this, "Horizontal", (sender) =>
{
this.Stack.Orientation = StackOrientation.Horizontal;
this.ForceLayout();
});
}
public StackLayout Stack;
}
2.In Android Project
[Activity (Label = "XamFormOrientation.Android.Android", MainLauncher = true, ConfigurationChanges = global::Android.Content.PM.ConfigChanges.Orientation | global::Android.Content.PM.ConfigChanges.ScreenSize)]
public class MainActivity : AndroidActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
Xamarin.Forms.Forms.Init (this, bundle);
SetPage (App.GetMainPage ());
}
public override void OnConfigurationChanged (global::Android.Content.Res.Configuration newConfig)
{
base.OnConfigurationChanged (newConfig);
if (newConfig.Orientation == global::Android.Content.Res.Orientation.Portrait) {
MessagingCenter.Send<MyPage> (null, "Vertical");
} else if (newConfig.Orientation == global::Android.Content.Res.Orientation.Landscape) {
MessagingCenter.Send<MyPage> (null, "Horizontal");
}
}
}
I solved a similar problem and find it on great post which maybe helpfull for you (see hyperlink below).
In shortcut : Find out orientation by
Page.Width < Page.Height
and use this information in constructor of ContentPage (or other) when creating page
http://www.sellsbrothers.com/Posts/Details/13740

datagridview control continues update, gui flickers when scrolling window

Problem:
I have a Winform application with a form en on this form i have a databound DataGridView.
The datagridview is updated from the backend by updating the bind object continuesly using a timer to get the data every 10 seconds. In order to update the gui with this new data i call a RefreshDatabindings. (if i do not do this, the gui is nog updated, i am binding to a BindingList and the object implement the INotifyPropertyChanged)
When the form is big enough to show the whole datagridview at once everything is working wel. But when the form is not big enough to show the hole datagridview a scrollbar appears.
When i scroll to the right to see the rest of the datagridview i see the gui flickering (only the part that wasn't visible before scrolling). When i strech the form to make de gridview fitting again, everything is working wel (no flashing and flickering). the flickering only happens when i have to scroll.
I am lost, can please somebody help me :)?
I allready tryed the DoubleBuffered = true.
Thanks in advance!
BindingList<InstanceTableViewModel> viewModelList;
public Form1()
{
InitializeComponent();
DoubleBuffered = true;
functionParamList = new List<FunctionParameter>();
functionParamList.Add(new FunctionParameter { DeviceValue = 100, InstanceId = "1", Name = "A" });
functionParamList.Add(new FunctionParameter { DeviceValue = 200, InstanceId = "2", Name = "B" });
functionParamList.Add(new FunctionParameter { DeviceValue = 300, InstanceId = "3", Name = "C" });
viewModelList = CreateInstanceTableViewModelList();
dataGridView1.DataSource = viewModelList;
//Create timer
updateDataTimer = new System.Timers.Timer();
updateDataTimer.Interval = 500;
updateDataTimer.Elapsed += updateDataTimer_Elapsed;
updateDataTimer.Start();
}
private void updateDataTimer_Elapsed(object sender, ElapsedEventArgs e)
{
ThreadPool.QueueUserWorkItem(ReadDataThreadPoolMethod);
}
private void ReadDataThreadPoolMethod(object state)
{
Random random = new Random();
int randomNumber = random.Next(0, 100);
foreach (FunctionParameter param in functionParamList)
{
param.DeviceValue = Convert.ToInt64(randomNumber);
}
}
void functionParameter_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
var functionParameter = sender as FunctionParameter;
var propertyName = e.PropertyName;
var propertyValue = functionParameter.DeviceValue;
var parameterName = functionParameter.Name;
UpdateViewModel(functionParameter.InstanceId, propertyName, propertyValue, parameterName);
}
private void UpdateViewModel(string instanceId, string propertyName, long propertyValue, string parameterName)
{
var instanceViewModel = viewModelList.Single(x => x.InstanceId == instanceId && x.NameLabel == parameterName);
if (instanceViewModel != null)
{
instanceViewModel.ValueHex = Convert.ToUInt16(propertyValue);
}
ResetBindingsSource();
}
delegate void UpdateBindingsInvoker();
public void ResetBindingsSource()
{
if (!this.IsDisposed)
{
if (this.InvokeRequired)
{
this.Invoke(new UpdateBindingsInvoker(UpdateDataGrid));
}
else
{
UpdateDataGrid();
}
}
}
private void UpdateDataGrid()
{
dataGridView1.Refresh();
}
So here my solution:
You only uses the Forms DoubleBuffering, but the following code is an extension method to the DataGridview and successfully works (at my tests ;)
public static void DoubleBuffered(this DataGridView dgv, bool setting)
{
Type dgvType = dgv.GetType();
PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
BindingFlags.Instance | BindingFlags.NonPublic);
pi.SetValue(dgv, setting, null);
}
I found this code right here at Codeprojct.
You can use it in this way:
YourDataGridView.DoubleBuffered(true);
I hope i could help you ^^

WPF Performance Issues

I have a WPF Application which allows user to enter production Data.
For that reason i created a Usercontrol which uses an WPF Toolkit Accordion. In Code behind i create 15 Accordion Items. Each Item has an Stackpanel and 5-10 Textboxes in it.
When adding 12 of these controls to the main Content Control it takes about 10 seconds.
What can be the cause of this behaviour?
public XXXMeasurementControl(Measurement meas)
{
InitializeComponent();
if (meas.ID == -2)
{
LineNameTextBlock.Text = "Total";
}
else
{
LineNameTextBlock.Text = meas.MeasureDate.ToString("HH:mm") + " - " + meas.MeasureDate.AddHours(1).ToString("HH:mm");
}
this.cells = meas.MainCells;
this.meas = meas;
Binding b = new Binding();
Remark.DataContext = Meas;
b.Mode = BindingMode.TwoWay;
b.Path = new PropertyPath("Remark");
BindingOperations.SetBinding(Remark, TextBox.TextProperty, b);
//Create Cells Start
foreach (Cell c in cells)
{
//Creating Textboxes & Bindings for Stations from Maincells
if (c.Name != "OQC")
{
//Setting Qualified Overall (=Qualified from Cell Appearance Check)
Common.BindTextBlock(QualifiedOverallTextBlock, c, "Qualified");
if (c.Name.Contains("Appearance Check"))
Common.BindTextBlock(QualifiedOverallTextBlock, c, "Qualified");
//Setting Scrap Rate (=Waste from Cell Acoustic Test)
if (c.Name.Contains("Acoustic Test"))
Common.BindTextBlock(ScrapRateTextBlock, c, "WasteRate");
AccordionItem aci = new AccordionItem();
StackPanel sp = new StackPanel();
StackPanel groupData = new StackPanel();
StackPanel all = new StackPanel();
all.Children.Add(sp);
all.Children.Add(groupData);
if (c.Stations != null)
//All Single Cell Line Controls
if (meas.ID != -2)
{
for (int i = 0; i < c.Stations.Count; i++)
{
NumberTextbox t = Common.CreateNumberTextbox(c.Stations[i], "Value", BindingMode.TwoWay, false, null, 80, 22);
t.LostFocus += new RoutedEventHandler(t_LostFocus);
c.Stations[i].PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(LineControl_PropertyChanged);
//Handling if Qualified Field is Editable
if (c.Stations[i].Name.Contains("Qualified"))
{
t.Background = new SolidColorBrush(Colors.BlanchedAlmond);
groupData.Children.Add(t);
}
else
{
sp.Children.Add(t);
}
}
}
groupData.Children.Add(Common.CreateNumberTextbox(c, "RejectQty", BindingMode.OneWay, true,null, 80, 22));
groupData.Children.Add(Common.CreateNumberTextbox(c, "PassRate", BindingMode.OneWay, true, new SolidColorBrush(Colors.BlanchedAlmond), 80, 22));
groupData.Children.Add(Common.CreateNumberTextbox(c, "RejectRate", BindingMode.OneWay, true, new SolidColorBrush(Colors.BlanchedAlmond), 80, 22));
aci.Header = "";
aci.Content = all;
MainCellsAccordion.Items.Add(aci);
}
}
}
I too experience terrible performance with the Accordion control in the WPF Toolkit. I have an Accordion control within a tab, and whenever I switch to that tab it takes a solid 2-3 seconds to initialize the contents. I do not have this problem when the Accordion Control is not being used.
I think the Accordion is your culprit.

Using a Storyboard animation on a programmatically-added control

I'm trying to fade in a new control to my application's "app" area which is programmatically added after the existing controls are removed. My code looks like this:
void settingsButton_Clicked(object sender, EventArgs e)
{
ContentCanvas.Children.Clear();
// Fade in settings panel
NameScope.SetNameScope(this, new NameScope());
SettingsPane s = new SettingsPane();
s.Name = "settingsPane";
this.RegisterName(s.Name, s);
this.Resources.Add(s.Name, s);
Storyboard sb = new Storyboard();
DoubleAnimation settingsFade = new DoubleAnimation();
settingsFade.From = 0;
settingsFade.To = 1;
settingsFade.Duration = new Duration(TimeSpan.FromSeconds(0.33));
settingsFade.RepeatBehavior = new RepeatBehavior(1);
Storyboard.SetTargetName(settingsFade, s.Name);
Storyboard.SetTargetProperty(settingsFade, new PropertyPath(UserControl.OpacityProperty));
ContentCanvas.Children.Add(s);
sb.Children.Add(settingsFade);
sb.Begin();
}
However, when I run this code, I get the error "No applicable name scope exists to resolve the name 'settingsPane'."
What am I possibly doing wrong? I'm pretty sure I've registered everything properly :(
I wouldn't hassle with the NameScopes etc. and would rather use Storyboard.SetTarget instead.
var b = new Button() { Content = "abcd" };
stack.Children.Add(b);
var fade = new DoubleAnimation()
{
From = 0,
To = 1,
Duration = TimeSpan.FromSeconds(5),
};
Storyboard.SetTarget(fade, b);
Storyboard.SetTargetProperty(fade, new PropertyPath(Button.OpacityProperty));
var sb = new Storyboard();
sb.Children.Add(fade);
sb.Begin();
I solved the problem using this as parameter in the begin method, try:
sb.Begin(this);
Because the name is registered in the window.
I agree, the namescopes are probably the wrong thing to use for this scenario. Much simpler and easier to use SetTarget rather than SetTargetName.
In case it helps anyone else, here's what I used to highlight a particular cell in a table with a highlight that decays to nothing. It's a little like the StackOverflow highlight when you add a new answer.
TableCell cell = table.RowGroups[0].Rows[row].Cells[col];
// The cell contains just one paragraph; it is the first block
Paragraph p = (Paragraph)cell.Blocks.FirstBlock;
// Animate the paragraph: fade the background from Yellow to White,
// once, through a span of 6 seconds.
SolidColorBrush brush = new SolidColorBrush(Colors.Yellow);
p.Background = brush;
ColorAnimation ca1 = new ColorAnimation()
{
From = Colors.Yellow,
To = Colors.White,
Duration = new Duration(TimeSpan.FromSeconds(6.0)),
RepeatBehavior = new RepeatBehavior(1),
AutoReverse = false,
};
brush.BeginAnimation(SolidColorBrush.ColorProperty, ca1);
It is possible odd thing but my solution is to use both methods:
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTarget(DA, myRect);
sb.Begin(this);
In this case there is no error.
Have a look at the code where I have used it.
int n = 0;
bool isWorking;
Storyboard sb;
string myObjectName;
UIElement myElement;
int idx = 0;
void timer_Tick(object sender, EventArgs e)
{
if (isWorking == false)
{
isWorking = true;
try
{
myElement = stackObj.Children[idx];
var possibleIDX = idx + 1;
if (possibleIDX == stackObj.Children.Count)
idx = 0;
else
idx++;
var myRect = (Rectangle)myElement;
// Debug.WriteLine("TICK: " + myRect.Name);
var dur = TimeSpan.FromMilliseconds(2000);
var f = CreateVisibility(dur, myElement, false);
sb.Children.Add(f);
Duration d = TimeSpan.FromSeconds(2);
DoubleAnimation DA = new DoubleAnimation() { From = 1, To = 0, Duration = d };
sb.Children.Add(DA);
myObjectName = myRect.Name;
Storyboard.SetTargetName(DA, myObjectName);
Storyboard.SetTarget(DA, myRect);
Storyboard.SetTargetProperty(DA, new PropertyPath("Opacity"));
sb.Begin(this);
n++;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message + " " + DateTime.Now.TimeOfDay);
}
isWorking = false;
}
}

Resources