C
C#ā€¢2y ago
Whiteboy

I want on a click of a button in form1 to change a thing in 2nd form, but can't manage [Answered]

So i have 4 forms - Main menu - then 3 subForms referenced to that. In form1 i want to click button so in form2 a picturebox will take an image from form1 and load it there to form2
56 Replies
Binto86
Binto86ā€¢2y ago
you need to pass the image into the constructor of form 2
Whiteboy
Whiteboyā€¢2y ago
using System.Windows.Forms;

namespace SimulatorApk
{
public partial class ItemUpgradeMenu : Form
{
public ItemUpgradeMenu()
{
InitializeComponent();

}

private void ItemUpgradeMenu_Load(object sender, System.EventArgs e)
{
}
}
}
using System.Windows.Forms;

namespace SimulatorApk
{
public partial class ItemUpgradeMenu : Form
{
public ItemUpgradeMenu()
{
InitializeComponent();

}

private void ItemUpgradeMenu_Load(object sender, System.EventArgs e)
{
}
}
}
i don't know where exactly that'd be
Binto86
Binto86ā€¢2y ago
thats the second form right?
Whiteboy
Whiteboyā€¢2y ago
yea
Binto86
Binto86ā€¢2y ago
so u need something like this
using System.Windows.Forms;

namespace SimulatorApk
{
public partial class ItemUpgradeMenu : Form
{
Image imageToDisplay;
public ItemUpgradeMenu(Image img)
{
InitializeComponent();
imageToDisplay = img;
}

private void ItemUpgradeMenu_Load(object sender, System.EventArgs e)
{
myPictureBox.Image=imageToDisplay;
}
}
}
using System.Windows.Forms;

namespace SimulatorApk
{
public partial class ItemUpgradeMenu : Form
{
Image imageToDisplay;
public ItemUpgradeMenu(Image img)
{
InitializeComponent();
imageToDisplay = img;
}

private void ItemUpgradeMenu_Load(object sender, System.EventArgs e)
{
myPictureBox.Image=imageToDisplay;
}
}
}
Whiteboy
Whiteboyā€¢2y ago
and in the 1st form i need this image?
Binto86
Binto86ā€¢2y ago
how do u create the second form in the first one?
Whiteboy
Whiteboyā€¢2y ago
ah i see problem now, it wants to take the image from main menu not from form1
private void OpenChildForm(Form childForm, object btnSender)
{
_activeForm?.Close();

_activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
this.panelDesktop.Controls.Add(childForm);
this.panelDesktop.Tag = childForm;
childForm.BringToFront();
childForm.Show();


}
private void OpenChildForm(Form childForm, object btnSender)
{
_activeForm?.Close();

_activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
this.panelDesktop.Controls.Add(childForm);
this.panelDesktop.Tag = childForm;
childForm.BringToFront();
childForm.Show();


}
Binto86
Binto86ā€¢2y ago
form1 is not the main menu?
Whiteboy
Whiteboyā€¢2y ago
no there is Main menu then 3 forms (ItemSelectionMenu, ItemUpgradeMenu, ItemRarityMenu) main menu is like just background i can show u on stream if it helps
Binto86
Binto86ā€¢2y ago
nah i think i understand so when form 1 is running and u click the button, is form 2 also running?
Whiteboy
Whiteboyā€¢2y ago
i guess
Binto86
Binto86ā€¢2y ago
umm that will be bit harder ok so u want to have form shown event in the second form
Whiteboy
Whiteboyā€¢2y ago
yes
Binto86
Binto86ā€¢2y ago
and there u need something like this:
private async void ItemUpgradeMenu_Shown(object sender, System.EventArgs e)
{
Image img = await GetImage();
}
Image GetImage(){
while(SharedClass.image == null){

}
return SharedClass.image;
}
private async void ItemUpgradeMenu_Shown(object sender, System.EventArgs e)
{
Image img = await GetImage();
}
Image GetImage(){
while(SharedClass.image == null){

}
return SharedClass.image;
}
Than u need SharedClass which is static class with public image property and in the form1 u need something like this:
private void Button_Click(object sender, System.EventArgs e)
{
SharedClass.image = myImageToDisplay;
}
private void Button_Click(object sender, System.EventArgs e)
{
SharedClass.image = myImageToDisplay;
}
Whiteboy
Whiteboyā€¢2y ago
In the form i take image from right?
Binto86
Binto86ā€¢2y ago
yes
Whiteboy
Whiteboyā€¢2y ago
Error CS1061 'Image' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'Image' could be found (are you missing a using directive or an assembly reference?) SimulatorApk C:\Users\Admin\Desktop\Games\SimulatorApk\SimulatorApk\ItemUpgradeMenu.cs 22 Active
Binto86
Binto86ā€¢2y ago
ah yep my bad its async Image GetImage()
Whiteboy
Whiteboyā€¢2y ago
Whiteboy
Whiteboyā€¢2y ago
it made it worse or im dumb
Binto86
Binto86ā€¢2y ago
wron line its the method declaration not the call
Whiteboy
Whiteboyā€¢2y ago
Binto86
Binto86ā€¢2y ago
also you didn't register the shown event ah shit wait its async Task<Image> GetImage() i think
Whiteboy
Whiteboyā€¢2y ago
Whiteboy
Whiteboyā€¢2y ago
it either wants it synchronous
Binto86
Binto86ā€¢2y ago
theres someone dumb here, ither its me or visual studio
Whiteboy
Whiteboyā€¢2y ago
XDDD
Binto86
Binto86ā€¢2y ago
just leave it as it is for now, i think it should work as i want it to
Whiteboy
Whiteboyā€¢2y ago
one more error i think i couldn't fix rn
using System.Drawing;

namespace SimulatorApk
{
static class SharedClass
{
public static Image Image
{
get
{
return Image;
}
}
}
}
using System.Drawing;

namespace SimulatorApk
{
static class SharedClass
{
public static Image Image
{
get
{
return Image;
}
}
}
}
Error CS0200 Property or indexer 'SharedClass.Image' cannot be assigned to -- it is read only SimulatorApk C:\Users\Admin\Desktop\Games\SimulatorApk\SimulatorApk\ItemSelectionMenu.cs 46 Active
Binto86
Binto86ā€¢2y ago
you are missing setter also you can just one line it public static Image image {get;set;}
Whiteboy
Whiteboyā€¢2y ago
and i assign wanted image in shared class on in the form UpgradeMenu?
Binto86
Binto86ā€¢2y ago
?
Whiteboy
Whiteboyā€¢2y ago
private void Button_Click(object sender, System.EventArgs e)
{
SharedClass.image = myImageToDisplay;
}
private void Button_Click(object sender, System.EventArgs e)
{
SharedClass.image = myImageToDisplay;
}
i mean thsi to the button but it doesn work
Binto86
Binto86ā€¢2y ago
what doesn't work? i need more info
Whiteboy
Whiteboyā€¢2y ago
so like this
private async void ItemUpgradeMenu_Shown(object sender, System.EventArgs e)
{
Image img = await GetImage();
}
private async void ItemUpgradeMenu_Shown(object sender, System.EventArgs e)
{
Image img = await GetImage();
}
has 0 ref what do i do with it
Binto86
Binto86ā€¢2y ago
you forgot to register the event
Whiteboy
Whiteboyā€¢2y ago
yea but where? one the upgradeMenu isuppose but there are so many events
Binto86
Binto86ā€¢2y ago
the event is called form shown i think
Whiteboy
Whiteboyā€¢2y ago
Binto86
Binto86ā€¢2y ago
ah its called just Shown
Whiteboy
Whiteboyā€¢2y ago
still not working and i have this error in main menu Error CS7036 There is no argument given that corresponds to the required formal parameter 'img' of 'ItemUpgradeMenu.ItemUpgradeMenu(Image)' SimulatorApk C:\Users\Admin\Desktop\Games\SimulatorApk\SimulatorApk\MyMain.cs 50 Active it looks like it still wants the image from main menu idk šŸ˜¦
Binto86
Binto86ā€¢2y ago
oh yep that was the original idea, just take the original methods you had there
using System.Windows.Forms;

namespace SimulatorApk
{
public partial class ItemUpgradeMenu : Form
{
public ItemUpgradeMenu()
{
InitializeComponent();

}

private void ItemUpgradeMenu_Load(object sender, System.EventArgs e)
{
}
private async void ItemUpgradeMenu_Shown(object sender, System.EventArgs e)
{
Image img = await GetImage();
}
async Task<Image> GetImage(){
while(SharedClass.image == null){

}
return SharedClass.image;
}
}
}
using System.Windows.Forms;

namespace SimulatorApk
{
public partial class ItemUpgradeMenu : Form
{
public ItemUpgradeMenu()
{
InitializeComponent();

}

private void ItemUpgradeMenu_Load(object sender, System.EventArgs e)
{
}
private async void ItemUpgradeMenu_Shown(object sender, System.EventArgs e)
{
Image img = await GetImage();
}
async Task<Image> GetImage(){
while(SharedClass.image == null){

}
return SharedClass.image;
}
}
}
Whiteboy
Whiteboyā€¢2y ago
i mean i didn't change a thing in main menu
Binto86
Binto86ā€¢2y ago
it should look like this now the second form should look like this now
Whiteboy
Whiteboyā€¢2y ago
oh yeah thnak you very much man u saved me a week of googling stuff
Binto86
Binto86ā€¢2y ago
np does it work well now?
Whiteboy
Whiteboyā€¢2y ago
yea!
Binto86
Binto86ā€¢2y ago
nice
Whiteboy
Whiteboyā€¢2y ago
Binto86
Binto86ā€¢2y ago
oh that looks really cool is there github repo for the project?
Whiteboy
Whiteboyā€¢2y ago
not yet i want to make but didn't quite think of it yet idk why xd didn't expect to do this much actually
Binto86
Binto86ā€¢2y ago
tbh visual studio support for github is awsome
Whiteboy
Whiteboyā€¢2y ago
i was using Rider b4 but something fucked up really hard and i chanegd to VS like 2 days ago so VS is giga new for me it's like my 2nd month of programming
Binto86
Binto86ā€¢2y ago
well you learned a lot i guess if you click view there is git changes button and that just gives you full git support inside vs @Whiteboy don't forget to close the thread
Accord
Accordā€¢2y ago
āœ… This post has been marked as answered!