Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Insert Button into GridCtrl Cell (Can you insert buttons (or any widgets) into gridCtrl?)
Insert Button into GridCtrl Cell [message #59699] Thu, 09 March 2023 20:30 Go to next message
jkastran is currently offline  jkastran
Messages: 10
Registered: September 2021
Location: Michigan, US
Promising Member
Hello,

I was wondering if it is possible to insert buttons into a GridCtrl

I have seen the option select example for ArrayCtrl, but I dont see anything for buttons, and I dont see any examples for inserting widgets into GridCtrl
  • Attachment: Grid.PNG
    (Size: 10.61KB, Downloaded 48 times)
Re: Insert Button into GridCtrl Cell [message #59700 is a reply to message #59699] Thu, 09 March 2023 21:09 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1096
Registered: August 2007
Senior Contributor
Hi,

For ArrayCtrl, you can add buttons and other ctrls. In fact, you can add different ctrls for each row of a given column. All you need to do is use the factory methods and setup a callback:

#include <CtrlLib/CtrlLib.h>

using namespace Upp;

struct MyApp : TopWindow {
	ArrayCtrl list;

	MyApp()
	{
		Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 1024, 800);
		Add(list.SizePos());
		auto sButtonFactory = [this] (int i, One<Ctrl>& c)
		{
			String s = list.Get(i, 0);
			c.Create<Button>()
				.SetLabel(s)
				.WhenAction = [s] { PromptOK("The value is " + s); };
		};
		
		list.AddColumn("texts");
		list.AddColumn("buttons").Ctrls(sButtonFactory);
		for(int i = 0; i < 100; i++)
			list.Add(AsString(100 - i), AsString(i));
		
	}
};

GUI_APP_MAIN
{
	MyApp().Run();
}



Same thing can be done with GridCtrl...

Best regards,
Oblivion


[Updated on: Thu, 09 March 2023 21:39]

Report message to a moderator

Re: Insert Button into GridCtrl Cell [message #59701 is a reply to message #59699] Thu, 09 March 2023 21:18 Go to previous messageGo to next message
cgokdemir is currently offline  cgokdemir
Messages: 12
Registered: February 2023
Location: TURKEY
Promising Member
Hi,

It's in the GridCtrlTest example, maybe you'd like to examine it.

void MakeEdit(One<Ctrl>& ctrl)
{
ctrl.Create<EditInt>();
ctrl->WantFocus();
}

//
grid.GetColumn(3).Ctrls(MakeEdit);

Kind regards


Windows 10 Home 64bit
Re: Insert Button into GridCtrl Cell [message #59702 is a reply to message #59701] Thu, 09 March 2023 22:08 Go to previous messageGo to next message
jkastran is currently offline  jkastran
Messages: 10
Registered: September 2021
Location: Michigan, US
Promising Member
Hi, I tried this already but because of the errors and that it was in the ArrayCtrlTest File even though the demo is for GridCtrl, I thought that it is not for GridCtrl. Here is the code and the error I am getting

EDIT:
I moved the factory before where I call the creation of the column and it works.

Now the problem is there is no explanation on how to change label or add functionality to individual buttons with the "MakeButton" method
  • Attachment: GridErr.PNG
    (Size: 502.21KB, Downloaded 54 times)

[Updated on: Thu, 09 March 2023 22:30]

Report message to a moderator

Re: Insert Button into GridCtrl Cell [message #59703 is a reply to message #59702] Thu, 09 March 2023 23:40 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1096
Registered: August 2007
Senior Contributor
Here's the thing: GridCtrl still uses pre-C++11 callbacks. (Deprecated API)
It should be updated to utilize the new Upp::Function variants and lambda functions with capture...

Good news is that you can achieve -mostly- the same results with the old api calls:

#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>

using namespace Upp;

void ButtonFactory1(One<Ctrl>& c, GridCtrl* list)
{
	static int i = 0;
	String s = list->Get(i++, 0);
	c.Create<Button>()
		.SetLabel(s)
		.WhenAction = [s] { PromptOK("The value is " + s); };
}

struct MyApp : TopWindow {
	GridCtrl  list;
	typedef MyApp CLASSNAME;
	MyApp()
	{
		Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 1024, 800);
		Add(list.SizePos());
		list.AddColumn("texts");
		list.AddColumn("buttons_1").Ctrls(callback1(ButtonFactory1, &list));
		list.AddColumn("buttons_2").Ctrls(THISBACK(ButtonFactory2));
		for(int i = 0; i < 100; i++)
			list.Add(AsString(i), AsString(i));
	
		
	}

	void ButtonFactory2(One<Ctrl>& c)
	{
		static int i = 0;
		String s = list.Get(i++, 0);
		c.Create<Button>()
			.SetLabel(s)
			.WhenAction = [s] { PromptOK("The value is " + s); };
	}

};

GUI_APP_MAIN
{
	MyApp().Run();
}






Best regards,
Oblivion


[Updated on: Thu, 09 March 2023 23:42]

Report message to a moderator

Re: Insert Button into GridCtrl Cell [message #59704 is a reply to message #59702] Fri, 10 March 2023 00:10 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 1096
Registered: August 2007
Senior Contributor
Quote:
Now the problem is there is no explanation on how to change label or add functionality to individual buttons with the "MakeButton" method


There are several ways, a simple one:

#include <CtrlLib/CtrlLib.h>
#include <GridCtrl/GridCtrl.h>

using namespace Upp;

struct MyButton : Button {
	Value val;
	MyButton() : Button()
	{
		WhenAction = [this]
		{
			PromptOK("The value of the button is " + AsString(val));
		};
	}
	void SetData(const Value& v) override
	{
		SetLabel(AsString(val = v));
	}
	
	Value GetData() const override
	{
		return val;
	}
};

struct MyApp : TopWindow {
	GridCtrl  list;
	MyApp()
	{
		Sizeable().Zoomable().CenterScreen().SetRect(0, 0, 1024, 800);
		Add(list.SizePos());
		list.AddColumn("texts");
		list.AddColumn("buttons").Ctrls<MyButton>();
		for(int i = 0; i < 100; i++)
			list.Add(AsString(i), i * 1000);
	
		
	}
};

GUI_APP_MAIN
{
	MyApp().Run();
}




Best regards,
Oblivion


[Updated on: Fri, 10 March 2023 00:26]

Report message to a moderator

Previous Topic: Choose valid characters for EditString/EditField
Next Topic: Website build
Goto Forum:
  


Current Time: Sat Jun 22 15:33:13 CEST 2024

Total time taken to generate the page: 0.01592 seconds