Need a little C# help again. (i'm still a noob)

BikerEcho

Fully Optimized
Messages
4,029
Location
Denmark
Hey Gang.
Last time I got some good advice from iPwn and Berry120.
Now i need a little help again. See i don't have the source code anymore for the small program. I want to make it again.
I figured i could do it without help and just look at the old thread, but i have already come to a halt. I need to open the next form in my program.
I have a main form called FormMain. Then another form called FormEdit.
I need to open that form when hitting the button called ButtonSettings.
I remember it to be easy. So i don't understand what i am doing wrong.

My code is

Code:
private void ButtonSettings_Click(object sender, EventArgs e)
        {
            Form FormEdit = new Form();
            FormEdit.Show();
        }

It just opens a new self generated form with nothing in it.
I have also tried just with "FormEdit.Show();"
That doesn't work ether.
 
I'm no expert, but of course it's a blank form if you've only just instantiated it! You need to populate the form with the data you want, whether you pass it in as an argument or use a new method.
 
Ohh wait. i got it.

This works:
Code:
private void ButtonSettings_Click(object sender, EventArgs e)
        {
            Form FE = new FormEdit();
            FE.Show();
        }

Why do i have to make a new temporarily name?
Hmm. anyway. I am sure there are more questions coming.
 
You were using a reserved word as the variable name (FormEdit), but I'm guessing you got that now haha ;)

Also, with C#, if the variable type is defined (e.g. = new Something()), you can use "var" as the declaration. For example:

Code:
var FE = new FormEdit();
FE.Show();
var returnString = FE.SomeFunction...

And so on...
 
Thanks. I am properly gonna need a little more help tomorrow. We will see how i do on my own this time :)
 
I wish I had grabbed this thread earlier haha. I just finished C# for my computer science classes and I literally hated C# with a passion, but I know it quite well
 
So i have come to a halt.
I need to split a string in 2 and add the data to 2 other strings. (test1 and test2)
The date in the string called "line" is "09:45 - 15:00" So i need test1 = "09:45" and test2 = 15:00
How do i do that?
I have tried different things like the .split function, but then it talks about arrays.

The reason i have made the string called buffer is because i can't seam to use the string "line" anyware else than inside the "using Streamreader sr" code.
How can i use the string "line" below the first "}"
Here are my code

Code:
private void timer1_Tick(object sender, EventArgs e)
        {
            string Buffer;
            string test1;
            string test2;

            var targetFile = @"c:\testmappe\test.txt";
            using (StreamReader sr = new StreamReader(targetFile))
            {
                String line = sr.ReadToEnd();
                LabelTime.Text = line;
                Buffer = line;
            }


}
 
I'm sure berry will have a more accurate response on this, but....

You pretty much have two options.
1. Use the String.Split method.
2. Use the Substring() method.

Example 1
Code:
static void Main(string[] args)
        {
            var delimiter = '-';
            string startTime;
            string stopTime;
            string completeTime;

            var targetFile = "c:\\testmeppe\\test.txt";
            using (var reader = new StreamReader(targetFile))
            {
                completeTime = reader.ReadToEnd();
                reader.Close();
            }

            var timesVar = completeTime.Split(delimiter);

            startTime = timesVar[0];
            stopTime = timesVar[1];

        }


Example 2
Code:
 static void Main(string[] args)
        {
            var delimiter = '-';
            string startTime;
            string stopTime;
            string completeTime;

            var targetFile = "c:\\testmeppe\\test.txt";

            using (var reader = new StreamReader(targetFile))
            {
                completeTime = reader.ReadToEnd();
                reader.Close();
            }

            startTime = completeTime.Substring(0, 5);
            stopTime = completeTime.Substring(8, 5);
        }

The problem with both of these is that, if that file changes in any way other than that exact format (09:00 - 00:00), then the program crashes in both examples.

Why not pass arguments to the program on launch?

e.g. C:\SomeProgram.exe "09:30" "15:00"

Then;
Code:
private static readonly Regex myTimePattern = new Regex(@"^(\d+)(\:(\d+))?$");

        private static void Main(string[] args)
        {
            if (!args.Any() || args.Count() < 2)
                SomeFunctionToReportAFailure("Invalid Argument");

            var startTimeText = args[0];
            var stopTimeText = args[1];
            TimeSpan startTime;
            TimeSpan stopTime;

            try
            {
                startTime = ConvertTime(startTimeText);
                stopTime = ConvertTime(stopTimeText);
            }
            catch (Exception e)
            {
                SomeFunctionToReportAFailure(e.Message);
            }


        }

        private static TimeSpan ConvertTime(string time)
        {
            if (time == null)
                throw new ArgumentNullException("time");
            var m = myTimePattern.Match(time);
            if (!m.Success)
                throw new ArgumentOutOfRangeException("time");
            var hh = m.Groups[1].Value;
            var mm = m.Groups[3].Value.PadRight(2, '0');
            var hours = int.Parse(hh);
            var minutes = int.Parse(mm);
            if (minutes < 0 || minutes > 59) 
                throw new ArgumentOutOfRangeException("time");
            var value = new TimeSpan(hours, minutes, 0);
            return value;
        }


EDIT:
How can i use the string "line" below the first "}"

This will probably solve your problem huh? haha, just declare it before your using statement.
Code:
String line;
using(var blablabla){
line = readingStuff
}
line.Something...

If you declare inside a curly set, then that variable only exists in those.
 
Last edited:
Thank you very much
I'll try method 2. The data in the file is always xx:xx - xx:xx. I am gonna make some if statements in the settings menu that will prevent users from typing in anything else. So the program shouldn't crash.

I will have to wait till Monday. The source files are at work. I do my programming in the breaks.

This will probably solve your problem huh? haha, just declare it before your using statement.
Code:
String line;
using(var blablabla){
line = readingStuff
}
line.Something...

If you declare inside a curly set, then that variable only exists in those.

:facepalm::facepalm::facepalm::facepalm::facepalm: Ofcause.
The code was copy phased. If i made it myself i think i would have done it right.
 
Back
Top Bottom