12 Replies - 1775 Views - Last Post: 11 June 2012 - 07:00 AM Rate Topic: -----

#1 She'ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 11

Accessing a button's property from another form

Posted 30 May 2012 - 12:51 PM

i am working on a desktop application, which contains three main forms: a splash screen, a mdi form and a login form.
first had issues to get my splash screen to work correctly, i wanted it to run for a specified time, then close and launch my mdi form, but the splash screen just didnt close, it kept running in the background.tried hiding the form using the hide method,but noticed that even when i close the mdi form the splashscreen form is still running. i placed a timer and a progress bar on it. this is the code i tried:
frmsplashscreen_load()
{
tmrstatus.start();
}
tmrstatus_tick()
{
  pbrStatus.Increment(1);
            if (pbrStatus.Value == 100)
            {
                tmrStatus.Stop();
            }
frmmdi mdi = new frmmdi();
mdi.show();
frmsplashscreen splash = new frmsplashscreen();
splash.close();
}


so i googled it and saw a video where a guy made use of a progressbar and a timer as well but now made use of the system.thread namespace. i tried it and it worked out fine.
now this is how the app is suppossed to run, after the splashscreen launches the mdi form it closes,ther are two buttons on the mdi form: one to login/logout and the other to exit, lets call the login/logout button btnlog. i didnt assign text to btnlog which i did from the load event of the mdi form, because i want it to serve as both a login and logout button. btnlog is to launch the login form, now the click event of the login button (btnlogin) on the login form is supposed to validate the user and allow access to the mdi form and also changing the text property of the login button on the mdi form to logout.
i first made the modifier of btnlog public and this is the code i tried:
btnlogin_click();
{
frmmdi md = new frmmdi();
md.btnlog.text = "logout";
}


now my app is re-running again from the splashscreen, anytime i click the btnlogin button. am thinking because of the object of the class i created its causing the thread to run again.
i have tried other things, but no solution yet.i would appreciate if i can get ideas on how to prevent this thread from re-running or a better way of changing the text property of btnlog.

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing a button's property from another form

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: Accessing a button's property from another form

Posted 30 May 2012 - 01:00 PM

What version of Visual Studios/.NET are you running with? In VS2010 you can right click on your project -> properties -> application tab -> check the 'enable application framework' -> wait for it to refresh -> then set a splash screen from any other form in the project..

edit - my thought is why chase more half patches with even MORE bad code - just go right to the source and have a no muss splash screen.

This post has been edited by modi123_1: 30 May 2012 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,940
  • Joined: 02-June 10

Re: Accessing a button's property from another form

Posted 30 May 2012 - 01:01 PM

Quote

now my app is re-running again from the splashscreen, anytime i click the btnlogin button


The program is not re-running, exactly.
You are telling it to make a new instance of the frmdi form - so it does. And that means anything that is part of the creation of that new instance of the form happens as well - Like login or whatever. It looks like the application launching, but it isn't: Its just another form launching.
3 frmmdi md = new frmmdi();


Quote

but the splash screen just didnt close


You must not have told it to close.
You did however make an all new instance of a splash screen, never show it, then closed that new instance.
14 frmsplashscreen splash = new frmsplashscreen();
15 splash.close();

You don't seem to understand the idea of instances of objects. Every time you say new widget() you are making a completely new object. It is in no way a reference to any existing widget that you may have already made somewhere else.
Was This Post Helpful? 1
  • +
  • -

#4 She'ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 11

Re: Accessing a button's property from another form

Posted 30 May 2012 - 01:32 PM

View Postmodi123_1, on 30 May 2012 - 01:00 PM, said:

What version of Visual Studios/.NET are you running with? In VS2010 you can right click on your project -> properties -> application tab -> check the 'enable application framework' -> wait for it to refresh -> then set a splash screen from any other form in the project..

edit - my thought is why chase more half patches with even MORE bad code - just go right to the source and have a no muss splash screen.

i am using vs 2011, done as said but couldnt find the enable application framework checkbox.but i saw a dropdown of target framework, dont know if its the same as what you are talking about. what do you mean by a muss splash screen?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: Accessing a button's property from another form

Posted 30 May 2012 - 01:36 PM

On the application tab... under the 'assembly_information' button.. in the middle of the tab.


Quote

what do you mean by a muss splash screen

You can ignore that - it is an idiom.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,940
  • Joined: 02-June 10

Re: Accessing a button's property from another form

Posted 30 May 2012 - 02:19 PM

Before you try to design and build an application you clearly need to learn the basics, including instantiating objects, how to even use Visual Studio, and so on. For now, work through a book and some on-line tutorials just to familiarize yourself with the application, C# and the general landscape of things and how things are done: Without the added complication of trying to design.

There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2.
Sure, 35 years ago the only way I could learn BASIC as a kid was to type programs from a book into a TRS-80 model 1 or Commodore VIC-20, then figure out the commands and syntax by watching the results as the program ran. There simply wasn't any courses for a kid back then.

But now, you can't help but to trip over "Learn {coding language} in 3 weeks" type books and the entire internet is at your disposal for tutorials.

I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Mar 2012
Spoiler

Was This Post Helpful? 0
  • +
  • -

#7 She'ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 11

Re: Accessing a button's property from another form

Posted 30 May 2012 - 10:57 PM

ok..tanks guys....but still any ideas on how to access the btnlog from my login form?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,940
  • Joined: 02-June 10

Re: Accessing a button's property from another form

Posted 31 May 2012 - 06:17 AM

You don't. You raise events. Directly accessing GUI controls between forms is just plain wrong.

How do I?

Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Accessing a button's property from another form

Posted 31 May 2012 - 06:23 AM

You can access them if you really want to but it's best not to.

{form_instance name}.{control}.{property}

This post has been edited by AdamSpeight2008: 31 May 2012 - 06:28 AM

Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,940
  • Joined: 02-June 10

Re: Accessing a button's property from another form

Posted 31 May 2012 - 06:39 AM

I really wasn't going to open that can of worms.
There's a reason I said "you don't" not "you can't".

Letting rookies know there is a way out there to do it wrong and make a further mess of things is irresponsible. (Not to mention will promote 50 more screwed up posts based on that code that we then have to deal with)

As students that have 3 weeks into their courses they don't know good from bad, right from wrong. Its all the same to them. We have a certain responsiblity to steer them in the right direction early on. They will find these alternatives on their own if they stay with the course long enough. But us putting it out there stamps it with an 'approved' mark because in their eyes...

Well it must be right, a forum leader told me to do it.


So I'm going to repeat: Don't try to access GUI controls across forms. Its wrong. Nobody will hire you if you do this sort of crap. It violates every guideline for 'black box' programming, Separation of Responsibility, loose binding of components, and event driven programming. Read the tutorials I pointed you to and learn to do it right the first time so you don't develop bad habits that you just have to un-learn later.
Was This Post Helpful? 1
  • +
  • -

#11 She'ne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-August 11

Re: Accessing a button's property from another form

Posted 10 June 2012 - 11:35 PM

gone through the tutorials..learnt how to create a custom event..is there any other way i can exchange info between form1 and form2 without making an instance of form1 or form2? cos i found out i still had to make an instance of my form in the tutorial which i do not want cos it is re-loading it.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9392
  • View blog
  • Posts: 35,271
  • Joined: 12-June 08

Re: Accessing a button's property from another form

Posted 11 June 2012 - 06:14 AM

You need to have at least one instance of one of those forms (as a child) to the other (the parent)...
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3622
  • View blog
  • Posts: 11,290
  • Joined: 05-May 12

Re: Accessing a button's property from another form

Posted 11 June 2012 - 07:00 AM

View PosttlhIn`toq, on 31 May 2012 - 06:39 AM, said:

Letting rookies know there is a way out there to do it wrong and make a further mess of things is irresponsible. (Not to mention will promote 50 more screwed up posts based on that code that we then have to deal with)

As students that have 3 weeks into their courses they don't know good from bad, right from wrong. Its all the same to them. We have a certain responsiblity to steer them in the right direction early on. They will find these alternatives on their own if they stay with the course long enough. But us putting it out there stamps it with an 'approved' mark because in their eyes...


+1. tlhIn'toq has it right on about teaching good practices early on. You may be dealing with their code in the future.

This is an aside, but related about teaching bad practices: I always cringe when I read or hear somebody blithely advice to "just throw in an Application.DoEvents() if it looks frozen". May those individuals be forever cursed to debug somebody else's non-reentrant code that took their advice. So that there is no mistaking: DO NOT USE DoEvents() unless you know exactly what you doing, why, and what potential side effects you will run into.

This post has been edited by Skydiver: 11 June 2012 - 07:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1