|
Confused me this. I would expect you to be able to change Size and Location on a UserControl without issue.
Where abouts are you trying to change these properties? In the UserControl itself or elsewhere? When you say the IDE complained that the properties could not be modified, what was the exception message?
Regards,
Rob Philpott.
|
|
|
|
|
I was trying to change these values in the constructor, and to provide functions within the control to change them. Apparently the correct way to do this is from the outside, not inside the control. In retrospect, the only thing I really need to do in the constructor is to access the Size property in order to scale the visual elements of the control.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
I'm not sure I understand your question.
Does the following not work for you?
var myUsercontrol = new UserControl1();
myUsercontrol.Size = new Size(130,150);
myUsercontrol.Location = new Point(10,30);
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Good question - I never tried that. Nick's post below identified what I was trying to do, and explains nicely why it didn't work. The book I'm using to learn this is quite insistent that the class UserControl is very important, then skimps on explaining how to make one. An unusual flaw, given the quality of the rest of the book.
Thanks!
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
I rarely use UserControl as most things I need to do can be done by using either Control (UI controls) and drawing everything I need myself, or Component (no UI required).
UserControl derives from ContainerControl so it's handy for using the designer for drag 'n' drop of other controls to make a composite control.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
I've been thinking about your response all day, trying to figure out where my reasoning has gone wrong. I selected UserControl as a base class only because the book I'm using as a guide recommends it. But your point about using Control as a base and drawing what's needed actually fits my needs more closely. What I'm trying to create is a reusable control that provides a Gannt chart style of scheduling bar. I'm using a rectangle for the task duration, overlaid with another rectangle to show the progress, with triangles at each end to mark the start and finish points. Each triangle also contains an anchor point to allow it to be linked to other dependent tasks. These are just graphical objects that need to be manually drawn, and your approach seems more logical. Where I'm getting stuck, I think, is in scaling the graphical objects in response to changes in the size of the base object, and in placing the control on a form. These changes would always be at design-time, never in run-time. But the control does need to respond to run-time changes in duration and progress.
Can you suggest an approach that you would use in implementing this control? This is my first attempt in C# at creating a control, and it's already been quite instructional. Your insight would be much appreciated.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
It does sound like the bar should derive from Control , and a chart of many bars would probably derive from ContainerControl , although it's quite possible to have just the one control which handles both, which would then derive just from Control .
If just the one control then scaling would be easy as you can do everything as a percentage of the Size/ClientSize. I think this method would be the easiest, but not necessarily the most flexible.
If you want the first method, then you may have to allocate each bar a space in the Chart container dynamically and call a custom ResizeRequest(Size maxSize) method in the bar, or something similar. Normally containers draw what they can but have no responsibility for resizing the contents and simply don't draw what won't fit.
It may be worth examining the .NET source and seeing how Dock and Anchor are implemented as it seems to be similar to what you want to do.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thanks, Dave!
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
This will not work:
Size.Height = 100;
Size.Width = 100;
Location.X = 100;
Location.Y = 100;
This is because Size is a System.Drawing.Size and Location is a System.Drawing.Point . Both of these types are struct s, which means that they are passed by value not by reference.
So when you write:
Size size = Size;
Point location = Location;
you are actually getting copies of the structs, not a reference to the original data.
This means it makes no sense to try to change, say the Height of the copy of the Control's Size and the compiler flags this as an error for you.
As Eric said, you can replace the underlying Size and Point like this:
Size = new Size( 100, 100 );
Location = new Point( 100, 100 );
This works because in the Control class, the properties have setters as well as getters:
partial class Control
{
public Size Size { get; set; }
public Point Location { get; set; }
}
Now, if you add new properties to your Control called Size and Location, they have no relation to the base class properties with the same names. The new properties just 'hide' the base class properties. The compiler warns you about doing this, as it is legal, although confusing and not recommended. You can add the new keyword to get rid of the warning, but this is also confusing and not recommended.
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
Thanks, Nick! That makes perfect sense.
Your first example is exactly what I was trying to do.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
Sometimes you can override the base classes properties, other times it is necessary to replace them using the new keyword.
In the case of Size , Control uses another readonly property DefaultSize alongside to set the initial size so that needs to be dealt with too. I've never needed to change the Location property inside the control itself as it is normally accessed by the control's host for layout within it's client area - I can't think of a reason to need to replace the default behaviour of this property.
Here's an example using new , override and an additional property
public class MyControl : Control
{
public event EventHandler IDChanged;
private static readonly Size MyDefaultSize = new Size(150, 50);
public const int MaxID = int.MaxValue;
public const int MinID = 0;
private int id;
public new Size Size
{
get { return base.Size; }
set { base.Size = value; }
}
protected override Size DefaultSize
{
get { return MyDefaultSize; }
}
public int ID
{
get { return id; }
set
{
if (value < MinID || value > MaxID)
throw new ArgumentOutOfRangeException(
"ID",
string.Format(
"ID must be between {0} and {1}",
MinID, MaxID));
if (id != value)
{
id = value;
OnIDChanged(EventArgs.Empty);
}
}
}
protected virtual void OnIDChanged(EventArgs e)
{
EventHandler eh = IDChanged;
if (eh != null)
eh(this, e);
}
}
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Thanks for the excellent example, Dave.
I was thinking, for some odd reason, that I needed to handle location and setting myself in the constructor, rather than relying on the base class to do it for me.
"A Journey of a Thousand Rest Stops Begins with a Single Movement"
|
|
|
|
|
i have to check if my string span to more than one lines than append space at start of each line..hw can this be done?
Regards
Ali
|
|
|
|
|
It would be great if you give some example so that I can give you proper solution.
Thanks !
Abhijit Jana | Codeproject MVP
Web Site : abhijitjana.net
Don't forget to click "Good Answer" on the post(s) that helped you.
|
|
|
|
|
dr[i - 1] = " " + fieldValues[i].ToString();
this is my code...it only appends space at first line of string but if string spans multiple lines hw to append space at start of each line.
|
|
|
|
|
You have to split the original string at new lines, add the space and then Join them together again. This is an example:
string original = ...;
string spaced = String.Join( "\n", original.Split( '\n' ).Select( s => " " + s ).ToArray() );
Nick
----------------------------------
Be excellent to each other
|
|
|
|
|
dr[i - 1] = " " + fieldValues[i].Replace("\n","\n ");
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
I got the help from MSDN that ViewPort3D could solve it
But I don't know how to code it
not in a WPF Project
I want to Display it in a WindowsFormsApplication Project
(in the Form1)
If display a 2DWPF I knew that DraingVisual could deal it in WFA
but How to display a 3D(WPF)?
thanks to all assistants!
This is my first question here,
and I am a new learner
Please excuse me for any faults.
|
|
|
|
|
Message Closed
modified 23-Nov-14 7:14am.
|
|
|
|
|
Thank you verry much,
and especially thanks for your introduction of the "online MSDN"
I solved it!
|
|
|
|
|
Hey,
I have a nullable 2d Double array which I want to convert to a non nullable 2d Double array. Is there any sane way of doing this?
Cheers!
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
By nullable 2d array, I presume you mean an array containing double?s.
So you want to convert double?[] to double[]... What happens if there is a null in there then?
Regards,
Rob Philpott.
|
|
|
|
|
At the point where I want to convert from nullable to non nullable, there are no null values.
I indeed mean Double?[,];
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|
|
var nullableDoubles = new double?[2][];
nullableDoubles[1] = new double?[]{null, 1.1, null, 1.2};
double[][] nonNullableDoubles = nullableDoubles.Select(doubleArray=>{
if(doubleArray==null)
return null;
else
return doubleArray.Where(doubleValue => doubleValue.HasValue).Cast<double>().ToArray();}).ToArray();
--EricDV Sig---------
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them.
- Laurence J. Peters
|
|
|
|
|
Hey,
Since I'm getting an error saying that 'double?[*,*]' does not contain a definition for 'select', I'm assuming I need to import some namespace. Which one?
Cheers!
Jeroen De Dauw
---
Forums ; Blog ; Wiki
---
70 72 6F 67 72 61 6D 6D 69 6E 67 20 34 20 6C 69 66 65!
|
|
|
|