|
Is it possible in C#?
I have a class, Hand. It contains an Array of cards. I have defined a property in the array which returns the array of cards that it contains, but it makes for convoluted syntax:
Player.Cards.Card[1];
Where Player.Cards refers to the "hand" a player holds.
If my Hand class looks something like:
public class Hand
private Card[] m_Hand;
public Hand(params Card[] cards)
{
m_Hand = cards;
}
public Card[] Cards
{
get
{
return m_Hand;
}
}
}
Can I simply overload the index operator for my Hand class and get rid of the Cards property?
So I could do something like:
Player.Cards[0];
Would refer to: Player.Cards.m_Hand[0];
That make sense?
As always, thanks!
|
|
|
|
|
Yes you can, using this as a property name and optionally using the IndexerNameAttribute to get it a name (for some languages like VB.NET have to refer to the indexer while C# doesn't) other than "Item":
public class Hand
{
[System.Runtime.CompilerServices.IndexerName("Card")]
public Card this[int index]
{
get { return m_Hand[index]; }
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi,
I can't change the background color of a specific cells in a datagrid on RUN TIME. I find a article but this article works on DESIGN TIME.
My problem is: For example user select a color on the colorDialog and i want to change current cell color. Not all row, just one cell change it.
For example: select green, ROW#1 CELL#5 is green
select red, ROW#2 CELL#5 is red
select blue, ROW#3 CELL#5 is blue ....
Please help me! Pleseeeeeeeeeeeeeeeeeeee...
I try from 3 day.
BORAN
|
|
|
|
|
|
This article on Design Time. I want to change on Run Time. Yes, i can solve it but some new problem. I can't understand how to add component class. I add ColumnStyle.cs but error on ColumnStyle() function.
Thanks alot
|
|
|
|
|
In your class that derives from DataGridColumnStyle and in your overload to DataGridColumnStyle.Paint you'll get a CurrencyManager and a position to use with that CurrencyManager to get the data for the cell being drawn and then paint it how you want.
I'm really not sure how you want to set it at run-time. Please be specific. Do you want some property to arbitrarily or based on data? The article is about how to set the color of a cell based on the value of the data in that cell and that happens at run time.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
How to add this code on my form:
protected override void Paint(Graphics g,Rectangle Bounds,CurrencyManager Source,int RowNum, Brush BackBrush ,Brush ForeBrush ,bool AlignToRight)
{
bool bdel = (bool) GetColumnValueAtRow(Source, RowNum);
if(bdel == true)
BackBrush = Brushes.Coral;
else
BackBrush = Brushes.White;
g.FillRectangle(BackBrush, Bounds.X, Bounds.Y, Bounds.Width,Bounds.Height);
System.Drawing.Font font = new Font(System.Drawing.FontFamily.GenericSansSerif , (float)8.25 );
g.DrawString( bdel.ToString() ,font ,Brushes.Black ,Bounds.X ,Bounds.Y );
}
|
|
|
|
|
You need to write your own class that extends DataGridColumnStyle , then you instantiate that class and add it to a DataGridTableStyle that you add to DataGrid.TableStyles . This isn't a component you can just drag and drop, but you have to write your class and use it (like writing your own control and adding that to a form.
The article details how to do this and the sample source gives a thorough example of using the sample class. The link I gave you earlier shows a good example of how to use table styles to customize how the data is displayed; otherwise, only the base DataGridTextBoxColumn and DataGridBoolColumn are used.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I am looking into extending an existing solution to perform a minor user management. It's for the following scenario:
I want to block a user's account entirely so that it can't login. As soon as the user checks in with his pass, his account (linked to that pass) has to be enabled again. But he should be able only to login to a specific computer. After he logs out of the computer again and he checks out of the room using his card his account should be blocked again.
From my view on this scenario I need two things:
- First I need to be able to enable/disable accounts using LDAP (Becaus its novell they use).
- Second I need to be able to manage the account so it has access to a computer. (I know how to do it by hand using the management tools, but not from the LDAP)
Anyone have any ideas on how I might be possible to do this? I think I found the solution to the first part of the problem. But not to the second part of the problem.
WM.
What about weapons of mass-construction?
|
|
|
|
|
Hello! I just wanted to ask a question: If I write a simple C# applicationm, for examle a "Hello World!" program, do I already use the .NET Framework?
Thanks in advance!
Have a nice day!
|
|
|
|
|
Yevgeny Efter wrote: If I write a simple C# applicationm, for examle a "Hello World!" program, do I already use the .NET Framework?
Yes
'A programmer is just a tool which converts caffeine into code'
|
|
|
|
|
To be more specific, C# is one of many language that compile to Intermediate Language, or IL. VB.NET is another example. All those compile to IL that then gets compiled on the fly by the Common Language Runtime (CLR).
The .NET Framework is a set of base class libraries (like Windows Forms, ASP.NET, etc.) and the CLR.
This (and a few more acronyms) allow languages to work together, like a Windows Forms application written in VB.NET can use a library written in C# because when both are compiled they are compiled to IL.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Im trying to get the mouse to perform a drag and drop using only the api. I can get the mouse to move around and perform clicks for me but the challenge is the drag and drop operation. im trying to store the previous state of the mouse (*ie when it was pressed down*) and then while the previous state==mousedown start dragging and holding it down, until i get a left mouse up message. but i cant get it to work correctly. here is the code im using
Input[] input = new Input[ 1 ];
input[0].type = INPUT.MOUSE;
input[0].mi.dx = x2;
input[0].mi.dy = y2;
//input[0].mi.dwFlags = MOUSEEVENTF.MOVE| MOUSEEVENTF.ABSOLUTE ;
if(prev.wParam==UserActivityHook.WM_LBUTTONDOWN)
{
input[0].mi.dwFlags =MOUSEEVENTF.LEFTDOWN| MOUSEEVENTF.ABSOLUTE;
SendInput( ( UInt32 ) input.Length, input, Marshal.SizeOf( input[ 0 ]) );
Console.WriteLine("test");
}
input[0].mi.dwFlags = MOUSEEVENTF.MOVE| MOUSEEVENTF.ABSOLUTE ;
SendInput( ( UInt32 ) input.Length, input, Marshal.SizeOf( input[ 0 ] ) );
Any ideas? i have been looking around online and will continue to. Thanks alot for your help
|
|
|
|
|
I know I can overload the same function to take specific parememters, but I'm looking for something a little more than that.
I want to return an array of ints, based on the number of ints passed into the function (really, I'm using this with objects).
public static int[] BuildArray( /* ACCEPT A MULTIPLE NUMBER OF INTS, WITHOUT BEING IN AN ARRAY ALREADY /* )
{
int[] ints = new ints[ /*NUMBER OF INTS PASSED INTO THE FUNCTION /* ];
int i = 0;
foreach( /* INT PASSED INTO THE FUNCTION */ )
ints[i] = /* EACH INT PASSED IN */;
i++;
}
return ints;
}
... The method accepts a multiple number of paremeters, all int. I want it to build an array of them, then pass that back out.
Is this possible? Like I said, I know I can overload the function a whole bunch of times like so:
public static int[] BuildArray(int i1, int i2)
{
// BUILD ARRAY AND RETURN IT
}
public static int[] BuildArray(int i1, i2, i3)
{
// BUILD ARRAY AND RETURN IT
}
public static int[] BuildArray(int i1, i2, i3, i4)
{
// BUILD ARRAY AND RETURN IT
}
... but that's not elegant, and I'll have to write a method for every freaking number of ints that could be passed in. Ugh.
Any solution here?
Thanks!
|
|
|
|
|
I think you have to use the params [^] keyword:
public static int[] BuildArray(params int[] numbers){
return numbers
}
Pompiedompiedom...
"..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.."
-- Mark McCormick
|
|
|
|
|
Works perfectly. Thank you very much.
One thing I noticed is that having the following two constructors threw an error:
public Hand(Card[] cards)
{
m_Hand = cards.Clone();
}
public Hand(params Card[] cards)
{
m_Hand = cards;
}
Caused a "redefinition" error.
The first allowed me to call it as such: Hand myHand = new Hand(cards); // REQUIRING A PREBUILT ARRAY OF CARDS
But did not allow me to do: Hand myHand = new Hand(card1, card2, card3);
The second will take a prebuild array of cards and work fine, or allow me pass in any number of cards and will automatically build the cards array from them.
I had to delete the first constructor, but that's fine. Works perfectly. Thank you.
|
|
|
|
|
How about a static helper method on your Hand class
class Hand
{
private Card[] cards;
public Hand(Card[] cards)
{
this.cards = cards;
}
public static Hand CreateFrom(params Card[] cards)
{
return new Hand(cards);
}
}
now you can do
Card [] myCards = new Card[]{...};
Hand myHand = new Hand(myCards);
or
Hand myHand = Hand.CreateFrom(Card1, Card2, Card3);
-- modified at 16:14 Saturday 5th November, 2005
|
|
|
|
|
J4amieC wrote: How about a static helper method on your Hand class
Why? Its already Card[] !
xacc-ide 0.0.99-preview7 now with C#, C, C++, IL, XML, Nemerle, IronPython, Perl, Caml, SML, Ruby, Flex, Yacc, Java, Javascript, Lua, Prolog and Boo highlighting support!
|
|
|
|
|
because he couldnt have 2 constructors one as "Card[] cards" and one as "params Card[] cards".
It was just a suggestion, call it syntactic sugar
|
|
|
|
|
The reason is that the methods or constructors would have the same signature and that, of course, isn't allowed for overloads. See the IL for a method defined using the params keyword in C#:
.method public hidebysig instance void Foo(object[] objects) cil managed
{
.param [1]
.custom instance void [mscorlib]System.ParamArrayAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 8
IL_0000: nop
IL_0001: ret
} As you can see, the effect is achieved using an attribute but the parameter itself is just an object[] array.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
I am trying to implement a dynamic system tray icon in C#. There are tons of applications, like FreeRam XP and so forth, that have a system tray that updates with numbers or text. I was curious if anyone knew of a means for implementing such a system tray in C#. I am familiar with the notifyicon methodology in C# but I have never been able to find a means for doing it. Any ideas?
|
|
|
|
|
Depending how dynamic your icon would appear to be, you have a couple of choices. If you really only have a small handful of states - like some CD ripping or burning software I've seen - you could just have a collection of icons that show the different states and loop through them.
If you need more control and dynamic capabilities than that you must draw your own icons. You should find several good articles here on Code Project about drawing icons that should help you.
So, consider whether you need only a few icons to rotate or otherwise show state (like something's open, closed, or not present) and use a static list of icons or draw your own. You don't even need to save them to a file - just cache them in a list in memory and assign them to NotifyIcon.Icon .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Customer Product-lifecycle Experience
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for the reply. I did an intensive search and found a posting on CodeProject.com from a couple of years ago, describing how to accomplish this task. Below is the code snippet:
Bitmap i = new Bitmap(16, 16);
Graphics g = Graphics.FromImage(i);
SolidBrush blackBrush = new SolidBrush(Color.Black);
g.DrawString(currentTemp,this.Font,blackBrush,new Point(0));
g.Dispose();
notifyIcon1.Icon = Icon.FromHandle(i.GetHicon());
|
|
|
|
|
You might consider using the using statement for cleaner and safer code to dispose objects because it compiles such that the call to IDisposable.Dispose is in the finally block of a try/finally :
Bitmap i = new Bitmap(16, 16);<br />
using (Graphics g = Graphics.FromImage(i))<br />
{<br />
g.DrawString(currentTemp, this.Font, Brushes.Black, new Point(0, 0));<br />
}<br />
notifyIcon1.Icon = Icon.FromHandle(h.GetHicon()); <br />
<br />
This posting is provided "AS IS" with no warranties, and confers no rights.<br />
<br />
Software Design Engineer<br />
Developer Division Customer Product-lifecycle Experience<br />
<span>Microsoft</span><br />
<br />
[<a href="http://www.codeproject.com/script/articles/list_articles.asp?userid=46969">My Articles</a>] [<a href="http://blogs.msdn.com/heaths/">My Blog</a>]
|
|
|
|
|
Hi there.
I am having problems making a small program I am making print out.
I put some Label controls on a form and also drew a line on the form. Then the plan is to make the program print out the text in the Labels and also the line drawn on the form.
The print_selected method is executed when the user selects Print from a menu. I can´t even get the PrintDialog box to appear
This is what I did so far:
private void print_selected (object sender, EventArgs ea)
{
PrintDocument prndoc = new PrintDocument();
PrintDialog prndlg = new PrintDialog();
prndlg.Document = prndoc;
if (prndlg.ShowDialog() ==DialogResult.OK)
{
prndoc.DocumentName ="Trial";
prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
}
prndoc.Print();
}
void OnPrintPage(object obj, PrintPageEventArgs ppea)
{
Graphics grfx = ppea.Graphics;
// I don´t know what code to put here so that the line and also the text
//contained in the Label controls gets printed out.
}
Can someone please help me make the program print out what I want it to print out?
Regards,
FJ
|
|
|
|
|