|
We could be heading for infinite recursion here
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi,
As an alternative, you could create an extension method to perform the check;
internal static class UtilityMethods
{
internal static bool IsEmpty(this object o)
{
return o == null;
}
} This would result in code that looks like this;
Object objVariable;
if(!objVariable.IsEmpty())
{
}
I are Troll
|
|
|
|
|
How to populate row number for datagridview in row header column with out any looping statements?
it will be very usful for me if you help.
Elango.N
modified on Tuesday, June 15, 2010 5:51 AM
|
|
|
|
|
You can implement your custom datagridview by overriding it and then use it's OnRowPostPaint method
to draw row number.
That's it. No whenever you use this grid it will automatically add row number to the datagridview.
This is the simplest solution I have to apply row number in datagridview without any kind of looping.
class MyDGV : DataGridView
{
public MyDGV()
{
}
protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
{
string strRowNumber = (e.RowIndex + 1).ToString();
while (strRowNumber.Length < this.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;
SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);
if (this.RowHeadersWidth < (int)(size.Width + 20)) this.RowHeadersWidth = (int)(size.Width + 20);
Brush b = SystemBrushes.ControlText;
e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
base.OnRowPostPaint(e);
}
}
HTH
Jinal Desai - LIVE
Experience is mother of sage....
|
|
|
|
|
I have a project for antiquity which need saving pictures and maps for specific impression.I search alot but articles often speak about saving path of picture in sgl.can anybody help me?
|
|
|
|
|
See here - may this will help you get started.
My signature "sucks" today
|
|
|
|
|
As everyone here is tired of hearing, I'm working on a simple User Control that's misbehaving. If you help me solve this, I promise to contribute an article about it.
The control displays various user-selectable shapes for water channels, then asks for the dimensions of the conduit. I have an event handler working nicely to notify the hosting Form when a value changes, but it's too responsive; too many events fire it. Specifically, when a user enters values into the textboxes for the dimensions of the channel, if for some ungodly reason the user decides to select another shape, the event fires for a change of value in the previous (unchanged) textbox. I added some code tonight to check to see that the value actually changed before the focus moves to another control, but it doesn't seem to be working, and I don't see why. The relevant code is as follows:
private void rbTrap_CheckedChanged(object sender, EventArgs e)
{
if (rbTrap.Checked == true)
{
Shape = myShape.Trap;
MyArgs.MyControl = "rbTrap";
RaiseEvent(rbTrap, MyArgs);
lblDim1.Text = "Depth, d";
lblDim2.Text = "Bottom Width, W1";
lblDim3.Text = "Top Width, W2";
lblDim3.Visible = true;
txtDim3.Visible = true;
Invalidate();
}
}
private double ValidateEntry(TextBox MyTextBox)
{
try
{
return Double.Parse(MyTextBox.Text);
}
catch (FormatException ex)
{
MessageBox.Show("Enter a valid numeric value" + ex.Message);
MyTextBox.Focus();
return 0.0;
}
}
private void txtDim1_Enter(object sender, EventArgs e)
{
txtDim1.SelectAll();
}
private void txtDim1_Leave(object sender, EventArgs e)
{
prev = dim1;
dim1 = ValidateEntry(txtDim1);
if (dim1 != 0.0 & prev != dim1)
{
MyArgs.MyControl = "txtDim1";
RaiseEvent(txtDim1, MyArgs);
}
}
private void txtDim1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
txtDim2.Focus();
}
}
In my understanding, if the value of dim1 is unchanged (dim1 = prev), the if test should fail, and RaiseEvent(...) should not execute. Maybe I've been staring at this too many hours, but I can't see where the error lies. For what it's worth, the event handling code is:
public event EventHandler ValueChanged;
public class MyEventArgs : EventArgs
{
private string myControl;
public string MyControl
{
get { return myControl; }
set { myControl = value; }
}
}
protected virtual void OnValueChanged(object sender, MyEventArgs e)
{
EventHandler eh = ValueChanged;
if (eh != null)
eh(this, e);
}
public void RaiseEvent(object sender, MyEventArgs e)
{
OnValueChanged(sender, e);
}
I adapted this from an article posted by DaveyM69 - an excellent article, by the way - and I admit that my understanding of event handling is shaky at best. But it does work nicely, albeit in weird ways.
Any assistance would be welcome...
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hi Roger.
I'm having trouble recreating your problem. I shoved two text boxes on a form and used the following code:-
double prev;
double dim1;
private void txtDim1_Leave(object sender, EventArgs e)
{
prev = dim1;
dim1 = ValidateEntry(txtDim1);
if (dim1 != 0.0 & prev != dim1)
{
MessageBox.Show("Event Fired");
}
}
private double ValidateEntry(TextBox MyTextBox)
{
try
{
return Double.Parse(MyTextBox.Text);
}
catch (FormatException ex)
{
MessageBox.Show("Enter a valid numeric value" + ex.Message);
MyTextBox.Focus(); return 0.0;
}
}
private void txtDim1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) { txtDim2.Focus(); }
}
and it worked as expected. Is there anywhere else in your code that you could be changing the value of txtDim1.
The FoZ
|
|
|
|
|
I think Dave may be on to something; it has to do with the radio button somehow. The extra event does not occur if I use the mouse to move to another text box, only when I move to a radio button.
The messages displayed also show that the text did not change, but it does happen that the text changes from Selected to unselected when the focus is moved to the radio button. I wonder if that's being picked up as a text change and firing the event?
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Hi Roger,
Thanks for the kind words
My only initial thought is to set a breakpoint inside if (rbTrap.Checked == true) . I have had wierd behaviour before with CheckBox es/RadioButton s where CheckedChanged has raised more often than expected.
If that doesn't help isolate the cause then feel free to drop me an email through the link at the bottom of this post and I'll happily take a look at the project for you and root the b**ger out!
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hello, I am working on my final project at the university. I need a .net TrackBar or a similar control to set an upper-limit, a lower-limit and a setting value.
Could somebody help me?
Thank you so much.
Nicolás.
|
|
|
|
|
|
Thanks for your answer Calla, I all ready saw that page. But that control only have one slider. Do you know if there is a similar control with 3 sliders?
Tranks again.
Nicolás.
|
|
|
|
|
There aren't any in-built ones. Search for .NET Range controls and you may find something that suits.
If not, it's fairly easy to code your own if it's only for one type of value such as an int.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hello Dave.
Yes, it is for one type of values. Do you have some example or a reference?
Nicolás.
|
|
|
|
|
I don't unfortunately. I did code one myself a while ago but never used it in the end - I'll have a dig through my backups at home and see if I can find it but it may take me a day or two to locate it.
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Perhaps you could modify this code[^] and add a value property?
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi, I've had a look through my files and I never completed it as it was no longer required. You are welcome to use what I did and finish it off if you'd like. The main things that need doing are marked // ToDo: - unfortunately that includes the painting and mouse handling - but the Range struct is pretty much complete and the main code for the control is in place.
using System;
namespace DaveyM69
{
[Serializable]
public struct Range : IEquatable<Range>
{
public static readonly Range Empty = new Range();
public static readonly Range MaxRange = new Range(int.MinValue, int.MaxValue);
public static readonly Range Percentage = new Range(0, 100);
private int lower;
private int upper;
public Range(int lower, int upper)
{
if (lower > upper)
{
int newUpper = lower;
lower = upper;
upper = newUpper;
}
this.lower = lower;
this.upper = upper;
}
public static bool operator ==(Range rangeA, Range rangeB)
{
return (rangeA.lower == rangeB.lower) && (rangeA.upper == rangeB.upper);
}
public static bool operator !=(Range rangeA, Range rangeB)
{
return !(rangeA == rangeB);
}
public int Lower
{
get { return lower; }
}
public int Upper
{
get { return upper; }
}
public Range Adjust(int offset)
{
return new Range(lower + offset, upper + offset);
}
public int Contain(int value)
{
return Contain(value, false);
}
public int Contain(int value, bool excludeExtremities)
{
Range testRange = excludeExtremities ? this.Expand(-1) : this;
if (value < testRange.lower)
return testRange.lower;
if (value > testRange.upper)
return testRange.upper;
return value;
}
public Range Contain(Range range)
{
return Contain(range, false);
}
public Range Contain(Range range, bool excludeExtremities)
{
if (Contains(range, excludeExtremities))
return range;
int newLower = range.lower;
int newUpper = range.upper;
if (!Contains(range.lower, excludeExtremities))
{
newLower = lower;
if (excludeExtremities)
newLower++;
}
if (!Contains(range.upper, excludeExtremities))
{
newUpper = upper;
if (excludeExtremities)
newUpper--;
}
Range result = new Range(newLower, newUpper);
if (!Contains(result, excludeExtremities))
throw new InvalidOperationException("The span of this range is too small to contain another range");
return result;
}
public bool Contains(Range range)
{
return Contains(range, false);
}
public bool Contains(Range range, bool excludeExtremities)
{
return Contains(range.lower, excludeExtremities) && Contains(range.upper, excludeExtremities);
}
public bool Contains(int value)
{
return Contains(value, false);
}
public bool Contains(int value, bool excludeExtremities)
{
Range testRange = excludeExtremities ? this.Expand(-1) : this;
if (testRange.lower < lower)
throw new InvalidOperationException("The span of the specified range is too small to contain another range");
return (value >= testRange.lower) && (value <= testRange.upper);
}
public Range Expand()
{
return Expand(1);
}
public Range Expand(int value)
{
return new Range(lower - value, upper + value);
}
public override bool Equals(object obj)
{
return obj is Range && this == (Range)obj;
}
public bool Equals(Range other)
{
return this == other;
}
public override int GetHashCode()
{
return lower ^ upper;
}
public override string ToString()
{
return string.Format("{0}, {1}", lower, upper);
}
}
}
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace DaveyM69.Windows.Forms
{
public class RangeControl : Control
{
private static readonly Size Default_Size = new Size(200, 50);
[Category("Range")]
public event EventHandler ExcludeExtremitiesChanged;
[Category("Range")]
public event EventHandler InnerRangeChanged;
[Category("Range")]
public event EventHandler OuterRangeChanged;
[Category("Range")]
public event EventHandler ValueChanged;
private bool excludeExtremities;
private Range innerRange;
private Range outerRange;
private int value;
public RangeControl()
{
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.ResizeRedraw |
ControlStyles.Selectable |
ControlStyles.SupportsTransparentBackColor |
ControlStyles.UserPaint, true);
excludeExtremities = false;
innerRange = new Range(25, 75);
outerRange = Range.Percentage;
value = 50;
}
protected override Size DefaultSize
{
get { return Default_Size; }
}
[Category("Range"),
DefaultValue(false)]
public bool ExcludeExtremities
{
get { return excludeExtremities; }
set
{
if (excludeExtremities != value)
{
excludeExtremities = value;
OnExcludeExtremitiesChanged(EventArgs.Empty);
InnerRange = innerRange;
Value = this.value;
}
}
}
[Category("Range"),
DefaultValue(typeof(Range), "25, 75")]
public Range InnerRange
{
get { return innerRange; }
set
{
value = outerRange.Contain(value, excludeExtremities);
if (innerRange != value)
{
innerRange = value;
Invalidate();
OnInnerRangeChanged(EventArgs.Empty);
Value = this.value;
}
}
}
[Category("Range"),
DefaultValue(typeof(Range), "0, 100")]
public Range OuterRange
{
get { return outerRange; }
set
{
if (outerRange != value)
{
outerRange = value;
OnOuterRangeChanged(EventArgs.Empty);
InnerRange = innerRange;
}
}
}
[Category("Range"),
DefaultValue(50)]
public int Value
{
get { return value; }
set
{
value = innerRange.Contain(value, excludeExtremities);
if (this.value != value)
{
this.value = value;
Invalidate();
OnValueChanged(EventArgs.Empty);
}
}
}
protected virtual void OnExcludeExtremitiesChanged(EventArgs e)
{
EventHandler eh = ExcludeExtremitiesChanged;
if (eh != null)
eh(this, e);
}
protected virtual void OnInnerRangeChanged(EventArgs e)
{
EventHandler eh = InnerRangeChanged;
if (eh != null)
eh(this, e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
}
protected virtual void OnOuterRangeChanged(EventArgs e)
{
EventHandler eh = OuterRangeChanged;
if (eh != null)
eh(this, e);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
protected virtual void OnValueChanged(EventArgs e)
{
EventHandler eh = ValueChanged;
if (eh != null)
eh(this, e);
}
}
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
If you open a new MVC2 App in VS 2010 in the HomeController in the Details action method you get this:
public ActionResult Details(int id)
{
return View();
}
Can anyone tell me what the /5 is for?
Thanks
Darrall
|
|
|
|
|
I think 5 is just a sample id. That URL is the equivalent of /Home/Details.aspx?id=5.
ShamWow
|
|
|
|
|
|
Hello everyone,
How does one go about creating an array that can be passed to C++ methods (as a pointer to a DLL exported function) and an array that will not be garbage collected.
I need to create an array in c#, pass it to a C++ DLL and I want the C++ side of things to control the lifetime of the array (so to be able to call delete[] on it).
Is there any easy way to achieve this in C#?
Thanks,
Keith
|
|
|
|
|
That is not how it works. The party allocating is also responsible for freeing; having a managed object/array is fine, you can keep the GC from moving or collecting it, however it is also the managed side that should undo this.
I have an unfinished article about that and more here[^]. Have a look at the callback stuff, so your native part can report it is done with the array.
|
|
|
|
|
Thanks you. The GCHandle method seems to be exactly what I need.
Thanks,
Keith
|
|
|
|
|
you're welcome.
|
|
|
|
|