• Welcome to Valhalla Legends Archive.
 

[C#] For Loop Help

Started by FrostWraith, March 10, 2007, 03:20 PM

Previous topic - Next topic

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.
        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;
            }
        }


K

Yes:


for (i = saElements.GetLowerBound(0); i < (saElements.GetUpperBound(0) + 1); i++)


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:


for (i = saElements.GetLowerBound(0); i < (saElements.GetUpperBound(0) + 1); i += n)

FrostWraith

#2
Simple enough. Thx.

MyndFyre

Quote from: FrostWraith on March 10, 2007, 03:27 PM
Simple enough. But now it seems it will only use one of my colors.
AddChat("Green", "line1", "Blue", "line2");
Outputs
line1 in green
line2 in green
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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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??

MyndFyre

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):


            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));
            }
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Joe[x86]

    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;
    }


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.
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.