Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:20:57+00:00 2026-06-14T04:20:57+00:00

Summary: I do not know how to set the main window dimensions so that

  • 0

Summary: I do not know how to set the main window dimensions so that no scroll bars are used around the wxGrid without changing the default widths of the columns.

I want to write a simple application where user can see a limited number of numbers, and she can make correction of some of them and save the new content. The application should be a kind of minimalistic. The number of rows and cells is small enough to fit in a window without the need for scroll bars. Here is a simplified code:

wxGrid *grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
grid->CreateGrid(21, 12);

// Setting the column labels and filling the grid with some numbers.

grid->Fit();

There will be some button below the grid. For now, the only sizer contains the only controll — the grid:

wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
topSizer->Add(grid, 1, wxEXPAND);
SetSizer(topSizer);

The code is placed into the constructor of my AppFrame class. Finally, I call the following:

topSizer->Fit(this);
topSizer->SetSizeHints(this);

It does almost exactly what I need, but some columns have longer labels and the Fit changes the widths of the columns. It is not nice visually. What should I call instead of Fit to get the same effect but without changing the uniform column widths?

Update: Actually, I did not noticed earlier, that also grid->Fit(); was called in my code (added now to the above example). After removing it, the grid does not set internally its dimensions and the window is extremely small (even the upper left corner window is not fully visible in height and ony a bit more that that on width. Then I have added also SetColSize for each column (to the same place where I set the format for the columns):

for (int i = 0; i < 12; ++i)
{
    grid->SetColSize(i, WXGRID_DEFAULT_COL_WIDTH);
    grid->SetColFormatFloat(i, -1, 3);
}

This did not help and the window is smaller than wanted. So, question should probably be reworded: How the filled grid can capture its own dimensions to be reported to the outer wrapper?

Update 2: It still does not work. The full code of the constructor. (I know, somewhere between the chair and the keyboard… 🙂 I personally consider i a bit dirty code (at least for the magic numbers) — possibly cleaned up later:

AppFrame::AppFrame() :
    wxFrame(NULL, wxID_ANY, "Month rates of Euro")
{
    InitDefaultRates();

    wxGrid *grid = new wxGrid(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);

    // Fixed grid for 12 months (columns) and the years 2000 to 2020 (rows).
    //
    grid->CreateGrid(21, 12);

    grid->BeginBatch();

    // Set the column labes (in Czech -- to lazy to translate).
    //
    grid->SetColLabelValue(0,  "leden");
    grid->SetColLabelValue(1,  "únor");
    grid->SetColLabelValue(2,  "březen");
    grid->SetColLabelValue(3,  "duben");
    grid->SetColLabelValue(4,  "květen");
    grid->SetColLabelValue(5,  "červen");
    grid->SetColLabelValue(6,  "červenec");
    grid->SetColLabelValue(7,  "srpen");
    grid->SetColLabelValue(8,  "září");
    grid->SetColLabelValue(9,  "říjen");
    grid->SetColLabelValue(10, "listopad");
    grid->SetColLabelValue(11, "prosinec");

    // Float with 3 decimal places for each of the columns.
    //
    for (int i = 0; i < 12; ++i)
    {
        grid->SetColFormatFloat(i, -1, 3);
    }

    // Filling with the default values calculated from 
    // http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist.zip.
    // Index zero is for the year 2000.
    //
    for (int year = 0; year < 21; ++year)
    {
        ostringstream oss;
        oss << 2000 + year;
        grid->SetRowLabelValue(year, oss.str().c_str());

        for (int month = 0; month < 12; ++month)
        {
            double default_value = default_rates[year][month];
            double value = default_value;

            oss.str("");
            oss << setprecision(3) << fixed << value;

            // Problem with decimal point separator. Replace the locale
            // comma by dot.
            //
            wxString s(oss.str().c_str());
            string::size_type pos = s.find_first_of('.');
            if (pos != string::npos)
                s[pos] = ',';

            grid->SetCellValue(year, month, s);

            if (value == 0.0)
                grid->SetCellTextColour(year, month, *wxLIGHT_GREY);
            else if (value == default_value)
                grid->SetCellTextColour(year, month, *wxBLUE);
        }
    }

    // Setting the initial size of the grid.
    //
    grid->SetInitialSize(grid->GetBestSize());

    // Vertical size will be the top one.
    //
    wxBoxSizer *topSizer = new wxBoxSizer(wxVERTICAL);
    topSizer->Add(grid, 1, wxEXPAND);
    SetSizer(topSizer);

    // I do not understand this (could be a problem). From wxBook p. 196.
    //
    topSizer->Fit(this);
    topSizer->SetSizeHints(this);

    // Allow redraw.
    //
    grid->EndBatch();

    // Set cursor to the current month.
    //
    int year = wxDateTime::GetCurrentYear() - 2000;
    int month = wxDateTime::GetCurrentMonth();
    grid->GoToCell(year, month);
}

Thanks for your time and experience,
Petr

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:20:58+00:00Added an answer on June 14, 2026 at 4:20 am

    Here is a complete working program:

    #include "wx/app.h"
    #include "wx/frame.h"
    #include "wx/grid.h"
    #include "wx/datetime.h"
    
    class MyFrame : public wxFrame
    {
    public:
        MyFrame()
            : wxFrame(NULL, wxID_ANY, "Chronological data")
        {
            wxGrid *grid = new wxGrid(this, wxID_ANY);
            grid->CreateGrid(21, 12);
    
            for ( wxDateTime::Month m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) )
            {
                grid->SetColLabelValue(m, wxDateTime::GetMonthName(m));
                for ( int y = 0; y < 21; y++ )
                grid->SetCellValue(y, m, wxString::Format("%.2f", (float)rand() / RAND_MAX));
            }
    
            grid->InvalidateBestSize();
            SetClientSize(grid->GetBestSize());
        }
    };
    
    class MyApp : public wxApp
    {
    public:
        virtual bool OnInit()
        {
            (new MyFrame)->Show();
            return true;
        }
    };
    
    wxIMPLEMENT_APP(MyApp);
    

    Notice that for me “September” doesn’t fit into the default column width, either call AutoSizeColumns() (but you don’t like this) or just increase the column width uniformly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that the linkedlist is not threadsafe and at work I ve been
Summary I want to create a product attribute that is not saved to products,
Summary The Faces Config Editor in Eclipse does not open when editing faces-config.xml. This
In summary, method with @PostConstruct is not call by JSF on WebLogic12c, on managed
In theory, the set of Request For Comments (RFC) contain everything that a developer
In theory, the set of Request For Comments (RFC) contain everything that a developer
I have a cron job that isn't running. I'm not terribly experienced in this
I have three tables in MySQL that are related, yet not technically linked to
Our facebook page is saying: Error Summary HTTP Error 405.0 - Method Not Allowed
How can I add a new object item to a Main Views model that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.