I want to animate buttons in c#. So i create a method called HoverMouseBut() as follows.
public void HoverMouseBut(Button name,Image img, int w, int h)
{
name.Image = Resources.img;
name.ForeColor = Color.FromArgb(232, 126, 4);
name.Size = new Size(w, h);
name.Font = new Font("Segoe UI", 12, FontStyle.Regular);
name.TextAlign = ContentAlignment.BottomCenter;
name.ImageAlign = ContentAlignment.MiddleCenter;
}
I called this method as follows;
private void addo_MouseHover(object sender, EventArgs e)
{
HoverMouseBut(addo,Add2,250,160);
}
This gave me a syntax error. I changed the method without image parameter and see.Then it worked.But I want to change the image of the button as well.How could I do this? Whats wrong with my code?
*Add2 is an image in resources .
You are using C# and forms are like other properties in C#. Therefore you have different options to do that.
One of the easiest ones is to have a public property of your needed type in your second form and use that to pass data
class Form2
{
public string myStringField{get;set;}
}
class Form1
{
myMethod()
{
....
var newForm= new Form2();
newForm.myStringField="Something";
}
}
However, if your Form2 can not exist without your field, it make sense to put your filed in the constructor
class Form2
{
public Form2 (string myStringField)
{...}
}
class Form1
{
myMethod()
{
....
var newForm= new Form2("Something");
}
}
If you are trying to pass an Image to the method for use, why did you set name.Image = Resources.img ?
Changing it to name.Image = img might work
Related
having a bit of a problem changing the label text when a button is pressed. my code looks like this. i searched a bit in this forum and tried this solution in form1 i have this code.
public string info
{
get
{
return label11.Text;
}
set
{
label11.Text = value;
}
}
in class search i have this
public void fighting()
{
character tom = new character();
Form1 f = new Form1();
Random explore = new Random();
int exploration = explore.Next(0, 3);
if (character.location == "Forest" || character.location == "Dungeon")
{
switch (exploration)
{
case 0:
f.info = "You didnt find anything";
f.Refresh();
f.herodamage = exploration.ToString();
break;
...
the button has this
public void button8_Click(object sender, EventArgs e)
{
Search find = new Search();
find.fighting();
}
what am i doing wrong? everytime i press the button the text wont change but it works because it does change a buttons text.
Fighting method is creating a new instance of Form1, different from your UI.
While it is updating label1.Text, it refers to different Form1.
My suggestion is for refactoring of your code to make responsibilities more clear.
Something like
var info = find.fighting(); // Where fighting returns string, instead of creating new Form1 and setting value, just return the value.
this.info = info;
If you still insist to update UI from search method, which is not correct way to approach this problem, you can pass around Form1 from the parameter.
Search find = new Search(this); // Store it as instance of Search class.
find.fighting(); // fighting should never create new instance of Form1 (reason of problem you are facing)
Instead of creating a new instance of Form1 you need to get a reference to the already existing instance of this form.
One simple way to do this is to use the Application.OpenForms property:
character tom = new character();
Form1 f = Application.OpenForms.OfType<Form1>().FirstOrDefault();
Random explore = new Random();
...
I am having trouble doing something that I would have though to have been quite simple, all I am trying to do is to call a method from another class, heres how I am calling the method:
Gimjes_2D_Game_Framework1.Characters.Character_One.Create();
and here is the contents of the method I am trying to call:
public static void Create()
{
Form1 f = new Form1();
System.Windows.Forms.PictureBox s = new System.Windows.Forms.PictureBox();
//location of image (in thia case it is from resources):
s.BackgroundImage = Gimjes_2D_Game_Framework1.Properties.Resources.DefaultSprite;
//Set to height and width of image:
s.Height = 64;
s.Width = 64;
s.Size = new System.Drawing.Size(60, 60);
s.Location = new System.Drawing.Point(50, 50);
f.Controls.Add(s);
}
Try adding
f.Show();
or
f.ShowDialog();
to the end of your Create method.
Otherwise, you're making a form, and never displaying it.
Show gives you a modeless form, and ShowDialog gives you a modal dialog.
See documentation here for more information.
If you intend to return a Form1 object, to be shown at a later point, you need to change your method to:
public static Form1 Create()
{
Form1 f = new Form1();
...
return f;
}
I created a class and I want to use the constructor of the class Rtb(),
public class Rtb
{
public RichTextBox newRTB;
public Rtb()
{
newRTB = new RichTextBox();
newRTB.IsReadOnly = true;
newRTB.MouseDoubleClick += new MouseButtonEventHandler(newRTB_MouseDoubleClick);
}
private void newRTB_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
.....
}
}
In below code, i created an object of Rtb(),but this object can not assigned to newBUC.Child, an error after run: Cannot implicitly convert type 'WpfApplication1.Rtb' to 'System.Windows.UIElement'
private void menu_Click(object sender, RoutedEventArgs e)
{
BlockUIContainer newBUC = new BlockUIContainer();
newBUC.Margin = new Thickness(50, 10, 50, 10);
mainMenu.Document.Blocks.Add(newBUC);
Rtb newnew = new Rtb();
newBUC.Child = newnew;
}
I tried to use to cast it, and use "as", like below, but it did not work. I think probably i need the right type to perform the assignment, how should i do?
newBUC.Child = newnew as BlockUIContainer;
newBUC.Child = (BlockUIContainer) newnew;
You can't add newnew as a Child, because you class does not inherit from UIElement. But what can you do is set Child to underlying RichTextBox called newRTB which inherits from UIElement
newBUC.Child = newnew.newRTB;
In my case I was missing a reference to PresentationFramework.dll
located on Program files* x86( reference assembles„Microsoft\FRamewrk\3.0\
or your FRamework version
This got me access to all dependencies on my XAML Partial class and therefore able to resolve
I hope this also can help on your coding, It worked for me.
I have a problem in passing a string value between two forms.
First I created public string sanacode which I assign the value passed in form 2
Form 1 code
AnalysisEdit ae = new AnalysisEdit();
int Row = dataGridView1.CurrentRow.Index;
ae.sanacode = dataGridView1[0, Row].Value.ToString();
ae.Show();
Form 2 constructor code
public AnalysisEdit()
{
InitializeComponent();
MessageBox.Show(sanacode,);
}
it shows me nothing
Change you constructor from
public AnalysisEdit()
{
InitializeComponent();
MessageBox.Show(sanacode);
}
to
public AnalysisEdit(string sanacode)
{
InitializeComponent();
MessageBox.Show(sanacode);
}
form call
int Row = dataGridView1.CurrentRow.Index;
AnalysisEdit ae = new AnalysisEdit(dataGridView1[0, Row].Value.ToString());
ae.Show();
The issue is that you're not calling things in the correct order. The form 2 constructor code will be called on line 1 of the form code, or AnalysisEdit ae = new AnalysisEdit(); However, this is before the assignment that takes place on line 3: ae.sanacode = dataGridView1[0, Row].Value.ToString(); So when you show the messagebox in the form 2 constructor, sanacode has not yet been assigned to.
There are two ways to fix this. Firstly, you can pass the value in via the constructor as per #kostas ch.'s answer, or you can override the form's OnShown event in form 2:
protected override void OnShown(EventArgs e)
{
MessageBox.Show(sanacode);
}
I wouldn't put your code
MessageBox.Show(sanacode,);
in the constructor. I would use the "Load"-Event.
If you use the "Load"-Event your MessageBox will Show, when you use
ae.Show();
Like this
private void AnalysisEdit_Load(object sender, EventArgs e)
{
MessageBox.Show(sanacode);
}
Perhaps you'd like some ideas for the various ways to pass data between forms. I have written two blog posts on the subject:
http://geek-goddess-bonnie.blogspot.com/2011/01/passing-data-between-forms.html
http://geek-goddess-bonnie.blogspot.com/2012/12/passing-data-between-forms-redux_31.html
You could temporarily add MessageBox.Show() to your setter:
// In Form2
public sanacode
{
set
{
_sanacode = value;
MessageBox.Show(_sanacode);
}
get
{
return _sanacode;
}
}
Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers