Skip to content

Instantly share code, notes, and snippets.

@Viklor
Last active October 6, 2020 20:49
Show Gist options
  • Save Viklor/8981f48902d0f1854857fb69605becf8 to your computer and use it in GitHub Desktop.
Save Viklor/8981f48902d0f1854857fb69605becf8 to your computer and use it in GitHub Desktop.
Rectangle ratio calculator
using System;
using System.Text;
using NGettext;
using R7.Webmate.Xwt.Icons;
using R7.Webmate.Xwt.Text;
using Xwt;
namespace R7.Webmate.Xwt
{
public class HelloWorldWidget : Widget
{
protected ICatalog T = TextCatalogKeeper.GetDefault();
protected TextEntry txtWidth;
protected TextEntry txtHeight;
protected TextEntry txtCalculed;
public HelloWorldWidget()
{
var vbox = new VBox();
var lblWidth = new Label(T.GetString("Ширина:"));
var lblHeight = new Label(T.GetString("Высота:"));
var btnCalculed = new Button(T.GetString("Calculate"));
txtWidth = new TextEntry();
txtHeight = new TextEntry();
txtCalculed = new TextEntry();
btnCalculed.Clicked += btnCalculed_Clicked;
vbox.PackStart(lblWidth, false, true);
vbox.PackStart(txtWidth, false, true);
vbox.PackStart(lblHeight, false, true);
vbox.PackStart(txtHeight, false, true);
vbox.PackStart(btnCalculed, false, true);
vbox.PackStart(txtCalculed, false, true);
vbox.Margin = Const.VBOX_MARGIN;
Content = vbox;
Content.Show();
}
static int GCD(int a, int b)
{
while (b != 0)
{
var t = b;
b = a % b;
a = t;
}
return a;
}
void btnCalculed_Clicked(object sender, EventArgs e)
{
var w = int.Parse(txtWidth.Text);
var h = int.Parse(txtHeight.Text);
var a = w;
var b = h;
var r = (w / GCD(a, b) + ":" + h / GCD(a, b)) ;
txtCalculed.Text = r.ToString();
}
}
}
@roman-yagodin
Copy link

Rename the file to HelloWorldWidget.cs

@roman-yagodin
Copy link

var w = int.Parse (txtWidth.Text);
var h = int.Parse (txtHeight.Text);
var r = (float) w / h;
txtCalculed.Text = r.ToString ();

@Viklor
Copy link
Author

Viklor commented Oct 2, 2020

namespace R7.Webmate.Xwt
{
public class HelloWorldWidget : Widget
{
protected ICatalog T = TextCatalogKeeper.GetDefault ();

    protected TextEntry txtWidth;

    protected TextEntry txtHeight;

    protected TextEntry txtCalculed;

    public HelloWorldWidget()
    {
        var vbox = new VBox ();
        var lblWidth = new Label (T.GetString ("Ширина:"));

        var lblHeight = new Label(T.GetString("Высота:"));

        var btnCalculed = new Button (T.GetString ("Click me!"));

        txtWidth = new TextEntry  ();
        txtHeight = new TextEntry();
        txtCalculed = new TextEntry ();

        var w = int.Parse(txtWidth.Text);
        var h = int.Parse(txtHeight.Text);
        var r = (float) w / h;

        btnCalculed.Clicked += btnCalculed_Clicked;

        vbox.PackStart (lblWidth, false, true);
        vbox.PackStart (txtWidth, false, true);
        vbox.PackStart(lblHeight, false, true);
        vbox.PackStart(txtHeight, false, true);
        vbox.PackStart (btnCalculed, false, true);
        vbox.PackStart (txtCalculed, false, true);

        vbox.Margin = Const.VBOX_MARGIN;
        Content = vbox;
        Content.Show ();
    }

    void btnCalculed_Clicked(object sender, EventArgs e)
    {
        txtCalculed.Text = r.ToString ();
    }
}

}
image

@Viklor
Copy link
Author

Viklor commented Oct 2, 2020

Basic error: "Имя "r" не существует в данном контексте". Because of this, the program cannot start.

@roman-yagodin
Copy link

roman-yagodin commented Oct 2, 2020

var w = int.Parse (txtWidth.Text);
var h = int.Parse (txtHeight.Text);
var r = (float) w / h;
txtCalculed.Text = r.ToString ();

А первые три строчки? Видимо, невидимые чернила...

@roman-yagodin
Copy link

roman-yagodin commented Oct 2, 2020

И не нужно дублировать весь код в комментариях - обновляете сам gist, там есть история версий (revisions) - см. мой форк.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment