I am quite new in C# programming. I would need your help in a question about creating own TextBox class (inheriting from the original one) in C#.
How could I create/inherit my own class from the generic TextBox class? My goal would be, that once I create 1 object from my class, the following attributes should be set automatically to all of the instances:
AutocompleteMode
AutocompleteSource
AutocompleteCustomSource
I would avoid to reuse my coding part every time, once I want to use the same text box, but in different forms and/or Tabs.
Thanks in advance,
Matyas
public class MyTextBox : TextBox
{
public MyTextBox()
{
// Change the background color
this.BackColor = Color.BurlyWood;
this.Font = new Font("Verdana", 16);
}
}
Build your project.
After that, look in your toolbox:
I didn't help you answer the auto complete, I have to make dinner now, but I wanted to give you the head start.
Search for auto complete tutorials, and it shouldn't be that hard for you to put a list box below your textbox. For this you want to create a custom User Control, and handle the keydown event of the textbox to filter your list.
Edit:
You also don't have to inherit from TextBox if you don't need to. What you can also do is combine two controls, like I do here with my LabelTextBoxControl, LabelComboBoxControl, LabelCheckBoxControl, etc. I didn't want to have to always drag two controls on to a form, so I combined a Label and other base controls.
If you want to see a sample open source project that has these type of user controls, (for Windows Forms) it is located here:
https://github.com/DataJuggler/DataJuggler.Win.Controls
P.S. > Always ignore the down voters on this site. This is how I learned to create user controls. Create what you want.
Related
I am working in windows forms application and have a following issue. I use tabControl in my application and there is a need to change a content inside certain tabPages when users perform specific actions.
for example tabPage one contains a text area and a button, when user clicks button information inside a text area should be stored somehow, and that same tabPage should display new content e.g. more text areas, buttons etc, I assume it is easier to do by using views inside it, so one view can be hidden and another can be shown.
This a to a degree a matter of taste. You can chose to show and hide controls one by one in a method or you can group them in a UserControl which you then show or hide in one command.
I would base my decision one way or the other by these questions:
Are there controls, that will always be visible and how is the layout for these?
How many controls are there to show/hide?
Is there a need to reuse one or more of your views?
The last question may make the big difference: If you want re-use, do go for the UserControl. It is basically meant to do just that: Group controls, like a form does.
For just a few controls doing it in a one by one (in a switchViewMode-method) would suffice, imo.
To add UCs you right-click your project in the project-explorer and chose add - usercontrol. Then chose a nice name, like UC_Goods or UC_Services or whatever your shop policy suggests.
You are then presented with the empty GUI. Now add the controls you need.
Here a decision is to be made: If you will reuse it make sure the controls get generic names! If not it doesn't matter. The reason is, that when you add two instances of the same UC, their controls will have the same names and you will have to qualify them by the parent (the UC)
Here you also script events etc..
Finally add instances to the TabPage as need like this:
public UC_Goods uc_goodsDelivered = new UC_Goods();
public UC_Goods uc_goodsOnHold = new UC_Goods();
public UC_Services uc_ItServices = new UC_Services ();
public Form1()
{
InitializeComponent();
tab.tp_goodsPage.Controls.Add(uc_goodsDelivered);
tab.tp_goodsPage.Controls.Add(uc_goodsOnHold);
goodsOnHold.Hide();
tab.tp_goodsPage.Controls.Add(uc_ItServices);
uc_ItServices .Hide();
// take care of dock style or anchors..
// ..and initialzing fields..
}
This delclares two UC classes and two and one instance of each respectively. Only one is visible. Since one class is used twice its controls have ambigous names until you qualify them e.g. like this: uc_goodsDelivered.Status...
hth
I am working on a WinForm based applications(Yes I don't know WPF) and want's a dashboard like panels in it. Picture given below
Each panel will have a title and records from Database and some action controls. How could this be achieved? I don't want to use GridControl as I don't want to show Excel like spreadsheet here. How could this be achieved?
It sounds like you want to make a UserControl, possibly coupled with an automatic layout panel like FlowLayoutPanel.
Simply speaking, you would create a UserControl with whatever properties and events you require (i.e. in your example you might have a Title property and a Data property), and any events you need to respond to (e.g. you might have a button that you provide a wrapper event for). Then you can add the control to your existing form as you would any other standard control.
As far as displaying data in list form goes, one suggestion is to use a Panel and dynamically add Labels to it. Another idea could be just a simple Label with line breaks in the Text.
I would like to develop a custom Toolstrip control. The control would be like a ToolStrip but with the following features :
The textbox would be proceeded by a label
The textbox would have two buttons after it.
I guess to implement this then a user drawn control would be required. Does anyone know of any resources available on the web to get me started on this.
First you should compose your usercontrol with label, textbox and buttons, exposing all the necessary Properties.
Here's a MSDN WalkThrough for UserControls creation
Then, use this answer (or this MSDN example) to create the custom ToolStripItem, just replace the TrackBar with you custom control.
P.S.
If you don't want to create a ToolStripItem, but just a popup showing your custom control, you can use this other example, replacing the ListBox with your control.
I have a Form with some data like name, favorite food or something, and want to be able to change these in a new form. This settings form should have the standard OK/Cancel/Apply buttons and update the main forms values on apply directly.
What's the best way to do this?
Thanks
This will not cover the complete approach, but I'd recommend the PropertyGrid control for powerful and fast settings editing, but it might be not so user friendly so it depends what your target user group is. You can keep all your settings in a class with a public property for each setting. When setting the PropertyGrids SelectedObject to an instance of this object you will be able to edit all settings directly. You can use some attributes to control the display of the properties.
You can find a nice tutorial on using this Grid here. Creating a basic Dialog Form with three buttons should be simple. Hope that helps.
Create a class that will store the data.
Show the data (perhaps using databinding in the first form and pass the same object to the edit form to initialize the controls in the edit form.
After closing the edit form with OK or Apply write the values of the controls to the data object.
I want to use a win-form in C# and display its contents or the whole form in a panel used in another form.
Lets say i have a form A with a panel Panel_of_A
and a form B with a panel Panel_of_B
I want to display contents of Panel_of_A into Panel_of_B or you may say i want to use Panel_of_A in another form.
Or you may provide help for displaying a form say A into a panel on form B.
Hope i made u understand my thoughts...
I think a better way for doing this is to create a User Control which contains that panel and (re)use it on both forms.
Here is a tutorial how to create User Controls on MSDN.
Create a user control that contains the logic you want to replicate, then incude the new user control in both places.
How's this for displaying an instance of FormA inside a panel on FormB? The UserControl is probably the better way to go, but what you asked for is possible.
fa = new FormA();
fa.TopLevel = false;
fa.FormBorderStyle = FormBorderStyle.None;
fa.Dock = DockStyle.Fill;
fb = new FormB();
fb.panel1.Controls.Add(fa); // if panel1 was public
fa.Show();
fb.Show();
I would suggest defining the panel you want to reuse as a separate class, and place this on both forms. If you need the displayed data to be the same as well, bind to a business object than can be passed between the forms.
Depending on the size and scale of your application, you may want to consider the (free) Composite UI Application Block
From the blurb:
It provides proven practices to build
complex smart client user interfaces
based on well known design patterns
such as the Composite pattern, in
which simple user interface parts can
be combined to create complex
solutions, but at the same time
allowing these parts to be
independently developed, tested, and
deployed.