Posts

Showing posts with the label Winforms

AutoEllipsis=true Affects The Vertical Positioning Of The Text

Answer : I see it. This looks like a limitation in the underlying winapi, DrawTextEx(). Which doesn't get a lot of help from the Label class, it doesn't turn on the DT_SINGLELINE option (aka TextFormatFlags.SingleLine) since it is capable of rendering multiple lines. DrawTextEx() documents that this is required to get vertically centered text (DT_VCENTER). So the real bug is that it shouldn't be centered at all :) Do note that you do get centered text when you grow the label vertically. The simplest way to work around it is by setting the label's UseCompatibleTextRendering property to True.

Alternative To ComponentOne 3D Surface Map Chart

Image
Answer : Here are some commercial providers that have the type of chart you're looking for: ChartFX for .NET - Gallery of available charts including 3D surface Nevron Chart for .NET - Gallery of available surface charts You may have already come across this, but I thought I'd point out the CodeProject article Plot 3D surfaces . I'm not sure if it's feature set is sufficient for your requirements, but it's worth investigating if you haven't already. The obvious advantage is that it's free and open source (so that you might expand it for your particular needs). Example screenshot: Another possible alternative that looks reasonably complete is the F# for Visualization library. This is commercial, but by the look of it would do more that what you might need. Don't let the fact that it is designed for F# put you off - you should still to use it directly from C# (or perhaps with a minor amount of interop to make things cleaner). Certainly, the...

Click Event For WPF Image

Answer : Just add a MouseDown (or MouseLeftButtonDown as suggested) event to your image like so <Image x:Name=aPicture Source="mypic.jpg" MouseDown="aPicture_MouseDown"/> // or <Image x:Name=aPicture Source="mypic.jpg" MouseLeftButtonDown="aPicture_MouseDown"/> which should add this to your code behind private void aPicture_MouseDown(object sender, MouseEventArgs e) { //do something here } In WPF each control has its default template (how it looks) but you can easily change these templates and make controls look like you want. This makes it easier to pick control by its functionality and make it look like you want. In your case you want Click so you choose Button and change its Template <Window ...> <Window.Resources> <Style TargetType="{x:Type Button}" x:Key="ImageButtonStyle"> <Setter Property="Template"> <Setter.Value...

Change Border Color In TextBox C#

Image
Answer : To change border color of TextBox you can override WndProc method and handle WM_NCPAINT message. Then get the window device context of the control using GetWindowDC because we want to draw to non-client area of control. Then to draw, it's enough to create a Graphics object from that context, then draw border for control. To redraw the control when the BorderColor property changes, you can use RedrawWindow method. Code Here is a TextBox which has a BorderColor property. The control uses BorderColor if the property values is different than Color.Transparent and BorderStyle is its default value Fixed3d . using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; public class MyTextBox : TextBox { const int WM_NCPAINT = 0x85; const uint RDW_INVALIDATE = 0x1; const uint RDW_IUPDATENOW = 0x100; const uint RDW_FRAME = 0x400; [DllImport("user32.dll")] static extern IntPtr GetWi...

Add Controls Dynamically In Flowlayoutpanel

Answer : For a FlowLayoutPanel, you don't need to specify a .Location since the controls are arranged for you: Represents a panel that dynamically lays out its contents horizontally or vertically. ... The FlowLayoutPanel control arranges its contents in a horizontal or vertical flow direction. Its contents can be wrapped from one row to the next, or from one column to the next. Just change " flowLayoutPanel1 " to the name of your FlowLayoutPanel : for (int i = 0; i < 5; i++) { Button button = new Button(); button.Tag = i; flowLayoutPanel1.Controls.Add(button); }

Automating The InvokeRequired Code Pattern

Answer : Lee's approach can be simplified further public static void InvokeIfRequired(this Control control, MethodInvoker action) { // See Update 2 for edits Mike de Klerk suggests to insert here. if (control.InvokeRequired) { control.Invoke(action); } else { action(); } } And can be called like this richEditControl1.InvokeIfRequired(() => { // Do anything you want with the control here richEditControl1.RtfText = value; RtfHelpers.AddMissingStyles(richEditControl1); }); There is no need to pass the control as parameter to the delegate. C# automatically creates a closure. UPDATE : According to several other posters Control can be generalized as ISynchronizeInvoke : public static void InvokeIfRequired(this ISynchronizeInvoke obj, MethodInvoker action) { if (obj.InvokeRequired) { var args = new object[0]; obj.Invoke(action, args); } else { action...

C# DataGridView Not Updated When Datasource Is Changed

Answer : Quick and dirty solution : dataGridView.DataSource = null; dataGridView.DataSource = phase3Results; Clean and correct solution : Use a BindingList<T> instead of List<T> as your DataSource. List<T> does not fire events when its collection changes. Also, if you additionally implement INotifyPropertyChanged for T , BindingList<T> automatically subscribes to property changes for each T in the collection and lets the view know about the change. Try using a BindingList<> instead of List<> and (as already suggested by Daniel), implement INotifyPropertyChanged. However, I think you can also call .Refesh() if you didn't want to implement the INotifyPropertyChanged interface. Here's an example ripped from here public class Car : INotifyPropertyChanged { private string _make; private string _model; private int _year; public event PropertyChangedEventHandler PropertyChanged; public Car(string make, string m...