Valhalla Legends Forums Archive | .NET Platform | [C#] For Loop Help

AuthorMessageTime
FrostWraith
I have just ventured into C# and was wondering is there was a way to mimic VB's Step parameter inside a For ... Loop. I would like to make it equivalent to 'Step 2'. As my first project I have been trying to convert Groks AddChat Procedure.  Help for this issue and and criticism is welcome.
[code]        private void AddChat(params String[] saElements)
        {
            int i;
            for (i = saElements.GetLowerBound(0); i < (saElements.GetUpperBound(0) + 1); i++)
            {
                rtbChat.SelectionStart = rtbChat.Text.Length;
                rtbChat.SelectionLength = 0;
                rtbChat.SelectionColor = System.Drawing.Color.FromName(saElements[i]);
                rtbChat.SelectedText = saElements[i++] + "\n";
                rtbChat.SelectionStart = rtbChat.Text.Length;
            }
        }[/code]
March 10, 2007, 9:20 PM
K
Yes:

[code]
for (i = saElements.GetLowerBound(0); i < (saElements.GetUpperBound(0) + 1); i++)
[/code]

The syntax of a for loop is
for ( initial-expression; loop-condition; loop-action)

Your loop action is to increment i by one (i++).  Try changing it to increment i by some other value:

[code]
for (i = saElements.GetLowerBound(0); i < (saElements.GetUpperBound(0) + 1); i += n)
[/code]
March 10, 2007, 9:26 PM
FrostWraith
Simple enough. Thx.
March 10, 2007, 9:27 PM
Myndfyr
[quote author=FrostWraith link=topic=16462.msg166546#msg166546 date=1173562057]
Simple enough. But now it seems it will only use one of my colors.
[code]AddChat("Green", "line1", "Blue", "line2");[/code]
Outputs
line1 in green
line2 in green
[/quote]
You need to set the SelectedText property before you set the SelectionColor property.  You're setting SelectionLength = 0, and the SelectionColor = green, but since you don't have any text selected, setting the selected color to green or blue doesn't get you anything.
March 10, 2007, 9:43 PM
FrostWraith
Thank you.

Along the lines of my function, I would like to know how to make the variables being passed be able to be both a long or a string.  How would I do this??
March 12, 2007, 1:19 AM
Myndfyr
You can't do it with params.  One way to do it would be to create a structure and pass copies of structures through.  This is one way that JinxBot does it (here is the ChatNode structure and the chat control that uses them).  Plugins that use the chat box can add chat using params, though, as in the BNCS plugin (note that it's incomplete, "it's" being the plugin):

[code]
            private void DisplayWelcomeMessage(IUiHost host)
            {
                host.AddChat(new ChatNode("Welcome to the JinxBot Battle.net(tm) plugin!", Color.Orange));
                host.AddChat(new ChatNode("Did you know that the JinxBot Battle.net Plugin can be configured to use files from your game to make the user interface look more like the official client?  JinxBot's Battle.net Plugin protects you from copyright infringement by doing this.  For more information, see the ", Color.SteelBlue),
                    new ChatNode("\"Configuring JinxBot and Battle.net\"", Color.LightSteelBlue, "http://www.jinxbot.net/index.php?title=Configuring_JinxBot_and_Battle.net"),
                    new ChatNode(" on the JinxBot website.", Color.SteelBlue));
                host.AddChat(new ChatNode("Battle.net(tm) and the games emulated by this plugin are trademarks or registered trademarks of Blizzard Entertainment in the United States and/or other countries, and has no affiliation with JinxBot or the JinxBot Battle.net plugin.  For more information on software included with this plugin, see the About dialog.", Color.SteelBlue));
            }
[/code]
March 12, 2007, 1:43 AM
JoeTheOdd
[code]    private void addChat(params Object[] saElements)
    {
        rtbChat.SelectionStart = rtbChat.Text.Length;
        rtbChat.SelectionColor = Color.White;
        rtbChat.SelectedText = "[" + System.DateTime.Now.ToLongTimeString() + "] ";
        for(int i = 0; i < saElements.Count; i+=2)
        {
            rtbChat.SelectionColor = (Color)  saElements[i];
            rtbChat.SelectedText  = (String) saElements[i+1];
        }
        rtbChat.SelectedText = System.Environment.NewLine;
        rtbChat.SelectionStart = rtbChat.Text.Length;
    }[/code]

That's more-or-less how I'd do it. I'm not sure about that Color typecast (I didn't test this), but other than that you should be good.
March 21, 2007, 7:47 AM

Search