mjj
add image programmatically
try
{
Image image = new Image();
BitmapImage bitmap = new BitmapImage(new Uri(imagePath, UriKind.Relative));
image.Source = bitmap;
overlayWindow.Children.Add(image);
}
catch (Exception ex)
{
Console.WriteLine("Error loading image: " + ex.Message);
}
2 replies
✅ How to Update the original instance of my Main Form from another form
he issue you're facing is because you are creating a new instance of Form1 (MainForm) in your second form (SecondForm) when you call Form1 f1 = new Form1();. This new instance is separate from the original instance of MainForm that was created when your application started, and therefore updating the list box on the new instance doesn't affect the original one.
To update the list box on the original instance of MainForm from SecondForm, you should pass a reference to the original MainForm to SecondForm. One way to do this is to modify the constructor of SecondForm to accept a reference to MainForm and store it in a field. Then, you can call the showMacro method on this reference.
Here's an example modification to your SecondForm: public partial class SecondForm : Form
{
private Form1 mainFormReference; // Field to store reference to MainForm
public SecondForm(Form1 mainForm)
{
InitializeComponent();
mainFormReference = mainForm; // Store the reference
}
private void btnOkayClick(object sender, EventArgs e)
{
try
{
File.WriteAllText(Path.Combine(Form1.macroPath, txbNameInput.Text));
mainFormReference.showMacro(); // Call showMacro on the original MainForm
this.Close();
}
catch (Exception ex)
{
MessageBox.Show("This is not a valid Name! " + ex.Message);
return;
}
}
}
__ And when you create an instance of SecondForm from MainForm, pass this (reference to the current instance of MainForm) to the constructor: SecondForm secondForm = new SecondForm(this);
secondForm.Show(); SecondForm secondForm = new SecondForm(this);
secondForm.Show(); |By doing this, you're working with the original instance of MainForm, and changes made in SecondForm will reflect on it.
8 replies
How would i write this in sfml
#include <SFML/Graphics.hpp>
class Disappear : public sf::RectangleShape {
public:
Disappear() {
setFillColor(sf::Color::Green);
}
void update(float deltaTime, sf::RectangleShape& player) {
if (getGlobalBounds().intersects(player.getGlobalBounds())) {
setFillColor(sf::Color::Red);
// Check if player stands on platform for 3 seconds
standingTime += deltaTime;
if (standingTime >= 3.0f) {
// Destroy the platform (remove it from the game)
destroy();
}
} else {
// Reset standing time if not on the platform
standingTime = 0.0f;
setFillColor(sf::Color::Green);
}
}
void destroy() {
// Your logic to destroy or remove the platform goes here
// For example: removeFromGame();
}
private:
float standingTime = 0.0f;
};
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Disappearing Platform");
sf::RectangleShape player(sf::Vector2f(50, 50));
player.setFillColor(sf::Color::Blue);
player.setPosition(375, 500);
Disappear platform;
platform.setSize(sf::Vector2f(200, 20));
platform.setPosition(300, 400);
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
float deltaTime = clock.restart().asSeconds();
// Update the platform and check for player collisions
platform.update(deltaTime, player);
window.clear();
window.draw(player);
window.draw(platform);
window.display();
}
return 0;
}
8 replies