Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

What I want to do is pass two variables from a button click event to another class in the same file.

Here is my code:

Settings.cs (Windows Form file)

namespace ShovelShovel

public partial class Settings : Form
{
    public Settings()
    {
        InitializeComponent();
    }

    public void button1_Click(object sender, EventArgs e)
    {
        SetWindowSize.SaveData(textBoxWidth.Text, textBoxHeight.Text);
    }
}
}
}

SetWindowSize.cs (class file)

namespace ShovelShovel

class SetWindowSize
{
    public static void SaveData(string width, string height)
    {          
        using (BinaryWriter binaryWriter = new BinaryWriter(File.Open("file.dat", FileMode.Create)))
        {
                binaryWriter.Write(width, height);
        }
    }
}
}

I want Settings.width and Settings.height in SetWindowSize.cs to get the text from textBoxWidth and textBoxHeight.

I can't change

public void button1_Click(object sender, EventArgs e)

to anything else, since it would break the function of the form, so I don't know what to do.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

Add new method to SetWindowSize class and call it from button1_Click

public static class SetWindowSize
{
    public static void SaveData(string width, string height)
    {
        File.WriteAllText("file.dat", string.Format("height: {0}, width: {1}.", height, width));
    }
}    

And button click

public void button1_Click(object sender, EventArgs e)
{
    SetWindowSize.SaveData(textBoxWidth.Text, textBoxHeight.Text);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...