Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 12-03-2014, 08:24 AM   #1
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Need a little C# help again. (i'm still a noob)

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.
__________________

__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 12-03-2014, 08:30 AM   #2
Fully Optimized
 
joedaman633's Avatar
 
Join Date: Apr 2012
Location: England, Birmingham
Posts: 1,812
Default Re: Need a little C# help again. (i'm still a noob)

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.
__________________

__________________
Athlon II x4 645 || 1TB 7200rpm HDD || EVGA GTX 650Ti OC || 8GB DDR3 RAM || Windows 7 Home x64

i5 4210M || 500GB Samsung EVO 850 SSD || GeForce 825M || 16GB DDR3 RAM || Windows 10 x64
joedaman633 is offline   Reply With Quote
Old 12-03-2014, 08:31 AM   #3
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Need a little C# help again. (i'm still a noob)

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.
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 12-03-2014, 09:01 AM   #4
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Need a little C# help again. (i'm still a noob)

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...
__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 12-03-2014, 12:21 PM   #5
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Need a little C# help again. (i'm still a noob)

Thanks. I am properly gonna need a little more help tomorrow. We will see how i do on my own this time
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 12-03-2014, 02:06 PM   #6
Daemon Poster
 
YoungIT's Avatar
 
Join Date: Apr 2013
Location: USA
Posts: 661
Default Re: Need a little C# help again. (i'm still a noob)

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
__________________
"Difficulties mastered are opportunities won" -Sir Winston S. Churchill
YoungIT is offline   Reply With Quote
Old 12-05-2014, 05:22 AM   #7
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Need a little C# help again. (i'm still a noob)

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


}
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 12-05-2014, 09:32 AM   #8
..m.0,0.m..
Site Team
 
iPwn's Avatar
 
Join Date: May 2010
Location: USA
Posts: 3,870
Default Re: Need a little C# help again. (i'm still a noob)

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:
Quote:
Originally Posted by BikerEcho View Post
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.
__________________
Me: You'd think as the dominant species we wouldn't be so effing stupid.
J: We're just intelligent enough to be completely effing stupid.
iPwn is offline   Reply With Quote
Old 12-05-2014, 09:59 AM   #9
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Need a little C# help again. (i'm still a noob)

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.

Quote:
Originally Posted by iPwn View Post

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.
Ofcause.
The code was copy phased. If i made it myself i think i would have done it right.
__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Old 12-08-2014, 04:59 AM   #10
Fully Optimized
 
BikerEcho's Avatar
 
Join Date: Feb 2011
Location: Denmark
Posts: 4,013
Default Re: Need a little C# help again. (i'm still a noob)

I worked great. Thanks.
I'll let you know if (or when) i come to a problem again.
__________________

__________________
MOBO: Asus Maximus 6 Hero - GPU: Gigabyte GTX 980ti xtreme - CPU: I7-4770K - PSU: Corsair AX760
RAM: Corsair Vantage 2x4GB - SSD: Samsung 840 EVO 512 + LiteOn LCS-256 - Case: NZXT H440
BikerEcho is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 05:22 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0