Question

I've been messing around with Python and IronPython this week and I've stumbled across an extremely annoying oddity.

Basically I am connecting to a MSSQL database and upon success setting self.label1.Text = "Connected to " + sqlConn.Database + " on " + sqlConn.DataSource, however my label is updating to just say "Connected to" on the form. I placed a MessageBox.Show(self.label1.Text) after and this displays all the correct information ("Connected to DATABASE on DATASOURCE"). My question is, why isn't IronPython setting my label text correctly on the form?

enter image description here

class MyForm(Form):
    def __init__(self):
        self.button1 = Button()
        self.button1.Text = "Click Me!"
        self.button1.Click += self.button1_Click
        self.Controls.Add(self.button1)

        self.label1 = Label()
        self.label1.Location = Point(10, 50)
        self.Controls.Add(self.label1)

    def button1_Click(self, sender, args):
        sqlConn = self.connectSql()
        if sqlConn.State == ConnectionState.Open:
            self.label1.Text = "Connected to " + sqlConn.Database + " on " + sqlConn.DataSource
            MessageBox.Show(self.label1.Text)
        else:
            self.label1.Text = "Failed connection"

    def connectSql(self):
        sqlConn = SqlClient.SqlConnection("Data Source=(local);Initial Catalog=IT_Project;Integrated Security=True;")
        try:
            sqlConn.Open()
        except System.Exception as ex:
            MessageBox.Show("Error!\r\n" + ex.Message, "EXCEPTION HANDLED")
        return sqlConn

Thanks in advance. :)

Was it helpful?

Solution

The content of the Label is truncated because of its default size and behavior. By manually setting a sufficient size or using the AutoSize-Property the full content can be displayed. The following code could be used after creating the label to size the label to its content:

self.label1.AutoSize = True

The fact that the message box displayed the full value from the actual control's text hinted to the underlying cause.

Note that according to the documentation the auto sizing default behavior may vary:

When added to a form using the designer, the default value is true. When instantiated from code, the default value is false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top