C
C#3y ago
Whiteboy

How do i pass data from one form to another?

eg. i want to click a button in form1 then be able to form2 and see the image of picturebox changed to the one i've chosen
18 Replies
RubyNovaDev
RubyNovaDev3y ago
assuming Form1 knows about Form2 that's easy just set a property on the instance of Form2 from Form1 or pass it in via a method
Whiteboy
WhiteboyOP3y ago
it's like i have one main form like main menu then 3 forms which have reference of Mainmenu and i want the data from form1 to form2 not from main menu D:
Buddy
Buddy3y ago
Keep the data unaware of the forms, that way you have more control over it.
RubyNovaDev
RubyNovaDev3y ago
ehhh
Buddy
Buddy3y ago
meaning, don't store the data within the forms
RubyNovaDev
RubyNovaDev3y ago
I don't see how this changes anything I just said you can just pass it along like I assume there's some relationship here that'll allow you to pass the information around so fundamentally its just a case of adding APIs that let you do that
Whiteboy
WhiteboyOP3y ago
im too green for this i guess
Whiteboy
WhiteboyOP3y ago
RubyNovaDev
RubyNovaDev3y ago
nah you're just overthinking it
Whiteboy
WhiteboyOP3y ago
Whiteboy
WhiteboyOP3y ago
and there is the picture box i want to take image from form1 there when i click that button
RubyNovaDev
RubyNovaDev3y ago
how are you getting from one form to the other can't read code in images so you're gonna have to help
Whiteboy
WhiteboyOP3y ago
i click these buttons on the left
RubyNovaDev
RubyNovaDev3y ago
in code where
Whiteboy
WhiteboyOP3y ago
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();
}
RubyNovaDev
RubyNovaDev3y ago
so you have the child form you already change stuff on it when the button is clicked from what you're showing me here just give it the image as well? like this looks to only take the base type so obv you're gonna need to do it at a point where you know what form it is you're working with but the gist is the same literally you can just be like
ItemUpgradeMenu menu = new();
menu.PictureOfItem = MyPictureBox.Whatever;
ItemUpgradeMenu menu = new();
menu.PictureOfItem = MyPictureBox.Whatever;
and you done you just need to expose something on your item upgrade menu for taking the picture and then pass it along
Whiteboy
WhiteboyOP3y ago
okay i'll try this
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?