DdoubleD 315 Posting Shark

Using the name "Main" for your form: I highly recommend you change this name to be something else that is more meaningful. Only because Main() is a reserved entry point symbol to the execution of an application and it is confusing (though I admit I didn't know you could do what you have done anyway).

DdoubleD 315 Posting Shark

Does it make a difference if I am just hiding the parent form? I am not actually destroying, am I?

What you say is true. Are you resetting the variable to the form's data as suggested?: user = main.user

What I don't understand is what the form is capable of doing since you are hiding it immediately after a modeless call to the Show() method, which prevents any user interaction with the form. e.g.

main.Show();
main.Hide();
DdoubleD 315 Posting Shark

This is a little weird how you are doing this--no big deal though. Can you paste the entire code or zip it up and attach as Serkan suggested?

DdoubleD 315 Posting Shark

GUESS: If you passing in the variable in the constructor, this won't work because the reference will be reset. After your call to ShowDialog(), reset your variable's content before the Form object is destroyed, e.g.:

frm.ShowDialog();
user = frm.user;
DdoubleD 315 Posting Shark

I am setting the variable on another form and passing it over. I know it is passing it because I tested it.

Show the line of code you are using to pass the variable to the form.

DdoubleD 315 Posting Shark

My guess is you are never actually setting the variable "user" or User property. Where is the code that assigns the TextBox value? e.g.

User = textBox1.Text;

Also, you probably want to rewrite your if statement to be:

if (User == admin)

instead of using the literal "Admin", since you already assigned it to variable 'admin', which is never used in this example.

DdoubleD 315 Posting Shark

QUESTIONS:
1) Where/when are you executing this code?
2) Where/how are you setting the value of the string "user"?
3) Can you include the whole code text?

kvprajapati commented: Good questions. +10
DdoubleD 315 Posting Shark

I see DoubleD's suggestion of the the caret Index or just creating a single custom control to accomplish it all to be the only two viable options so far to accomplish this.

Unfortunately, I lied about the CaretIndex property.:icon_redface:
That exists only for .ASPX code, which also uses C#--don't ask me why they didn't extend the functionality.

Anyway, here are a couple of methods that should accomplish the manipulation you described:

static int GetCaretPos(TextBox tbx)
        {
            return tbx.SelectionStart; // current position (won't work for you if user has selected some text)
        }
        static void SelectCharAtPos(TextBox tbx, int pos)
        {
            tbx.Select(pos, 1); // select one char beginning at pos
        }

However, you are really opening a can of worms when you begin to implement the behavior you described because: What happens if the user tries to select any portion of the text manually?--etc. I used to create controls to do funky stuff for clients years ago in WinAPI, which I could probably dig up, but do you really want to go to all this trouble?

DdoubleD 315 Posting Shark

A better way that captures what I said in a finally block, but, ideally, you would also put both your Stream creations inside of a try block themselves or ensure the caller is doing so.

public static void DoStreamReader()
        {
            StreamReader iFile = new StreamReader("Input.txt");
            StreamWriter oFile = new StreamWriter("output.txt");

            int totalScore = 0;
            int scoreCount = 0;
            decimal averageTestScore;



            if (File.Exists("input.txt"))
                try
                {
                    while (iFile.Peek() > 0)
                    {
                        totalScore = totalScore + Convert.ToInt32(iFile.ReadLine());
                        scoreCount++;
                    }
                    averageTestScore = totalScore / scoreCount;
                    oFile.Write(String.Format("The average of these scores is: {0}", averageTestScore));

                }

                catch (System.IO.IOException exc)
                {
                    //lblOutput.Text = exc.Message;
                }
                finally
                {
                    iFile.Close();
                    oFile.Flush(); // flush memory to your output file
                    oFile.Close(); // close your output file
                }
        }
DdoubleD 315 Posting Shark

Here are the changes I made for my test that worked:

public static void DoStreamReader()
        {
            StreamReader iFile = new StreamReader("Input.txt");

            int totalScore = 0;
            int scoreCount = 0;
            decimal averageTestScore;



            if (File.Exists("input.txt"))
                try
                {
                    while (iFile.Peek() > 0)
                    {
                        totalScore = totalScore + Convert.ToInt32(iFile.ReadLine());
                        scoreCount++;
                    }
                    averageTestScore = totalScore / scoreCount;
                    StreamWriter oFile = new StreamWriter("output.txt");
                    oFile.Write(String.Format("The average of these scores is: {0}", averageTestScore));
                    oFile.Flush(); // flush memory to your output file
                    oFile.Close(); // close your output file

                }

                catch (System.IO.IOException exc)
                {
                    //lblOutput.Text = exc.Message;
                }
                finally
                {
                    iFile.Close();
                }
        }
DdoubleD 315 Posting Shark

SUGGESTION: Remove the reference to the complete path and just use the filename. This way, you can move the exe and the input.txt file anywhere and it will run.

NEVERMIND: I see you already edited the code to do this.

DdoubleD 315 Posting Shark

If you make those changes, I just ran a test and it was successful--good luck.

DdoubleD 315 Posting Shark

Duh(me)!
Leave your iFile.Close(), but add oFile.Flush and oFile.Close()

This means you will have to either move the instantiation of the oFile object outside of the try block, or simply add the calls after your oFile.Write statement.

DdoubleD 315 Posting Shark

QUESTIONS:
1) What does the input file data look like?
2) Is it throwing an IOException?

COMMENT: I would put iFile.Flush(); call prior to the iFile.Close(); in your "finally" block to ensure the memory buffer is flushed to the file.

DdoubleD 315 Posting Shark

true and i agree with you but if he needs to create it it wouldn't already exist lol

Yea, I fired that one off a little quick and didn't notice the Read-Only attribute condition at first. I've never seen that static method before and I was afraid at first if the program was run more than once since there are no Open mode flags being passed in.

I like to toss message boxes in to check variables to be sure the correct output is coming during testing.

Not sure if you've seen this, but there is also an output window method you can use while in the debugger to output information, which you can go back and evaluate during execution and after exiting as well: System.Diagnostics.Debug.Write... methods.

DdoubleD 315 Posting Shark

If you pursue your customized editing (overwrite mode with char highlighting of caret position), you will need to manipulate how the control functions by checking the cursor/caret position.

You can use the TextBox.CaretIndex property to determine and set position.

You can use the TextBox.Select method to highlight/select the character at the current position.

Most programmers would create a custom control to do all of the things you are trying to achieve, but this sounds like a homework assignment you have decided to enhance even further. If so, kudos for your curiosity and extra effort!

DdoubleD 315 Posting Shark

WriteAllLines will fail if the file already exists and is Read-Only:

The following conditions may cause an exception:
* The file exists and is read-only.
* The path name may be too long.
* The disk may be full.

You should be sure you put into a try-catch block at least.

DdoubleD 315 Posting Shark

Check out the String.Length property to see how you can determine the number of characters already entered, then see if you can figure out where to put it in the code I gave you to prevent more than 16 being entered--good luck.

DdoubleD 315 Posting Shark

If you attach (wire) the following key events to your control, it will limit the chars to uppercase A thru P. You can also modify this code to convert lower chars to upper if you wish.

// uses boolean set in KeyDown event to determine whether this was an allowed character pressed
        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (!charAllowed)
            {
                // Stop the character from being entered into the control
                e.Handled = true;
            }
            else if (e.KeyChar < 'A' || e.KeyChar > 'P')
                e.Handled = true;
        }

        // Used to set flag to stop specific charactrs from entry into our control
        // Works in conjunction our KeyPress event handler.
        private void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            // Initialize the flag.
            charAllowed = true;

            // Determine whether the keystroke is allowed
            // Exclude those with the following modifiers
            if (Control.ModifierKeys == Keys.Control ||
                Control.ModifierKeys == Keys.Alt)
            {
                charAllowed = false;
            }
        }
DdoubleD 315 Posting Shark

How do you intend to use the search engine?

DdoubleD 315 Posting Shark

Try Listbox.FindString to lookup the typed text, then use the returned index to set ListBox.TopIndex = index, which will make those items beginning with your typed text appear at the top of the listbox.

DdoubleD 315 Posting Shark

That is a little more tricky, but not too much. Here is a code snippet written in Python that should give you the general idea. If you don't know Python (I don't), the syntax is similar enough I think you will understand it: http://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers-python

DdoubleD 315 Posting Shark

Some things to check:
1) using subreports pointing to datasource with invalid path
2) are you saving data with report? this used to cause a variety of problems and although it might not be the exact cause, you might get different results if you change this option
3) are the schemas exactly the same for each of the datasource locations?

DdoubleD 315 Posting Shark

Here is an interesting article that allows you to code your own using mixed-language: http://www.blackwasp.co.uk/NumberToWords.aspx