Jump to content

C# Winforms questions

Go to solution Solved by cluelessgenius,
Just now, MatazaNZ said:

Thank you. Now to research creating custom events. How would I make a form listen to an event from another from? 

form a

{

public event EventHandler EventTriggered;

 

function a

{

EventTriggered(object,e);

}

}

 

parentform

{

form a = new form a();

form a.EventTriggert += new EventHandler(UpdatingFormB);

 

private void UpdatingFormB(object,e)

{

ForomB.Update();

}

}

 

so something a little like this would do it. im really not into typing right now but if you want you can give me a call on skype and id gladly take a look or give some tips or whatever if you have further questions. honestly im basically offering tutor service for free here :D if my skype isnt in my profile anymore its lowjoe_mobil

I have a question about running multiple windows in a winforms project. My application has a main menu, and four other menus that you can open up simultaneously. These menus are Customers, Purchases, Logs and Jobs. 

 

I have included code that makes sure only one of each of those menu windows are open. While this works perfectly fine as is, and I can manipulate each window while they are all open, I wanted to know about the threading they use. Is it at all redundant for me to run each window in a separate thread? Does .NET make the threads automatically when the window is created and opened? 

 

And my other question is this. Some windows contain information used by other windows as well. For example, the logs window will show logs that are associated with jobs that have been/are being entered. When the content in a certain window (let's say logs) is being added to or edited (all content is held by objects in a list specific to the window) from another window (say, jobs),  how would I go about notifying the logs window to update from the jobs form? I would assume I would need an event handler, but I don't know where to go from there. 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/
Share on other sites

Link to post
Share on other sites

I'm not fluent in C# , I'm on the VB side so I only kind of know what's going on.

First off, I think all the forms and their threads are created when they're created in the beginning of the code (as opposed to when the forms are first shown) and they're only dropped if you ever dispose them. I mean to say, showing/hiding them does nothing to the threads in that regard.

For the second part, in VB if I use, say a textbox in form1 (where the textbox is created) it'll be just textbox1.text=string1 but if I want to access it from form2, I'll have to do form1.textbox1.text=string2 where the variable string1 originates from form1 and string2 from form2.

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7335551
Share on other sites

Link to post
Share on other sites

Threading they use: they'll all be on a single thread right now.

Need to run in different threads?: Are you seeing slowness or hanging in your app? If not, I'd say there's probably no need to refactor just now/yet. Also, there's a nice little post with a warning on this approach from SO... http://stackoverflow.com/questions/7568376/multiple-ui-threads-winforms

New threads when your app forms are created/opened: Nope - by default there'll be 1 thread.

 

In the Windows world, the MVVM pattern is the preferred route when you've got a really complicated or large UI to handle & it will allow you to offload the handling of the log communication as well as the data or state sharing between multiple UI views, away from those individual UI views themselves but instead into more shared classes called models. It could be worth your while, especially later to look into this more on MSDN, Channel9 or MVA too.

 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7335667
Share on other sites

Link to post
Share on other sites

ok what i would do is send the data through an event from window 1 to your main form and from there call a function of form2 that accepts the data.as parameters. you could also give window1 and instance of windows 2 on initializing but that wouldnt be as clean imo.

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7335860
Share on other sites

Link to post
Share on other sites

7 hours ago, cluelessgenius said:

ok what i would do is send the data through an event from window 1 to your main form and from there call a function of form2 that accepts the data.as parameters. you could also give window1 and instance of windows 2 on initializing but that wouldnt be as clean imo.

I'm not passing data between forms, I've managed to make sure the data is held separately from the forms. All I need to do is send an event to form b from form a which triggers form b to update it's view (which I already have an update function that's used internally) 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7337866
Share on other sites

Link to post
Share on other sites

1 minute ago, MatazaNZ said:

I'm not passing data between forms, I've managed to make sure the data is held separately from the forms. All I need to do is send an event to form b from form a which triggers form b to update it's view (which I already have an update function that's used internally) 

same thing. call an event in form a and listen to it in parentform. then on triggering call update function in form b

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7337887
Share on other sites

Link to post
Share on other sites

2 minutes ago, cluelessgenius said:

same thing. call an event in form a and listen to it in parentform. then on triggering call update function in form b

Thank you. Now to research creating custom events. How would I make a form listen to an event from another from? 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7337910
Share on other sites

Link to post
Share on other sites

8 hours ago, alex_read said:

Threading they use: they'll all be on a single thread right now.

Need to run in different threads?: Are you seeing slowness or hanging in your app? If not, I'd say there's probably no need to refactor just now/yet. Also, there's a nice little post with a warning on this approach from SO... http://stackoverflow.com/questions/7568376/multiple-ui-threads-winforms

New threads when your app forms are created/opened: Nope - by default there'll be 1 thread.

 

In the Windows world, the MVVM pattern is the preferred route when you've got a really complicated or large UI to handle & it will allow you to offload the handling of the log communication as well as the data or state sharing between multiple UI views, away from those individual UI views themselves but instead into more shared classes called models. It could be worth your while, especially later to look into this more on MSDN, Channel9 or MVA too.

 

I do know about MVVM, and it's concepts. I have tried to follow this as best I can, but winforms doesn't make it easy. The content for both forms are stored in a separate class with global variables, so all that needs to happen is to get a message from one form to the other to refresh it's content with the edited variable. 

 

And on the threading, do I only need to create multiple threads for each form if it's running slowly in the single thread? 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7337964
Share on other sites

Link to post
Share on other sites

Just now, MatazaNZ said:

Thank you. Now to research creating custom events. How would I make a form listen to an event from another from? 

form a

{

public event EventHandler EventTriggered;

 

function a

{

EventTriggered(object,e);

}

}

 

parentform

{

form a = new form a();

form a.EventTriggert += new EventHandler(UpdatingFormB);

 

private void UpdatingFormB(object,e)

{

ForomB.Update();

}

}

 

so something a little like this would do it. im really not into typing right now but if you want you can give me a call on skype and id gladly take a look or give some tips or whatever if you have further questions. honestly im basically offering tutor service for free here :D if my skype isnt in my profile anymore its lowjoe_mobil

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7337974
Share on other sites

Link to post
Share on other sites

46 minutes ago, cluelessgenius said:

form a

{

public event EventHandler EventTriggered;

 

function a

{

EventTriggered(object,e);

}

}

 

parentform

{

form a = new form a();

form a.EventTriggert += new EventHandler(UpdatingFormB);

 

private void UpdatingFormB(object,e)

{

ForomB.Update();

}

}

 

so something a little like this would do it. im really not into typing right now but if you want you can give me a call on skype and id gladly take a look or give some tips or whatever if you have further questions. honestly im basically offering tutor service for free here :D if my skype isnt in my profile anymore its lowjoe_mobil

Thank you, I think I got it now. I'll add that to the project when I have time

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7338325
Share on other sites

Link to post
Share on other sites

Check this out : DotNet Perls Events

// Gigabyte 990FXA-UD3 // AMD FX-8320 CPU @ 4.3 Ghz (7-21.5 Multiplier) 200.90mhz FSB CPU-Z Validated // Kraken X40 AIO - 2x140mm Push-Pull // 4GB Corsair Vengeance LP - 8GB Avexir Core Series Red 1760Mhz // Sapphire R9 Fury Nitro 1130mhz/4GB 1025mhz (Effective) GPU-Z Validation // Corsair SP2500 2.1 & Microlab Solo 9C Speakers // Corsair K90 Silver - Cherry MX Red & Blue LEDs // EVGA SuperNova 850w G2

Link to comment
https://linustechtips.com/topic/557067-c-winforms-questions/#findComment-7347762
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×