C
C#•3y 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•3y ago
you need to pass the image into the constructor of form 2
Whiteboy
WhiteboyOP•3y 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•3y ago
thats the second form right?
Whiteboy
WhiteboyOP•3y ago
yea
Binto86
Binto86•3y 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
WhiteboyOP•3y ago
and in the 1st form i need this image?
Binto86
Binto86•3y ago
how do u create the second form in the first one?
Whiteboy
WhiteboyOP•3y 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•3y ago
form1 is not the main menu?
Whiteboy
WhiteboyOP•3y 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•3y ago
nah i think i understand so when form 1 is running and u click the button, is form 2 also running?
Whiteboy
WhiteboyOP•3y ago
i guess
Binto86
Binto86•3y ago
umm that will be bit harder ok so u want to have form shown event in the second form
Whiteboy
WhiteboyOP•3y ago
yes
Binto86
Binto86•3y 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
WhiteboyOP•3y ago
In the form i take image from right?
Binto86
Binto86•3y ago
yes
Whiteboy
WhiteboyOP•3y 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•3y ago
ah yep my bad its async Image GetImage()
Whiteboy
WhiteboyOP•3y ago
Whiteboy
WhiteboyOP•3y ago
it made it worse or im dumb
Binto86
Binto86•3y ago
wron line its the method declaration not the call
Whiteboy
WhiteboyOP•3y ago
Binto86
Binto86•3y ago
also you didn't register the shown event ah shit wait its async Task<Image> GetImage() i think
Whiteboy
WhiteboyOP•3y ago
Whiteboy
WhiteboyOP•3y ago
it either wants it synchronous
Binto86
Binto86•3y ago
theres someone dumb here, ither its me or visual studio
Whiteboy
WhiteboyOP•3y ago
XDDD
Binto86
Binto86•3y ago
just leave it as it is for now, i think it should work as i want it to
Whiteboy
WhiteboyOP•3y 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•3y ago
you are missing setter also you can just one line it public static Image image {get;set;}
Whiteboy
WhiteboyOP•3y ago
and i assign wanted image in shared class on in the form UpgradeMenu?
Binto86
Binto86•3y ago
?
Whiteboy
WhiteboyOP•3y 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•3y ago
what doesn't work? i need more info
Whiteboy
WhiteboyOP•3y 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•3y ago
you forgot to register the event
Whiteboy
WhiteboyOP•3y ago
yea but where? one the upgradeMenu isuppose but there are so many events
Binto86
Binto86•3y ago
the event is called form shown i think
Whiteboy
WhiteboyOP•3y ago
Binto86
Binto86•3y ago
ah its called just Shown
Whiteboy
WhiteboyOP•3y 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•3y 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
WhiteboyOP•3y ago
i mean i didn't change a thing in main menu
Binto86
Binto86•3y ago
it should look like this now the second form should look like this now
Whiteboy
WhiteboyOP•3y ago
oh yeah thnak you very much man u saved me a week of googling stuff
Binto86
Binto86•3y ago
np does it work well now?
Whiteboy
WhiteboyOP•3y ago
yea!
Binto86
Binto86•3y ago
nice
Whiteboy
WhiteboyOP•3y ago
Binto86
Binto86•3y ago
oh that looks really cool is there github repo for the project?
Whiteboy
WhiteboyOP•3y 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•3y ago
tbh visual studio support for github is awsome
Whiteboy
WhiteboyOP•3y 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•3y 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•3y ago
āœ… This post has been marked as answered!

Did you find this page helpful?